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

发布于 2024-10-17 02:16:49 字数 113 浏览 2 评论 0原文

我有一个程序,其中有一个选项可以最小化任务栏的通知区域。为了使其发挥作用,我需要一种可靠的方法来检测用户何时最小化应用程序。

如何在 C++ 应用程序中使用 Windows API 来做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

葬﹪忆之殇 2024-10-24 02:16:49

当用户最小化窗口时(使用标题栏上的框,或从系统菜单中选择“最小化”选项),您的应用程序将收到一个 WM_SYSCOMMAND 消息。该消息的 wParam 参数将包含值 SC_MINIMIZE,该值指示所请求的系统命令的特定类型。在这种情况下,您不必关心lParam

因此,您需要设置一个消息映射来侦听 WM_SYSCOMMAND 消息,并将 wParam 设置为 SC_MINIMIZE。收到此类消息后,您应该执行代码以将应用程序最小化到任务栏通知区域,并返回 0(表示您已处理该消息)。

我不确定你使用的是什么 GUI 框架。对于不同的工具包,示例代码可能看起来非常不同。以下是您可以在直接 Win32 C 应用程序中使用的内容:

switch (message)
{
case WM_SYSCOMMAND:
    if ((wParam & 0xFFF0) == SC_MINIMIZE)
    {
        // shrink the application to the notification area
        // ...

        return 0;
    }
    break;
}

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.

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).

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:

switch (message)
{
case WM_SYSCOMMAND:
    if ((wParam & 0xFFF0) == SC_MINIMIZE)
    {
        // shrink the application to the notification area
        // ...

        return 0;
    }
    break;
}
迷离° 2024-10-24 02:16:49

我认为您正在寻找 WM_SIZE。当您收到此消息时,请检查 wParam 以获取详细信息。这是 MSDN 页面。

WM_SIZE

I think you are looking for WM_SIZE. When you get this, check the wParam to get the specifics. Here is the MSDN page.

WM_SIZE

旧情别恋 2024-10-24 02:16:49

您可以检查从 GetClientRect 返回的区域大小 - 如果为零,则最小化,对我有用,但可能不适用于所有情况。

You can check the area size returned from GetClientRect - if zero it's minimised, works for me but may not work in all cases.

与之呼应 2024-10-24 02:16:49

这就是 IsIconic应该确定,但它对我来说并不一致。 (哦,为了确定这一点的一致方法......)

That's what IsIconic is supposed to determine, but it doesn't work consistently for me. (Oh, for a consistent way to determine this...)

分分钟 2024-10-24 02:16:49

为了完整起见,还有 GetWindowPlacement。窗口状态显示在 WINDOWPLACEMENT 结构,如果窗口最小化,则其值为 2,即 SW_SHOWMINIMIZED

For completeness, there's also 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文