CreateWindow永远不会触发WM_CREATE

时间:2016-03-11 09:41:02

标签: windows-8 wndproc

我有以下代码;

#include <windows.h>

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
    printf("%d\n", message);

    return 0;
}

int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){

    WNDCLASSEX wc = {0};

    wc.cbSize = sizeof(WNDCLASSEX);

    wc.lpfnWndProc   = WndProc;
    wc.hInstance     = hInstance;
    wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
    wc.lpszClassName = "oglversionchecksample";
    wc.style = CS_OWNDC;

    if(!RegisterClassEx(&wc))
        return 1;

    CreateWindow(wc.lpszClassName, "openglversioncheck", WS_OVERLAPPED, 0, 0, 640, 480, 0, 0, hInstance, 0);

    return 0;
}

致电CreateWindow()会触发消息号码36 WM_GETMINMAXINFO,129 WM_NCCREATE,然后是130 WM_NCDESTROY,但消息号1 WM_CREATE永远不会像预期的那样触发是。

我做错了导致WM_CREATE没有被触发?

2 个答案:

答案 0 :(得分:3)

它不会触发任何消息,因为尚未调用消息循环。

你必须在CreateWindow

之后加上这个
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

此外,WndProc不应该只返回零。它应该返回以下内容:

return DefWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

Visual Studio或其他IDE可以创建C ++ - &gt; Win32项目基本项目,它设置了所有代码。

答案 1 :(得分:2)

创建的窗口没有时间接收消息,它立即关闭。您应该在循环中调用GetMessage以使窗口保持活动状态。

// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
}
return Msg.wParam;

请参阅此示例:http://www.winprog.org/tutorial/simple_window.html