简体   繁体   English

如何检测我的应用程序何时最小化?

[英]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 返回的区域大小 - 如果为零,则最小化,对我有用,但可能不适用于所有情况。

That's what IsIconic is supposed to determine, but it doesn't work consistently for me.这就是IsIconic应该确定的,但它对我来说并不一致。 (Oh, for a consistent way to determine this...) (哦,对于确定这一点的一致方法......)

I think you are looking for WM_SIZE.我认为您正在寻找 WM_SIZE。 When you get this, check the wParam to get the specifics.获得此信息后,请检查 wParam 以获取详细信息。 Here is the MSDN page.这是 MSDN 页面。

WM_SIZE WM_SIZE

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.

相关问题 为什么我的应用程序最小化时 CPU 使用率会增加? - Why does CPU usage increase when my application is minimized? 如何检测我的应用程序是作为服务运行还是在交互式会话中运行? - How do I detect that my application is running as service or in an interactive session? 我如何在C ++中的应用程序上共享“this”实例? - How could I share “this” instance over my application in C++? 如何检测我的应用程序在Qt中失去焦点? - How to detect that my application lost focus in Qt? 如何检测我的Windows应用程序是否已结束? - How to detect if my Windows application has ended? 如何检测HWND是否属于我的应用程序? - How to detect if a HWND belong to my application? 在qt中最小化时如何恢复到窗口模式 - How to restore to window mode when minimized in qt 最小化模式窗口时最小化所有应用程序窗口(在Linux上) - Minimize all application windows when a modal window gets minimized (on Linux) 如何链接我的c ++控制台应用程序以控制现有控制台? - How could I link my c++ console application to take control of an existing console? 如何使用带最小化主窗口的CreateProcess启动控制台应用程序 - How to launch console application using CreateProcess with Minimized main window
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM