c++ - 如果已最小化,如何恢复 winapi 窗口?

标签 c++ windows winapi

我已经尝试了很多函数,例如 ShowWindowIsWindowVisible 至少尝试在窗口最小化时给出结果,更不用说恢复它了。无论窗口是否最小化,这些函数都会不断返回 false。 我也尝试过将 GetWindowPlacementSetWindowPlacement 一起使用,但没有成功。 我的 HWND 使用 FindWindow(TEXT("Chrome_WidgetWin_1"), NULL); 找到 Chrome,这是成功的,但我想测试/恢复窗口,如果它被最小化并且这些过去10 个小时没有什么可证明的。

最佳答案

Chrome 有一个不可见的同名窗口。只需要跳过不可见的窗口。

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

关于c++ - 如果已最小化,如何恢复 winapi 窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29837268/

相关文章:

c++ - 从 C++ 中的函数重新调整多个 vector ?

c++ - 在非静态数据成员上使用 SFINAE?

Windows 从修改电源条件的命令提示符创建任务计划

node.js - 在 Windows 7 中打开太多文件(EMFILE 错误)

c# - 获取独占进程句柄

c++ - C++ 新手的类成员函数

c++ - CodeChef 的子序列等式

linux - 部署工具后Matlab UI程序运行不正常

c++ - 使用 CreateFont() 创建时,Tahoma(MS Shell Dlg 2)字体在 Win7 中看起来被压扁了

c++ - CreateFile 和长设备名