CreateWindow 从不触发 WM_CREATE
CreateWindow never triggers WM_CREATE
我有以下代码;
#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
没有被触发?
它不会触发任何消息,因为消息循环还没有被调用。
你必须把这个放在CreateWindow
之后
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
此外,WndProc
不应该只是 return 零。它应该 return 如下:
return DefWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
Visual Studio或其他IDE的可以创建C++ -> Win32工程基础工程,它设置了所有代码。
创建的window来不及接收消息,立即关闭。您应该循环调用 GetMessage 以使 window 保持活动状态。
// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
我有以下代码;
#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
没有被触发?
它不会触发任何消息,因为消息循环还没有被调用。
你必须把这个放在CreateWindow
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
此外,WndProc
不应该只是 return 零。它应该 return 如下:
return DefWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
Visual Studio或其他IDE的可以创建C++ -> Win32工程基础工程,它设置了所有代码。
创建的window来不及接收消息,立即关闭。您应该循环调用 GetMessage 以使 window 保持活动状态。
// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;