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

后端 未结 1 413
终归单人心
终归单人心 2021-01-21 07:05

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

1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-21 07:44

    Chrome has an invisible window with the same name. The invisible window simply needs to be skipped. It wasn't much of a mystery in hindsight.

    void show(HWND hwnd)
    {
        //ShowWindow(hwnd, SW_NORMAL);
        //SetForegroundWindow(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;
    }
    

    I edited this a whole bunch of time. Previous edits are here: https://stackoverflow.com/posts/29837548/revisions

    0 讨论(0)
提交回复
热议问题