开发者

Win32: No Window Appears

I was programming the example code from Frank Luna's book "Introduction to 3D Game Programming with DirectX 10". The code is the first Win32 example in the Appendix A: Windows Programming section.

Right now, the program compiles under both VC++ 2008/2010, but no window appears, although the d开发者_如何学运维ebug session has started and I have to forcefully close it. I have no idea where it is, I'm not using Win32 Console mode, I have closed all other windows and no other IDE or session of VC++ is running.

Any idea why this might be happening?

PS: I have also checked my Processes. It is indeed running.

    #include <Windows.h>

    HWND ghMainWnd = 0;

    bool InitWindowsApp(HINSTANCE instanceHandle, int show);
    int Run();

    LRESULT CALLBACK
        WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

    int WINAPI 
        WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int nShowCmd )
    {
        if( !InitWindowsApp(hInstance, nShowCmd) )
            return 0;

        return Run();
    }

    bool InitWindowsApp( HINSTANCE instanceHandle, int show )
    {
        WNDCLASS wc;

        wc.style         = CS_HREDRAW | CS_VREDRAW;
        wc.lpfnWndProc   = WndProc;
        wc.cbClsExtra    = 0;
        wc.cbWndExtra    = 0;
        wc.hInstance     = instanceHandle;
        wc.hIcon         = LoadIcon(0, IDI_APPLICATION);
        wc.hCursor       = LoadCursor(0, IDC_ARROW );
        wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        wc.lpszMenuName  = 0;
        wc.lpszClassName = L"BasicWndClass";

        if( !RegisterClass(&wc) ) 
        {
            MessageBox(0, L"RegisterClass FAILED", 0, 0);
            return false;
        }

        ghMainWnd = CreateWindow(
            L"BasicWndClass",
            L"Win32Basic",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            0,
            0,
            instanceHandle,
            0);

        if( ghMainWnd = 0 )
        {
            MessageBox( 0, L"Window Creation FAILED", 0, 0 );
            return false;
        }

        ShowWindow( ghMainWnd, show );
        UpdateWindow( ghMainWnd );

        return true;
    }

    int Run()
    {
        MSG msg = {0};
        BOOL bRet = 1;

        while( bRet = GetMessage( &msg, 0, 0, 0 ) != 0 )
        {
            if( bRet == -1 )
            {
                MessageBox( 0, L"GetMessage FAILED", 0, 0 );
                break;
            }
            else
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }

        return (int)msg.wParam;
    }

    LRESULT CALLBACK
        WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
    {
        switch( msg )
        {
        case WM_LBUTTONDOWN:
            MessageBox( 0, L"Hello, World", 0, 0 );
            return 0;

        case WM_KEYDOWN:
            if( wParam == VK_ESCAPE )
                DestroyWindow( ghMainWnd );
            return 0;

        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        }

        return DefWindowProc(hWnd, msg, wParam, lParam);
    }


Change this:

if( ghMainWnd = 0 ) 
        { 
            MessageBox( 0, L"Window Creation FAILED", 0, 0 ); 
            return false; 
        }

to:

if( ghMainWnd == 0 ) 
        { 
            MessageBox( 0, L"Window Creation FAILED", 0, 0 ); 
            return false; 
        } 

Two equals signs instead of one. :)


Wild guess: _UNICODE is not defined by project settings. Use CreateWindowW, RegisterClassW, to avoid dependency.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜