使用WM_DPICHANGED消息发送的建议窗口大小太大

时间:2017-07-14 19:52:54

标签: c++ winapi dpi

我的应用程序支持每个监视器DPI感知版本2.我有两个监视器 - 一个缩放为100%,另一个缩放为125%。将我的应用程序窗口移动到具有DPI缩放的监视器并使用WM_DPICHANGED消息中给出的建议大小设置新大小时,生成的客户端区域大小比它应该大几个像素。

例如,在我的情况下,窗口的客户区大小为300x200像素。在具有125%缩放比例的监视器上,缩放因子为1.25,因此生成的客户端区域大小应为375x250。当我使用WM_DPICHANGED消息中收到的推荐窗口大小设置窗口大小时,生成的客户端区域大小为377x252。 Windows documentation claims缩放为线性,但它无法像那样工作。

最小例子:

#include <Windows.h>

void set_window_size(HWND window, DWORD window_style, int width, int height)
{
    UINT dpi = GetDpiForWindow(window);
    float scaling_factor = static_cast<float>(dpi) / USER_DEFAULT_SCREEN_DPI;

    RECT scaled_size;
    scaled_size.left = 0;
    scaled_size.top = 0;
    scaled_size.right = static_cast<LONG>(width * scaling_factor);
    scaled_size.bottom = static_cast<LONG>(height * scaling_factor);

    // Adjust the size to account for non-client area
    AdjustWindowRectExForDpi(&scaled_size, window_style, false, 0, dpi);

    SetWindowPos(window, nullptr, 0, 0, scaled_size.right - scaled_size.left, scaled_size.bottom - scaled_size.top, SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOMOVE));
}

// These sizes are for the client area
constexpr auto window_width = 300;
constexpr auto window_height = 200;

constexpr auto window_class_name = L"startup_dialog";
constexpr auto window_style = WS_OVERLAPPEDWINDOW;

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

        case WM_DPICHANGED:
        {
            RECT* rect = reinterpret_cast<RECT*>(l_param);
            SetWindowPos(window, nullptr, rect->left, rect->top, rect->right - rect->left, rect->bottom - rect->top, SWP_NOZORDER | SWP_NOACTIVATE);
        }
    }

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

int CALLBACK wWinMain(HINSTANCE instance, HINSTANCE prev_instance, PWSTR cmd_line, int cmd_show)
{
    SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);

    WNDCLASSEXW window_class;
    window_class.cbSize = sizeof(window_class);
    window_class.style = CS_HREDRAW | CS_VREDRAW;
    window_class.lpfnWndProc = 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;

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

    // Set the initial DPI-scaled window size
    set_window_size(window, window_style, window_width, window_height);

    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 10 SDK 14393+进行编译,并运行Windows 10 1607+。

如何修复WM_DPICHANGED消息中提供的错误推荐窗口大小?

1 个答案:

答案 0 :(得分:2)

由于Windows错误导致问题,导致错误地计算新窗口大小。可以通过处理WM_GETDPISCALEDSIZE消息并自己计算新窗口大小来解决该错误。

根据问题的示例处理邮件的示例:

case WM_GETDPISCALEDSIZE:
{
    UINT dpi = static_cast<UINT>(w_param);
    float scaling_factor = static_cast<float>(dpi) / USER_DEFAULT_SCREEN_DPI;

    RECT client_area;
    client_area.right *= scaling_factor;
    client_area.bottom *= scaling_factor;

    RECT window_rectangle;
    window_rectangle.left = 0;
    window_rectangle.top = 0;
    window_rectangle.right = static_cast<LONG>(window_width * scaling_factor);
    window_rectangle.bottom = static_cast<LONG>(window_height * scaling_factor);

    if (!AdjustWindowRectExForDpi(&window_rectangle, window_style, false, 0, dpi))
    {
        // Error handling
        return 0;
    }

    SIZE* new_size = reinterpret_cast<SIZE*>(l_param);
    new_size->cx = window_rectangle.right - window_rectangle.left;
    new_size->cy = window_rectangle.bottom - window_rectangle.top;

    return 1;
}

请注意,使用此方法,您必须在邮件中提供window_widthwindow_height变量。在问题的例子中,这是使用constexpr全局变量完成的。

替代方法,根据以前的客户区域大小进行扩展,但可能稍微慢一些:

case WM_GETDPISCALEDSIZE:
{
    UINT dpi = static_cast<UINT>(w_param);
    float scaling_factor = static_cast<float>(dpi) / USER_DEFAULT_SCREEN_DPI;

    RECT client_area;

    if (!GetClientRect(window, &client_area))
    {
        // Error handling
        return 0;
    }

    client_area.right = static_cast<LONG>(client_area.right * scaling_factor);
    client_area.bottom = static_cast<LONG>(client_area.bottom * scaling_factor);

    if (!AdjustWindowRectExForDpi(&client_area, window_style, false, 0, dpi))
    {
        // Error handling
        return 0;
    }

    SIZE* new_size = reinterpret_cast<SIZE*>(l_param);
    new_size->cx = client_area.right - client_area.left;
    new_size->cy = client_area.bottom - client_area.top;

    return 1;
}

另外值得注意的是,似乎还有另一个错误,如果您在上述任一消息中断点,则传递给WM_DPICHANGED的推荐矩形将不正确。