我知道在 Windows 通知消息中,WM_CLOSE 指的是通过窗口右上角的“X”按钮关闭窗口。
有谁知道用 File->Exit 关闭的通知消息?
我问的原因是因为我正在尝试实现 JNI native 代码以在用户启动系统关闭时优雅地关闭窗口。请参阅我之前的帖子 ( Winapi - SetWindowLongPtr in ShutdownBlockReasonCreate / Destroy implementation of JNI native code ) 了解背景。
单击“X”关闭时,会出现确认对话框,防止关闭原因消息消失(当我希望它在一段时间后消失时)。我知道 File->Exit from menu bar 不要求确认,但我如何使用 Windows 通知消息实现它?
在深入研究之后,我发现的唯一建议是使用 DestroyWindow。所以我尝试使用 DestroyWindow() 函数关闭窗口,但它只是“销毁”窗口,而不是结束整个应用程序。这是我的 WndProc CallBack 函数中的 switch 语句:
switch (message) {
case WM_QUERYENDSESSION:
PostMessage(hWnd, WM_CLOSE, 0, 0);
return 0;
case WM_ENDSESSION:
PostMessage(hWnd, WM_CLOSE, 0, 0);
return 0;
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_NCDESTROY:
RemoveWindowSubclass(hWnd, AppWndProc, uIdSubclass);
break;
}
如有任何帮助,我们将不胜感激!
干杯
最佳答案
I know with Windows notification message, WM_CLOSE refers to closing the window via "X" button on the top right hand corner of the window.
实际上,当窗口的标准“X”按钮被点击时(或者窗口左上角菜单上的标准“关闭”项被选中,如果启用的话,或者窗口接收到 ALT+F4 keystroke),WM_SYSCOMMAND
消息被发送到窗口,其中 wParam
包含 SC_CLOSE
标志。如果该消息被传递给 DefWindowProc()
(默认行为),它会向窗口发出一个 WM_CLOSE
消息。
参见 Closing the Window .
其他情况也可能导致发出 WM_CLOSE
消息。
Does anyone know the notification message for closing with File->Exit?
当选择该菜单项时会发生什么是由应用程序定义的,而不是操作系统。应用程序可以做任何它想做的事情,包括如果它想立即销毁窗口。
但是,话虽这么说,如果菜单是标准的 Win32 菜单,那么窗口将收到一条 WM_COMMAND
消息,其中至少包含所选菜单项的 ID。
The reason I asked is because I'm trying to implement JNI native code to gracefully close window when user initiated system shutdown.
默认情况下,您不需要为此做任何事情。操作系统在系统关闭期间自动关闭所有打开的窗口。如果您需要清理任何资源,而不是手动关闭窗口,您应该对关闭的窗口使用react。
When clicking on 'X' to close, confirmation dialog box comes up which prevents shutdown reason message from disappearing (when I expect it to disappear after a while).
然后应用程序没有正确处理系统关闭。
大多数应用程序都会在收到 WM_CLOSE
消息时显示这样的确认框。如果确认中止,应用程序将丢弃该消息并继续前进。但是,应用程序不应在系统关闭期间提示用户进行确认。但并非所有应用程序都遵循该规则。
I know File->Exit from menu bar doesn't ask for confirmation
同样,这是由应用程序决定的,而不是操作系统。
how do I implement this using windows notification message? After some digging around the only suggestions I found is to use DestroyWindow.
正确。或者,您也可以将 WM_QUIT
消息发布到消息队列。请参阅 PostQuitMessage()
函数。
So I tried closing the window using DestroyWindow() function, but it only "Destroys" the window, rather than ending the whole application.
应用程序有责任自行终止,通常是在主窗口被销毁时退出消息循环。
Here's my switch statement in my WndProc CallBack function:
无需发布 WM_CLOSE
以响应 WM_QUERYENDSESSION
或 WM_ENDSESSION
。让操作系统为您处理。
如果您不希望在系统关闭期间出现确认,请将您的代码更改为更像这样的内容:
bool shuttingDown = false;
LRESULT CALLBACK AppWndProc(
_In_ HWND hWnd,
_In_ UINT message,
_In_ WPARAM wParam,
_In_ LPARAM lParam,
_In_ UINT_PTR uIdSubclass,
_In_ DWORD_PTR dwRefData
) {
switch (message) {
case WM_QUERYENDSESSION:
shuttingDown = true;
break;
case WM_ENDSESSION:
if (wParam == FALSE)
shuttingDown = false;
break;
case WM_CLOSE:
if (shuttingDown) {
DestroyWindow(hWnd);
// or:
// PostQuitMessage(0);
return 0;
}
break;
case WM_NCDESTROY:
RemoveWindowSubclass(hWnd, AppWndProc, uIdSubclass);
break;
}
return DefSubclassProc(hWnd, message, wParam, lParam);
}
关于c++ - Winapi - WM_CLOSE 的替代品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59675754/