How could I detect when my application is minimized?

Andro Miguel M. Bondoc picture Andro Miguel M. Bondoc · Feb 11, 2011 · Viewed 8.6k times · Source

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?

Answer

Cody Gray picture Cody Gray · Feb 11, 2011

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;
}