Thread: newbie Windows problem...

  1. #1
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942

    newbie Windows problem...

    Code:
    #include <windows.h>
    int WINAPI WinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine,
                       int nShowCmd)
    {
                       WNDCLASS wndclass;
                       HWND     hWindow;
                       if(RegisterClass(&wndclass) == 0)
                                                   return FALSE;
                       hWindow = CreateWindow
                       ("MyWindow",            // from wndclass.lpszClassName
                       "Gcn_Zelda's Window",        // this will show on title bar
                        WS_OVERLAPPEDWINDOW,   // border, title, min-max-close button
                        // WS_OVERLAPPED          // border, title
                        // WS_POPUP               // no title, no button
                        // WS_VISIBLE             // window is visible from start
                        // WS_VSCROLL             // window has vertical scroll bar
                        // WS_HSCROLL             // window has horizontal scroll bar
                        // WS_MAXIMIZE            // window is created in maximized state
                        // WS_MINIMIZE            // window is created in minimized state
                        0,                     // starting x-coord (upper-left)
                        0,                     // starting y-coord (upper-left)
                        CW_USEDEFAULT,         // window's x-size
                        CW_USEDEFAULT,         // window's y-size
                        NULL,                  // parent window's handle
                        NULL,                  // handle to a menu
                        hInstance,             // copy from WinMain()
                        NULL);                 // disregard...
                        if(hWindow == NULL)
                                return FALSE;
                        ShowWindow(hWindow, SW_SHOW);        // show created window
                                         // SW_HIDE          // hide created window
                                         // SW_RESTORE       // show window in restored state
                                         // SW_MAXIMIZE      // show window in maximized state
                                         // SW_MINIMIZE      // show window in minimized state
    }
    My program compiles, but the Console screen pops up and dissapears quickly. Does anybody know the problem? I'm using Dev C++ 4....

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Create a windows project, not a console project.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    ...stupid me...thanks.

  4. #4
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942

    I tried

    I just tried that and it still doesn't work. Now, the Dev-C++ screen minimizes and nothing happens. It says "Compilation Completed"

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    not sure what your problem is, but you might want to do more if an error occurs, like:
    Code:
    if(hWindow == NULL)
    {
    MessageBox(NULL, "Window Creation Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);                            
    return FALSE ; //now return error
    }
    oh, and don't you have to do more to register the window? i haven't done windows programming in a while...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> don't you have to do more to register the window?

    Not to actually register the class, but you'll certainly want to fill in the members of your wndclass structure, here is an example from one of my tutorial programs...

    Code:
        WinClass.cbSize = sizeof(WNDCLASSEX);   
        WinClass.hInstance = hThisInst;
        WinClass.lpszClassName = "Window";
        WinClass.lpfnWndProc = WindowFunc; 
        WinClass.style = 0;     
        WinClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        WinClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
        WinClass.hCursor = LoadCursor(NULL, IDC_ARROW);
        WinClass.lpszMenuName = NULL; 
        WinClass.cbClsExtra = 0; 
        WinClass.cbWndExtra = 0;
        WinClass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    ... change the structure name and any of the other things you want, NOTE I am using WNDCLASSEX, not WNDCLASS.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    isn't a callback function also required to handle the painting of the window? or will windows automatically handle that?

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Actually, you are missing someting crucial here. After registering you need to setup the callback system for your program:

    Example
    Code:
      MSG messages;
    
      while(GetMessage(&messages, NULL, 0, 0)) {
         TranslateMessage(&messages);
         DispatchMessage(&messages);
      }

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Yes, you are missing several important points:

    * You need to fill in some of the data members before you register the class. For example, you aren't supplying a class name or a callback function, so the class is useless (and you're making a window for a class that doesn't exist because you registered a class with an unknown name (an uninitialized name), but try to make a window of class "MyWindow"

    * You need a message pump (as master5001 said)

    * You need a window procedure (callback) that, at a minimum, responds to WM_DESTROY by calling PostQuitMessage(), and that passes all other calls to the default window procedure. This procedure is another function that you must specify in the class structure.

  10. #10
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I had assumed that what you had posted was an extract. If that is, indeed, your whole program, then as the others have added, you are way off. Attached is a real basic Windows program.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. windows problem
    By ElastoManiac in forum Windows Programming
    Replies: 3
    Last Post: 11-25-2005, 07:17 AM
  2. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  3. Replies: 1
    Last Post: 10-18-2005, 10:20 AM
  4. newbie question about using windows app in Dev C++
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2002, 10:50 PM
  5. Windows 2000 Problem
    By DISGUISED in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 12-19-2001, 09:43 PM