Search code examples
windowswinapishowwindow

Get the last ShowWindow state for a window in winapi


When you click a window in your taskbar (Windows users) it will retain it's last state - maximised or normal scalable window. I'm trying to do a similar thing, but programatically and without the window gaining focus (eg. becoming foreground and disturbing my current activity in another window).

Can I do that? Current window state can be obtained using this API call:

   //Empty Window placement structure
   WinDefExt.WINDOWPLACEMENT placement = new WinDefExt.WINDOWPLACEMENT();
   //winapi call to external User32.dll file
   UserExt.GetWindowPlacement(hwnd, placement);
   //showCmd should be equal to one of the SW_ constants (here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms633548%28v=vs.85%29.aspx)
   placement.showCmd;

Solution

  • ShowWindow isn't a "state", it's an "action". There's no GetShowState command. You can infer a value from the current state of the window, but there's no way to find out the actual last value used with ShowWindow.

    if (!IsWindowVisible(hWnd))
        swState = SW_HIDE;
    else
    if (IsIconic(hWnd))
        swState = SW_MINIMIZE;
    else
    if (IsZoomed(hWnd))
        swState = SW_MAXIMIZE;
    else
    {
        // not hidden, minimized or zoomed, so we are a normal visible window
        // last ShowWindow flag could have been SW_RESTORE, SW_SHOW, SW_SHOWNA, etc
        // no way to tell
        swState = SW_SHOW;
    }