[英]How could I detect when my application is minimized?
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 做到这一点?
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 .当用户最小化窗口时(使用标题栏上的框,或通过从系统菜单中选择“最小化”选项),您的应用程序将收到
WM_SYSCOMMAND
消息。 The wParam
parameter of that message will contain the value SC_MINIMIZE
, which indicates the particular type of system command that is being requested.该消息的
wParam
参数将包含值SC_MINIMIZE
,它指示正在请求的系统命令的特定类型。 In this case, you don't care about the lParam
.在这种情况下,您不关心
lParam
。
So you need to set up a message map that listens for a WM_SYSCOMMAND
message with the wParam
set to SC_MINIMIZE
.因此,您需要设置一个消息映射来侦听
WM_SYSCOMMAND
消息,并将wParam
设置为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).收到此类消息后,您应该执行代码以将应用程序最小化到任务栏通知区域,并返回 0(表示您已处理该消息)。
I'm not sure what GUI framework you're using.我不确定您使用的是什么 GUI 框架。 The sample code could potentially look very different for different toolkits.
对于不同的工具包,示例代码可能看起来非常不同。 Here's what you might use in a straight Win32 C application:
以下是您可能在直接的 Win32 C 应用程序中使用的内容:
switch (message)
{
case WM_SYSCOMMAND:
if ((wParam & 0xFFF0) == SC_MINIMIZE)
{
// shrink the application to the notification area
// ...
return 0;
}
break;
}
您可以检查从 GetClientRect 返回的区域大小 - 如果为零,则最小化,对我有用,但可能不适用于所有情况。
For completeness, there's also GetWindowPlacement .为了完整起见,还有GetWindowPlacement 。 The window state is revealed in the
showCmd
member of the WINDOWPLACEMENT structure, and if the window is minimized it has a value of 2, or SW_SHOWMINIMIZED .窗口状态在WINDOWPLACEMENT结构的
showCmd
成员中显示,如果窗口被最小化,则它的值为 2 或SW_SHOWMINIMIZED 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.