Thread: Errors with simple window handler

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    6

    Errors with simple window handler

    OK, so I'm trying to learn C++ and get into game programming in windows.... I've been using the book Tricks of the Windows Game Programming Gurus and I had some trouble compiling the second example. I'm using VC++ 6.0 Introductory Edition and am running Win XP.

    This is my WINMAIN.... I used ^ and ^^ to indicate the lines that the errors occure on.

    Code:
    int WINAPI WinMain(HINSTANCE hinstance, 
                        HINSTANCE hprevinstance, 
                        LPSTR lpcmdline, 
                        int ncmdshow)
    {
        WNDCLASSEX winclass;    // this will hold the class we create
        HWND hwnd;              // generic window handle
        MSG msg;                // generic message
        
        // first fill in the window class structure
        winclass.cbSize         = sizeof(WNDCLASSEX);
        winclass.style          = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
        winclass.lpfnWndProc    = WindowProc;
        winclass.cbClsExtra     = 0;
        winclass.cbWndExtra     = 0;
        winclass.hInstance      = hinstance;
        winclass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
        winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
       ^winclass.hbrBackground  = GetStockObject(BLACK_BRUSH);
        winclass.lpszMenuName   = NULL;
        winclass.lpszClassName  = WINDOW_CLASS_NAME;
        winclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);
        
        // register the window class
        if (!RegisterClassEx(&winclass))
            return(0);
            
        // create the window
        if (!(hwnd = CreateWindowEx(NULL,                   // extended style
                                    WINDOW_CLASS_NAME,      // class
                                    "Your Basic Window",    // title
                                  ^^WS_OVERLAPPEDWINDOW | WS_VISBLE,
                                    0,0,                    // initial x,y
                                    400,400,                // initial width, height
                                    NULL,                   // handle to parent
                                    NULL,                   // handle to menu
                                    hinstance,              // instance of this application
                                    NULL)))                 // extra creation parms
            return(0);
            
        // enter main event loop
        while(GetMessage(&msg,NULL,0,0))
        {
            // translate any accelerator keys
            TranslateMessage(&msg);
            
            // sned the message to the window proc
            DispatchMessage(&msg);
        } // end while
        
        // return to Windows like this
        return(msg.wParam);
        
    
    } // end WinMain
    I get the following errors during compile...

    ^error C2440: '=' : cannot convert from 'void *' to 'struct HBRUSH__ *'
    Conversion from 'void*' to pointer to non-'void' requires an explicit cast

    ^^error C2065: 'WS_VISBLE' : undeclared identifier
    Error executing cl.exe.

    If you need any more info to help me out just ask.

    Thanks.

  2. #2
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    >> winclass.hbrBackground = GetStockObject(BLACK_BRUSH);

    Change it to

    winclass.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);


    >> ^^WS_OVERLAPPEDWINDOW | WS_VISBLE,

    Change it to

    WS_OVERLAPPEDWINDOW | WS_VISIBLE,

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    WS_VISABLE is not a paramater of dwExStyle so get rid of it
    if you want to show your window do this after you create the window
    Code:
    ShowWindow(hwnd,SW_SHOWNORMAL);
    UpdateWindow(hwnd);
    //msg loop
    Woop?

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    6
    Awesome Thanks guys.... I'm getting a linking error now though

    --------------------Configuration: main - Win32 Debug--------------------
    Compiling...
    main.cpp
    Linking...
    LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
    Debug/main.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    main.exe - 2 error(s), 0 warning(s)

    Again, if you need more info just ask.

    Thanks

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Can you post all your code?
    [edit] Also a word of advice use some braces to begin and end your if statments also some indentation too, you will be happy you did this later.[/edit]
    Last edited by prog-bman; 08-29-2004 at 08:22 PM.
    Woop?

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    6
    By request....:

    Code:
    //WINDOWS - A Complete Windows Program
    
    // INCLUDES ///////////////////////////
    #define WIN32_LEAN_AND_MEAN // just say no to MFC
    
    #include <windows.h>   // include all the windows headers
    #include <windowsx.h>  // include useful macros
    #include <stdio.h> 
    #include <math.h>
    
    // DEFINES ////////////////////////////
    #define WINDOW_CLASS_NAME "WINCLASS1"
    
    // GLOBALS ////////////////////////////
    
    // FUNCTIONS //////////////////////////
    LRESULT CALLBACK WindowProc(HWND hwnd, 
                                UINT msg, 
                                WPARAM wparam, 
                                LPARAM lparam)
    {
        // this is the main message handler of the system
        PAINTSTRUCT ps;     // used in WM_PAINT
        HDC hdc;            // handle to a device context
        
        // what is the message
        switch(msg)
        {
            case WM_CREATE:
            {
                // do initialization stuff here
    
                // return success
                return(0);
            } break;
            
            case WM_PAINT:
            {
                // simply validate the window
                hdc = BeginPaint(hwnd,&ps);
    
                // you would do all your painting here
                
                EndPaint(hwnd,&ps);
                
                // return success
                return(0);
            } break;
            
            case WM_DESTROY:
            {
                // kill the application, this sends a WM_QUIT message
                PostQuitMessage(0);
                
                // return success
                return(0);
            } break;
            
            default: break;
            
        }   // end switch
        
        // process any messages that we didn't take care of
        return (DefWindowProc(hwnd, msg, wparam, lparam));
        
    }   //end WinProc
    
    // WINMAIN ///////////////////////////////
    
    int WINAPI WinMain(HINSTANCE hinstance, 
                        HINSTANCE hprevinstance, 
                        LPSTR lpcmdline, 
                        int ncmdshow)
    {
        WNDCLASSEX winclass;    // this will hold the class we create
        HWND hwnd;              // generic window handle
        MSG msg;                // generic message
        
        // first fill in the window class structure
        winclass.cbSize         = sizeof(WNDCLASSEX);
        winclass.style          = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
        winclass.lpfnWndProc    = WindowProc;
        winclass.cbClsExtra     = 0;
        winclass.cbWndExtra     = 0;
        winclass.hInstance      = hinstance;
        winclass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
        winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
        winclass.hbrBackground  = (HBRUSH) GetStockObject(BLACK_BRUSH);
        winclass.lpszMenuName   = NULL;
        winclass.lpszClassName  = WINDOW_CLASS_NAME;
        winclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);
        
        // register the window class
        if (!RegisterClassEx(&winclass))
            return(0);
            
        // create the window
        if (!(hwnd = CreateWindowEx(NULL,                   // extended style
                                    WINDOW_CLASS_NAME,      // class
                                    "Your Basic Window",    // title
                                    WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                                    0,0,                    // initial x,y
                                    400,400,                // initial width, height
                                    NULL,                   // handle to parent
                                    NULL,                   // handle to menu
                                    hinstance,              // instance of this application
                                    NULL)))                 // extra creation parms
            return(0);
            
        // enter main event loop
        while(GetMessage(&msg,NULL,0,0))
        {
            // translate any accelerator keys
            TranslateMessage(&msg);
            
            // sned the message to the window proc
            DispatchMessage(&msg);
        } // end while
        
        // return to Windows like this
        return(msg.wParam);
        
    } // end WinMain
    
    ///////////////////////////////////////////////////////
    This is basically copied from the book, so this isn't my code formatting. I usually use braces and indentation in my IF statements. I just need to get this to work to really be able to learn from it.

  7. #7
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Code:
    #define WIN32_LEAN_AND_MEAN // just say no to MFC
    win32 has no idea about MFC so this is tottally wrong. This is just saying don't include unnecessary headers
    umm it does compile for me i just get some warnings about NULL being passed here
    Code:
    if (!(hwnd = CreateWindowEx(NULL,                   // extended style
                                    WINDOW_CLASS_NAME,      // class
                                    "Your Basic Window",    // title
                                    WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                                    0,0,                    // initial x,y
                                    400,400,                // initial width, height
                                    NULL,                   // handle to parent
                                    NULL,                   // handle to menu
                                    hinstance,              // instance of this application
                                    NULL)))
    But easily fixed by passing 0 as the first parameter instead of NULL.
    What book are you using? Maybe you should invest in a better one or you could check out these good online tuts http://www.winprog.org/tutorial/
    Last edited by prog-bman; 08-29-2004 at 09:52 PM.
    Woop?

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    6
    I mentioned earlier that I was using the book "Tricks of the Windows Game Programming Gurus". Thats interesting that it compiles fine for you other than some warnings. I wonder if its just some settings in my compiler then?

    I'll definately read through those tutorials you mentioned.

    So, no suggestions for fixing the linking error then??

  9. #9
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Create a Win32 Project, not a Win32 Console project.

  10. #10
    Registered User
    Join Date
    Aug 2004
    Posts
    6
    Thanks Dante... I guess the default is a Win32 Console project; I never actually made a project before I just loaded the .cpp file and then made a workspace.

    So it compiles fine now. But it still tells me it can't execute the file... Are there some settings I have to set up first??

  11. #11
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    But it still tells me it can't execute the file
    Strange. Is there an error message? Could you copy and paste it?

  12. #12
    Registered User
    Join Date
    Aug 2004
    Posts
    6
    It simply says...

    Cannot Execute Program
    Thats all it says....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  2. Window scrollbar
    By maxorator in forum Windows Programming
    Replies: 2
    Last Post: 10-07-2005, 12:31 PM
  3. my wndProc is out of scope
    By Raison in forum Windows Programming
    Replies: 35
    Last Post: 06-25-2004, 07:23 AM
  4. dont want to use all params
    By stallion in forum Windows Programming
    Replies: 2
    Last Post: 02-18-2003, 08:10 AM
  5. Winamp Vis if anyone can help
    By Unregistered in forum Windows Programming
    Replies: 6
    Last Post: 01-27-2002, 12:43 AM