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

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

本文介绍了如何将一个窗口保持在另一个应用程序窗口的前面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I needed to display flashing border in on top of chosen application's window (in my example the application is cmd.exe). I used layered windows for this purpose. Everything works fine, except the one thing: I can't bring target window (in my case - cmd.exe) in front if it is overlapped by another window (if another window is in the foreground). It works when I maximize/minimize target window, but doesn't work in case when target windows is overlapped. I can't restore in clicking on app's icon in the taskbar.

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

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

So the question here is how to restore target window after it was overlapped by another window?

推荐答案

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

这篇关于如何将一个窗口保持在另一个应用程序窗口的前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 02:22