windows 如何检测我的应用程序何时最小化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4966194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How could I detect when my application is minimized?
提问by Andro Miguel M. Bondoc
I have a program with an option to enable minimizing to the taskbar's notification area. In order for this to work, I need a reliable way of detecting when the user has minimized the application.
我有一个程序,可以选择最小化任务栏的通知区域。为了让它起作用,我需要一种可靠的方法来检测用户何时最小化应用程序。
How can I do that using the Windows API in a C++ application?
如何在 C++ 应用程序中使用 Windows API 做到这一点?
回答by Cody Gray
When the user minimizes the window (either using the box on the title bar, or by selecting the "Minimize" option from the system menu), your application will receive a WM_SYSCOMMAND
message. The wParam
parameter of that message will contain the value SC_MINIMIZE
, which indicates the particular type of system command that is being requested. In this case, you don't care about the lParam
.
当用户最小化窗口时(使用标题栏上的框,或通过从系统菜单中选择“最小化”选项),您的应用程序将收到一条WM_SYSCOMMAND
消息。该wParam
消息的参数将包含值SC_MINIMIZE
,该值指示正在请求的系统命令的特定类型。在这种情况下,您不关心lParam
.
So you need to set up a message map that listens for a WM_SYSCOMMAND
message with the wParam
set to SC_MINIMIZE
. Upon receipt of such a message, you should execute your code to minimize your application to the taskbar notification area, and return 0 (indicating that you've processed the message).
所以,你需要建立一个消息映射侦听WM_SYSCOMMAND
与消息wParam
设置为SC_MINIMIZE
。收到此类消息后,您应该执行代码以将您的应用程序最小化到任务栏通知区域,并返回 0(表示您已处理该消息)。
I'm not sure what GUI framework you're using. The sample code could potentially look very different for different toolkits. Here's what you might use in a straight Win32 C application:
我不确定您使用的是什么 GUI 框架。对于不同的工具包,示例代码可能看起来非常不同。以下是您可能在直接的 Win32 C 应用程序中使用的内容:
switch (message)
{
case WM_SYSCOMMAND:
if ((wParam & 0xFFF0) == SC_MINIMIZE)
{
// shrink the application to the notification area
// ...
return 0;
}
break;
}
回答by kailoon
回答by Stef
You can check the area size returned from GetClientRect - if zero it's minimised, works for me but may not work in all cases.
您可以检查从 GetClientRect 返回的区域大小 - 如果为零,则最小化,对我有用,但可能不适用于所有情况。