-1
\$\begingroup\$

The program compiles and runs successfully but does not show a window, it simply ends immediately.

#include <Windows.h>


LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);


int WINAPI wWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPWSTR CmdLine, int CmdShow )
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(CmdLine);

    WNDCLASSEX wndClass = {0};

    wndClass.cbSize = sizeof(WNDCLASSEX);
    wndClass.style = CS_HREDRAW | CS_VREDRAW;
    wndClass.lpfnWndProc = WndProc;
    wndClass.hInstance = hInstance;
    wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wndClass.lpszMenuName= NULL;
    wndClass.lpszClassName= "DX11BookWindowClass";

    if(!RegisterClassEx(&wndClass));
        return -1;

        RECT rc = {0,0,640,480};
        AdjustWindowRect(&rc,WS_OVERLAPPED,FALSE);

        HWND hwnd = CreateWindowA( "DX11BookWindowClass", "FirstWinProgram", WS_OVERLAPPEDWINDOW,
                                CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top,
                                NULL, NULL, hInstance, NULL );

        if(!hwnd)
            return -1;

        ShowWindow(hwnd,CmdShow);

        MSG msg = {0};

        while( msg.message != WM_QUIT )
        {
            if( PeekMessage(&msg,0,0,0,PM_REMOVE) )
            {
                TranslateMessage( &msg );
                DispatchMessage( &msg );
            }
            else
            {
                //Update
                //Draw
            }

        }
        //Demo ShutDown

        return static_cast<int>(msg.wParam);
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT paintstruct;
    HDC hDC;

    switch(message)
    {
    case WM_PAINT:
        {
        hDC = BeginPaint( hwnd,&paintstruct);
        break;
        }
    case WM_DESTROY:
        {
        PostQuitMessage( 0 );
        break;
        }
    default:
        {
        return DefWindowProc( hwnd, message, wParam, lParam);
        }

    }
    return 0;
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Practice some basic debugging. Ensure what you're expecting matches with what the program is actually coded to do. Put print statements or even better set break points and see why the program exits when you think it should keep going. I'm voting to close this question as too localized, since it's essentially a "debug my code for me" question. Read up on the FAQ to learn what types of questions to ask here (and what kind not to). \$\endgroup\$
    – House
    Commented Apr 7, 2013 at 16:07

1 Answer 1

0
\$\begingroup\$

oh dear...classic typo

if(!RegisterClassEx(&wndClass)); <---- typo, remove the ; that it works
return -1;

this means that the program always returns there

work on your coding style (i recommend to use everytime braces)

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged .