SetWindowPos() 跨进程 DPI 感知

2024-01-05

我正在创建一个程序,可以从另一个进程移动/调整窗口大小SetWindowPos()。我自己的程序是PROCESS_PER_MONITOR_DPI_AWARE。其他程序可以是以下任何内容PROCESS_DPI_UNAWARE, PROCESS_SYSTEM_DPI_AWARE or PROCESS_PER_MONITOR_DPI_AWARE.

因为我自己的程序是PROCESS_PER_MONITOR_DPI_AWARE,我传递给的坐标SetWindowPos()是在物理坐标中。我现在想做的是调整客户区大小到特定尺寸逻辑坐标.

我尝试做的是

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

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

  • 如果我将窗口从一个屏幕移动到下一个屏幕,则窗口框架尺寸的计算将关闭。
  • 对于使用以下内容的应用程序来说,这会失败PROCESS_SYSTEM_DPI_AWARE,当窗口位于辅助屏幕上时(辅助屏幕使用 96dpi,而主屏幕使用 120dpi)。这与窗框尺寸无关,我还不确定它到底为什么失败,但目标x and y坐标被放大,以便窗口移出屏幕。
  • 如果由于调整大小,窗口中心改变了屏幕,会发生什么情况?然后screenDPI将不再正确,对吗?我该如何处理这个案子?

我知道还有这个功能AdjustWindowRectExForDpi,但不知何故我无法让它正常工作。是什么dpi我应该传递给它的值?目标屏幕的dpi、目标窗口的dpi还是我自己程序的dpi?另外,该功能仅在Windows 10以上版本可用,那么在旧版Windows客户端上如何处理呢?

我希望得到一些帮助。谢谢!


我应该传递给它的 dpi 值是多少?目标屏幕的dpi、目标窗口的dpi还是我自己程序的dpi?

您需要从一个屏幕移动到下一个屏幕的窗口的 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);
}

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SetWindowPos() 跨进程 DPI 感知 的相关文章

随机推荐