The recommended window size sent with the WM_DPICHANGED message is too big

Viewed 2217

My application supports per-monitor DPI-awareness version 2. I have two monitors - one scaled at 100% and the other at 125%. When moving my application's window to the monitor with DPI scaling and setting the new size using the recommended size given in the WM_DPICHANGED message, the resulting client area size is a few pixels bigger than it should be.

For example, the window's client area size in my case is 300x200 pixels. On the monitor with 125% scaling, the scaling factor is 1.25, so the resulting client area size should be 375x250. When I set the window size using the recommended one received in the WM_DPICHANGED message, the resulting client area size is 377x252. Windows documentation claims scaling to be linear, yet it fails to work like that.

Minimal example:

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

Error checking is removed for brevity.
The example requires Windows 10 SDK 14393+ to compile and Windows 10 1607+ to run.

How do I fix the incorrect recommended window size given in the WM_DPICHANGED message?

1 Answers
Related