SetWindowPos() 跨进程 DPI 感知

SetWindowPos() cross-process DPI aware

我正在创建一个 moves/resizes windows 来自另一个进程 SetWindowPos() 的程序。我自己的程序是PROCESS_PER_MONITOR_DPI_AWARE。其他程序可以是 PROCESS_DPI_UNAWAREPROCESS_SYSTEM_DPI_AWAREPROCESS_PER_MONITOR_DPI_AWARE.

中的任何内容

因为我自己的程序是PROCESS_PER_MONITOR_DPI_AWARE,所以我传给SetWindowPos()的坐标是物理坐标。我现在要做的是 将客户区 调整为 逻辑坐标 .

中的特定大小

我尝试做的是

  1. 获取window所在显示器的DPI为screenDPI
  2. 获取目标window的DPI为windowDPI
  3. scaleFactor 设为 screenDPI / windowDPI
  4. 将所需的客户区大小缩放 scaleFactor
  5. 通过从 window 矩形大小中减去当前客户端矩形大小来计算 window 帧的额外大小。

这在大多数情况下都有效,但是当我使用两个显示比例不同的屏幕时,

我知道还有 AdjustWindowRectExForDpi 功能,但不知为何我无法让它正常工作。我应该传递给它的 dpi 值是多少?目标屏幕的dpi,目标window的dpi还是我自己程序的dpi?此外,此功能仅在 Windows 10 之后可用,所以我如何在较旧的 Windows 客户端上处理它?

我将不胜感激。谢谢!

What is the dpi value I am supposed to pass to it? The dpi of the target screen, the dpi of the target window or the dpi of my own program?

您需要从一个屏幕移动到下一个屏幕的 window 的 DPI。

代码示例:

#include <Windows.h>

LRESULT CALLBACK startup_window_procedure(HWND window, UINT message, WPARAM w_param, LPARAM l_param)
{
    switch (message)
    {
    case WM_DESTROY:
    {
        PostQuitMessage(0);
        return 0;
    }

    case WM_DPICHANGED:
    {
        // Resize the window
        RECT* new_rect = reinterpret_cast<RECT*>(l_param);

        if (!SetWindowPos(window, nullptr, new_rect->left, new_rect->top, new_rect->right - new_rect->left, new_rect->bottom - new_rect->top, SWP_NOZORDER | SWP_NOACTIVATE))
        {
            return 1;
        }

        return 0;
    }
    }

    return DefWindowProcW(window, message, w_param, l_param);
}

int CALLBACK wWinMain(HINSTANCE instance, HINSTANCE prev_instance, PWSTR cmd_line, int cmd_show)
{
    constexpr auto window_class_name = L"example_dialog";
    constexpr auto window_style = WS_OVERLAPPEDWINDOW;

    // Enable per-monitor DPI-awareness version 2
    if (!SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2))
    {
        return 1;
    }

    // Create the window
    WNDCLASSEXW window_class;
    window_class.cbSize = sizeof(window_class);
    window_class.style = CS_HREDRAW | CS_VREDRAW;
    window_class.lpfnWndProc = startup_window_procedure;
    window_class.cbClsExtra = 0;
    window_class.cbWndExtra = 0;
    window_class.hInstance = instance;
    window_class.hIcon = nullptr;
    window_class.hCursor = nullptr;
    window_class.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
    window_class.lpszMenuName = nullptr;
    window_class.lpszClassName = window_class_name;
    window_class.hIconSm = nullptr;

    if (!RegisterClassExW(&window_class))
    {
        return 1;
    }

    HWND window = CreateWindowExW(0, window_class_name, L"Example window", window_style, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, nullptr, nullptr, instance, nullptr);

    if (!window)
    {
        return 1;
    }

    UINT dpi = GetDpiForWindow(window);
    float scaling_factor = static_cast<float>(dpi) / 96;
    // Actually set the appropriate window size
    RECT scale;
    scale.left = 0;
    scale.top = 0;
    scale.right = static_cast<LONG>(300 * scaling_factor);
    scale.bottom = static_cast<LONG>(150 * scaling_factor);

    if (!AdjustWindowRectExForDpi(&scale, window_style, false, 0, dpi))
    {
        return 1;
    }

    if (!SetWindowPos(window, nullptr, 0, 0, scale.right - scale.left, scale.bottom - scale.top, SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOMOVE))
    {
        return 1;
    }

    ShowWindow(window, SW_SHOWNORMAL);

    // Message loop
    MSG message;
    int result;

    while ((result = GetMessageW(&message, nullptr, 0, 0)) != 0)
    {
        if (result == -1)
        {
            return 1;
        }
        else
        {
            TranslateMessage(&message);
            DispatchMessageW(&message);
        }
    }

    return static_cast<int>(message.wParam);
}

windows 可以从一个屏幕移动到下一个屏幕并成功重新计算 window 大小。