Thread: g_hWndMain = hWnd;error C2065,C2440

  1. #1
    Unregistered
    Guest

    Question g_hWndMain = hWnd;error C2065,C2440

    ...cpp(155) : error C2065: 'g_hWndMain' : undeclared identifier
    ?Where should I declare this?

    ...cpp(155) : error C2440: '=' : cannot convert from 'struct HWND__ *' to 'int' This conversion requires a reinterpret_cast, a C-style cast or function-style cast


    I don't have a clue as to what I am doing incorrectly.

    This is an example from 'The ZEN of Direct3d Progamming' by Peter Walsh.

    Most of this stuff is above my head.
    All of my other programs I have written as Console apps. So any help you would give me would be greatly appreciated.






    // Notes: Random Rectangles.

    #define WIN32_LEAN_AND_MEAN

    // Include files.
    #include <windows.h>
    #include <stdlib.h>


    //A global handle to the main window
    HWND g_hWindMain;


    int GameLoop ();




    ////////////////////////////////////////////
    ////////////////////////////////////////////
    // The windows procedure to handle events.//
    ////////////////////////////////////////////
    ////////////////////////////////////////////

    long CALLBACK WndProc( HWND hWnd, UINT uMessage,
    WPARAM wParam, LPARAM lParam )
    {
    PAINTSTRUCT PaintStruct; //Structure used during windows painting.
    HDC hDC; //Handle to device context for painting.

    //Switch the windows message to figure out what it is.
    switch( uMessage )
    {
    case WM_CREATE: //The CreateWindow() Function was just called.
    {
    // One Time Initialization
    return 0;
    }

    case WM_PAINT: // The window needs to be redrawn.
    {
    // Tell windows we want to start updating the window.

    hDC = BeginPaint( hWnd, &PaintStruct );


    /////////////////////////////////////
    /////////////////////////////////////
    // //
    // Do any drawing with the GDI here//
    // //
    /////////////////////////////////////
    /////////////////////////////////////


    // Tell windows we have finisthed updating the window.
    EndPaint( hWnd, &PaintStruct );
    return 0;
    }
    case WM_DESTROY: // The window is about to be closed.
    {
    // This means we want our app to exit.

    // Tell windows to put a WM_QUIT message in our message queue.
    PostQuitMessage( 0 );
    return 0;
    }
    default:
    {
    // Let windows handle this message.
    return DefWindowProc( hWnd, uMessage, wParam, lParam );
    }
    }
    }


    ///////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////
    // The windows entry point. The app. starts here. //
    ///////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////


    int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
    PSTR pstrCmdLine, int iCmdShow )
    {
    HWND hWnd; // The handle to out main window.
    MSG msg; // The message windows is sending us.
    WNDCLASSEX wc;
    // The window class used to create our window.

    // The NAME of our class and also the TITLE to out window.
    static char strAppName[] = "Direct 3d App.";

    LOGBRUSH CrossHatchBrush;

    CrossHatchBrush.lbStyle = BS_HATCHED;
    CrossHatchBrush.lbColor = RGB ( 0,0,0);
    CrossHatchBrush.lbHatch = HS_CROSS;

    HBRUSH hCrossBrush = CreateBrushIndirect( &CrossHatchBrush );


    /////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////
    //Fill in the window class with the attributes for our main window.//
    /////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////


    // The size of this struct. in bytes.
    wc.cbSize = sizeof( WNDCLASSEX );
    // The style of the window.
    wc.style = CS_HREDRAW |CS_VREDRAW |CS_OWNDC;

    // Useless info. Just set to 0.
    wc.cbClsExtra = 0;
    // Useless info. Just set to 0.
    wc.cbWndExtra = 0;
    // The name of our event handler
    wc.lpfnWndProc = WndProc;
    // A handle to the applications instance.
    wc.hInstance = hInstance;
    // The handle to to the brush for the windows background.DKGRAY_BRUSH
    wc.hbrBackground = hCrossBrush; //(HBRUSH)GetStockObject( BS_HOLLOW );
    // A handle to the icon to use for the window.
    wc.hIcon =LoadIcon( NULL, IDI_APPLICATION );
    // A handle to a smaller version of the apps. icon.
    wc.hIconSm = LoadIcon( NULL, IDI_HAND );
    // A handle to the cursor to use while the mouse is over our window.
    wc.hCursor = LoadCursor(NULL, IDC_CROSS );
    // A handle to the resource touse as our menu.
    wc.lpszMenuName = NULL;
    // The human readable name for this class.
    wc.lpszClassName = strAppName;

    // Register the class with windows.
    RegisterClassEx( &wc );

    // Create the window based on the previous class.
    hWnd = CreateWindowEx(NULL, // Advanced style settings.
    strAppName, // The name of the class.
    strAppName, // The window caption.
    WS_OVERLAPPEDWINDOW, // The window style.
    CW_USEDEFAULT, // The initial x position.
    CW_USEDEFAULT, // The initial y position.
    512,512, // The initial width / height.
    NULL, // Handle to the parent window.
    NULL, // Handle to the menu.
    hInstance, // Handle to the apps instance.
    NULL); // Advanced context.

    //keep the window handle in a global
    g_hWndMain = hWnd;

    /************************************************** ************************
    ************************************************** *************************
    ************************************************** *************************

    The previous line gives the errors:

    I looked these error up on MSDN but that didn't help me.

    ?????????????????????????????????????????????????? ??????????
    ?????????????????????????????????????????????????? ??????????
    ************************************************** *************************
    ************************************************** *************************
    ************************************************** *************************/

    // Display the window we just created.
    ShowWindow( hWnd, iCmdShow );
    // Draw the window contents for the fist time.
    UpdateWindow( hWnd );

    ////////////////////////////
    ////////////////////////////
    // Start the message loop.//
    ////////////////////////////
    ////////////////////////////

    while( TRUE )
    {
    if(PeekMessage( &msg,NULL, 0, 0, PM_REMOVE ) )
    {
    if( msg.message==WM_QUIT )
    break;
    TranslateMessage( &msg );
    DispatchMessage( &msg);
    }
    else
    {
    //Run the game loop here.
    GameLoop();
    }


    }
    DeleteObject( hCrossBrush );
    return msg.wParam;
    }



    int GameLoop()
    {
    //Create a handle to the DC
    HDC hDC;
    //Get a DC for this window
    hDC = GetDC( g_hWndMain );
    //Rect structure to hold the dimensions of the client area
    RECT ClientRect;
    //Get the client area size
    GetClientRect( g_hWndMain, &ClientRect );
    //Make sure the window is not minimized
    if( ClientRect.bottom == 0 || ClientRect.right == 0 )
    {
    //Return if the window is minimized
    ReleaseDC( g_hWndMain, hDC );
    return 0;
    }
    //Create a solid pen for the rectangular border
    //with a rndom width and random color
    HPEN hPen = CreatePen( PS_SOLID, rand()%5,
    RGB( rand()%255, rand()%255, rand()%255 ) );

    //Brush structure to describe the brush
    LOGBRUSH logBrush;

    //The color will be random
    logBrush.lbColor =RGB( rand()%255, rand()%255, rand()%255 );
    //We are not using a hatch
    logBrush.lbHatch = 0;
    //The fill should be solic
    logBrush.lbStyle = BS_SOLID;

    //Create the brush
    HBRUSH hBrush = CreateBrushIndirect( &logBrush );

    //Select the brush into the device context
    SelectObject( hDC, hBrush );

    //Select the pen into the device context
    SelectObject( hDC, hPen );

    //Draw the rectangle using random dimensions
    Rectangle(hDC, rand()%ClientRect.right,
    rand()%ClientRect.bottom,
    rand()%ClientRect.right,
    rand()%ClientRect.bottom );

    //Get rid of the brush
    DeleteObject( SelectObject( hDC, GetStockObject( BLACK_BRUSH ) ) );
    //Get rid of the pen
    ReleaseDC( g_hWndMain, hDC );
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    As a global variable you have 'HWND g_hWindMain' and in WinMain() you have 'g_hWndMain'.

  3. #3
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    Thank you. I feel like an IDIOT! NT

    Thank you!

  4. #4
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    Question Weird problems. More than one instance?

    I changed my global variable to the correct name. Almost everything works now except:

    if you hold your cursor over the minimize, maximize, or close button. It creates another window partially obscured by the first window that includes my title bar. Then if you do close the window it screws up the desktop and I have to click 'show desktop' several times to get it back to normal.

    What in the world is causing this?

Popular pages Recent additions subscribe to a feed