PostQuitMessage(0) 不会终止我的 'program'
PostQuitMessage(0) won't terminate my 'program'
我只是想学习一点 C++ 并写了几行除了打开 window 什么都不做。
我也添加了一个消息处理程序,通过单击 window 上的 [X],它会按预期关闭。
作为下一步,我希望程序在单击 [X] 时终止,但它没有这样做。
这是我的代码:
#include <Windows.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){
switch (msg){
case WM_CLOSE:
PostQuitMessage(88);
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE pPrevInstance, LPSTR lpCmdLine, int cCmdShow) {
const auto pClassName = "M3D";
WNDCLASSEX wc = { 0 };
wc.cbSize = sizeof(wc);
wc.style = CS_OWNDC;
wc.lpfnWndProc = DefWindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = nullptr;
wc.hCursor = nullptr;
wc.hIcon = nullptr;
wc.hCursor = nullptr;
wc.hbrBackground = nullptr;
wc.lpszMenuName = nullptr;
wc.lpszClassName = pClassName;
RegisterClassEx(&wc);
HWND hWnd = CreateWindowEx(0, pClassName, "Fenster M3D", WS_CAPTION | WS_MAXIMIZEBOX | WS_SYSMENU, 200, 200, 640, 480, nullptr, nullptr, hInstance, nullptr);
//Fenster aufrufen
ShowWindow(hWnd, SW_SHOW);
//
//message Pumpe
MSG msg;
while (GetMessage(&msg,nullptr,0,0) > 0){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
谁能告诉我哪里出了问题以及为什么我的 PostQuitMessage() 无法正常工作?
我发现的其他线程并没有真正帮助
问题出在这个作业上:
wc.lpfnWndProc = DefWindowProc;
您告诉消息循环将消息直接发送到 DefWindowProc()
,因此您的自定义 WndProc()
永远不会被使用,所以 PostQuitMessage()
永远不会被调用。 DefWindowProc()
不会为其处理的任何消息调用 PostQuitMessage()
。
改为将该分配改为:
wc.lpfnWndProc = WndProc;
此外,根据 MSDN 上的 Closing the Window 文档,DefWindowProc()
在处理 WM_CLOSE
时调用 DestroyWindow()
,因此您应该调用 PostQuitMessage()
来回复 WM_DESTROY
改为:
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){
switch (msg){
case WM_DESTROY:
PostQuitMessage(88);
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
我只是想学习一点 C++ 并写了几行除了打开 window 什么都不做。 我也添加了一个消息处理程序,通过单击 window 上的 [X],它会按预期关闭。 作为下一步,我希望程序在单击 [X] 时终止,但它没有这样做。 这是我的代码:
#include <Windows.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){
switch (msg){
case WM_CLOSE:
PostQuitMessage(88);
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE pPrevInstance, LPSTR lpCmdLine, int cCmdShow) {
const auto pClassName = "M3D";
WNDCLASSEX wc = { 0 };
wc.cbSize = sizeof(wc);
wc.style = CS_OWNDC;
wc.lpfnWndProc = DefWindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = nullptr;
wc.hCursor = nullptr;
wc.hIcon = nullptr;
wc.hCursor = nullptr;
wc.hbrBackground = nullptr;
wc.lpszMenuName = nullptr;
wc.lpszClassName = pClassName;
RegisterClassEx(&wc);
HWND hWnd = CreateWindowEx(0, pClassName, "Fenster M3D", WS_CAPTION | WS_MAXIMIZEBOX | WS_SYSMENU, 200, 200, 640, 480, nullptr, nullptr, hInstance, nullptr);
//Fenster aufrufen
ShowWindow(hWnd, SW_SHOW);
//
//message Pumpe
MSG msg;
while (GetMessage(&msg,nullptr,0,0) > 0){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
谁能告诉我哪里出了问题以及为什么我的 PostQuitMessage() 无法正常工作? 我发现的其他线程并没有真正帮助
问题出在这个作业上:
wc.lpfnWndProc = DefWindowProc;
您告诉消息循环将消息直接发送到 DefWindowProc()
,因此您的自定义 WndProc()
永远不会被使用,所以 PostQuitMessage()
永远不会被调用。 DefWindowProc()
不会为其处理的任何消息调用 PostQuitMessage()
。
改为将该分配改为:
wc.lpfnWndProc = WndProc;
此外,根据 MSDN 上的 Closing the Window 文档,DefWindowProc()
在处理 WM_CLOSE
时调用 DestroyWindow()
,因此您应该调用 PostQuitMessage()
来回复 WM_DESTROY
改为:
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){
switch (msg){
case WM_DESTROY:
PostQuitMessage(88);
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}