Kilo King
Kilo King

Reputation: 199

How can I restore a winapi window if it's minimized?

I have tried many functions, such as ShowWindow & IsWindowVisible to at least try to give the result if the window is minimized, let alone restore it. These functions constantly return false whether the window is minimized or not. I have also tried using GetWindowPlacementwith SetWindowPlacement with no success. My HWND finds Chrome with FindWindow(TEXT("Chrome_WidgetWin_1"), NULL); which is successful, but I want to test/restore the window if it's minimized and these past 10 hours has nothing to show for it.

Upvotes: 4

Views: 6187

Answers (1)

Barmak Shemirani
Barmak Shemirani

Reputation: 31599

Chrome has an invisible window with the same name. The invisible window simply needs to be skipped.

void show(HWND hwnd)
{
    //We can just call ShowWindow & SetForegroundWindow to bring hwnd to front. 
    //But that would also take maximized window out of maximized state. 
    //Using GetWindowPlacement preserves maximized state
    WINDOWPLACEMENT place;
    memset(&place, 0, sizeof(WINDOWPLACEMENT));
    place.length = sizeof(WINDOWPLACEMENT);
    GetWindowPlacement(hwnd, &place);

    switch (place.showCmd)
    {
    case SW_SHOWMAXIMIZED:
        ShowWindow(hwnd, SW_SHOWMAXIMIZED);
        break;
    case SW_SHOWMINIMIZED:
        ShowWindow(hwnd, SW_RESTORE);
        break;
    default:
        ShowWindow(hwnd, SW_NORMAL);
        break;
    }

    SetForegroundWindow(hwnd);
}

int WINAPI WinMain(HINSTANCE hinst, HINSTANCE, LPSTR cmdline, int nshow)
{
    const wchar_t *classname = L"Chrome_WidgetWin_1";

    HWND hwnd = NULL;
    for (;;)
    {
        hwnd = FindWindowEx(0, hwnd, classname, 0);
        if (!hwnd) break;

        //skip Chrome's invisible winodw
        if (IsWindowVisible(hwnd))
        {
            wchar_t buf[260];
            GetWindowText(hwnd, buf, 260);
            OutputDebugString(buf);
            OutputDebugString(L"\n");

            show(hwnd);
            break;
        }
    }

    return 0;
}

Upvotes: 11

Related Questions