How could I detect when my application is minimized?
Asked Answered
C

5

7

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?

Culp answered 11/2, 2011 at 6:23 Comment(4)
i would just like to ask if WM_Destroy is for exit what will minimize be?Culp
Do you want to make it so that your program minimizes itself to the Notification Area when the user clicks on the minimize button?Barry
yeah its what i want to know in C++Culp
I see that you recently asked a related question about minimizing an application to the notification area. Just in case you're still struggling with this, you may find this article useful. It looks like a complete implementation of what you want.Priscilapriscilla
P
7

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;
}
Priscilapriscilla answered 11/2, 2011 at 6:50 Comment(0)
S
0

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

Stow answered 11/2, 2011 at 6:30 Comment(3)
Wrong. You don't want to listen for WM_SIZE to detect when the application has been minimized.Priscilapriscilla
Out of curiosity, would it get both or is WM_SYSCOMMAND just the preferred way?Stow
Yeah, you would get it for both. Although your answer is somewhat incomplete there. You'd actually be looking for a wParam equal to SIZE_MINIMIZED. But the primary issue with handling WM_SIZE is that you can't override the default implementation. The message isn't sent until after the window has been minimized.Priscilapriscilla
H
0

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

Hersch answered 27/7, 2013 at 8:34 Comment(0)
O
0

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

Oriole answered 15/8, 2017 at 18:13 Comment(0)
L
0

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.

Lawford answered 21/7, 2020 at 10:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.