如何将一个窗口保持在另一个应用程序窗口的前面

时间:2018-10-21 18:04:50

标签: c++ winapi gdi

我需要在所选应用程序的窗口顶部显示闪烁的边框(在我的示例中,应用程序为cmd.exe)。我为此使用了分层窗口。一切都正常,除了一件事:如果目标窗口(与我的情况相同,则为cmd.exe)与另一个窗口(如果另一个窗口位于前台)重叠,则无法将其放在前面。当我最大化/最小化目标窗口时,它起作用,但是当目标窗口重叠时,它不起作用。我无法在任务栏中单击应用程序的图标来恢复。

const COLORREF MY_COLOR_KEY = RGB(255, 128, 0);
HWND cmdHanlde = NULL;
constexpr unsigned int timerIdWindowUpdate = 1;
constexpr unsigned int timerIdFrameColor = 2;
bool tick = false;
bool minimized = false;

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
             _In_opt_ HINSTANCE hPrevInstance,
             _In_ LPWSTR    lpCmdLine,
             _In_ int       nCmdShow)
{
    WNDCLASSEX wc = {};
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpszClassName = L"MyTransparentFrame";
    wc.hCursor = ::LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = NULL;

    wc.lpfnWndProc = [](HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) -> LRESULT
    {
        switch (msg)
        {
        case WM_PAINT:
        {
            PAINTSTRUCT ps{};
            HDC hdc = BeginPaint(hwnd, &ps);

            RECT rc{}; GetClientRect(hwnd, &rc);
            HPEN hPen = CreatePen(PS_SOLID, 5, tick ? RGB(255, 128, 1) : RGB(255, 201, 14));
            HBRUSH hBrush = CreateSolidBrush(MY_COLOR_KEY);
            HGDIOBJ hOldPen = SelectObject(hdc, hPen);
            HGDIOBJ hOldBrush = SelectObject(hdc, hBrush);

            Rectangle(hdc, rc.left, rc.top, rc.right, rc.bottom);

            if (hOldPen)
                SelectObject(hdc, hOldPen);
            if (hOldBrush)
                SelectObject(hdc, hOldBrush);
            if (hPen)
                DeleteObject(hPen);
            if (hBrush)
                DeleteObject(hBrush);

            EndPaint(hwnd, &ps);
        }
        break;
        case WM_TIMER:
        {
            if (wp == timerIdWindowUpdate)
            {
                WINDOWPLACEMENT windowPlacement = { sizeof(WINDOWPLACEMENT), };
                if (::GetWindowPlacement(cmdHanlde, &windowPlacement))
                {
                    if (windowPlacement.showCmd == SW_SHOWMINIMIZED
                        || !IsWindowVisible(cmdHanlde))
                    {
                        minimized = true;
                    }
                    else
                    {
                        RECT rect = {};
                        DwmGetWindowAttribute(cmdHanlde, DWMWA_EXTENDED_FRAME_BOUNDS, &rect, sizeof(rect));
                        MONITORINFO monInfo;
                        monInfo.cbSize = sizeof(MONITORINFO);
                        GetMonitorInfoW(MonitorFromWindow(cmdHanlde, MONITOR_DEFAULTTONEAREST), &monInfo);
                        if (cmdHanlde != NULL && ::IsZoomed(cmdHanlde))
                        {
                            rect.left = monInfo.rcWork.left;
                            rect.top = monInfo.rcWork.top;
                            rect.bottom = monInfo.rcWork.bottom > rect.bottom ? rect.bottom : monInfo.rcWork.bottom;
                            rect.right = monInfo.rcWork.right > rect.right ? rect.right : monInfo.rcWork.right;
                        }
                        if (minimized)
                        {
                            ::SetWindowPos(hwnd, cmdHanlde, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
                            minimized = false;
                        }
                        else
                        {
                            ::SetWindowPos(cmdHanlde, hwnd, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
                            ::SetWindowPos(hwnd, 0, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
                        SWP_SHOWWINDOW);
                        }
                    }
                }
            }
            else if (wp == timerIdFrameColor)
            {
                tick = !tick;
                ::RedrawWindow(hwnd, NULL, NULL, RDW_INVALIDATE);
            }
            break;
        }
        case WM_DESTROY:
            PostQuitMessage(0);
            break;

        default:
            return DefWindowProcW(hwnd, msg, wp, lp);
        }

        return 0;
    };

    RegisterClassEx(&wc);

    HWND hwnd = CreateWindowExW(WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE |     WS_EX_LAYERED |     WS_EX_TRANSPARENT, wc.lpszClassName, L"", WS_POPUP | WS_VISIBLE | WS_DISABLED,
0, 0, 0, 0, nullptr, nullptr, nullptr, nullptr);
    ::SetTimer(hwnd, timerIdWindowUpdate, 50, NULL);
    ::SetTimer(hwnd, timerIdFrameColor, 500, NULL);
    SetLayeredWindowAttributes(hwnd, MY_COLOR_KEY, 255, LWA_COLORKEY);
    ShowWindow(hwnd, SW_SHOW);
    cmdHanlde = FindWindow(L"ConsoleWindowClass", L"C:\\WINDOWS\\system32\\cmd.exe");

    MSG msg;
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}

所以这里的问题是在目标窗口与另一个窗口重叠后如何恢复?

1 个答案:

答案 0 :(得分:3)

if (minimized) {
    ShowWindow(hwnd, SW_HIDE);
    minimized = false;
} else {
    SetWindowPos(hwnd, 0, rect.left, rect.top, rect.right - rect.left, 
                 rect.bottom - rect.top, SWP_NOZORDER);
    SetWindowPos(hwnd, GetNextWindow(cmdHandle, GW_HWNDPREV), 0, 0, 0, 0, 
                 SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE);
}