首页 > 解决方案 > What message is sent when you minimize a window?

问题描述

I'm catching WindowProc and looking for WM_SYSCOMMAND / SC_MINIMIZE, but it only fires the message when I minimize the window by clicking in its taskbar icon.

When I click in the window's minimize button the message isn't fired.

switch (Msg)
{
case WM_SYSCOMMAND:
    switch (wParam)
    {
    case SC_MINIMIZE:
        {
            OutputDebugStringW(L"Window minimized!");
            //return TRUE;
        }
    }
    break;
}

I noticed that my window was not sending the WM_SIZE message, then while searching i have found this:

By default, the DefWindowProc function sends the WM_SIZE and WM_MOVE messages to the window. The WM_SIZE and WM_MOVE messages are not sent if an application handles the WM_WINDOWPOSCHANGED message without calling DefWindowProc. It is more efficient to perform any move or size change processing during the WM_WINDOWPOSCHANGED message without calling DefWindowProc.

I confirmed that its sending WM_WINDOWPOSCHANGED when i try to minimize by clicking in the minimize button.

    case WM_WINDOWPOSCHANGING:

        tagWINDOWPOS * wp    = reinterpret_cast<tagWINDOWPOS *>(lParam);
        wp->x = ...;
        wp->y = ...;
        wp->cx = ...;
        wp->cy = ...;

        LPARAM lp    = reinterpret_cast<LPARAM>(wp);

I have tried to set x, y, cx, cy values to the current ones, but the window still got minimized.

标签: c++windowswinapi

解决方案


当窗口大小改变时,WM_SIZE消息被发送到窗口;如果该大小更改是为了最小化窗口,那么wParam参数将为SIZE_MINIMIZED. (这WM_SIZE也将在WM_SYSCOMMAND/SC_MINIMIZE组合之后发送,因此您可以检测到最小化操作,无论是什么用户操作触发了它。)

因此,您的代码WndProc将遵循以下几行:

    switch (Msg) {
        case WM_SIZE:
            switch (wParam) {
                case SIZE_MINIMIZED:
                    OutputDebugStringW(L"Window minimized!");
                //  return TRUE;
                    break;
                //...
            }
            break;

            //...
    }

推荐阅读