1
votes

While trying to create an application that correctly supports per-monitor DPI-awareness version 2, I encountered an issue where my application's window's client area size was incorrect when started on a monitor, where DPI scaling was enabled.

I leave choosing the appropriate location for the window up to Windows, so I can't know on which monitor the window is going to be created, thus I'm also unable to know the DPI I should scale for before the creation of the window.
The solution to this is that I obtain the DPI for the monitor, once the window has been created, using GetDpiForWindow and set the size, so it matches the client area size I desire. In this case I want the client area to be scaled - for example 300x150 client area when on a 125% display should be 375x187.

The DPI is obtained correctly (120 in my case), but using SetWindowPos means that I have to account for the window borders, titlebar, etc. For that purpose I use AdjustWindowRectExForDpi, which accounts for the DPI scaling of the window borders.

To my surprise, the resulting client area size still is 300x150, when the application is started on a DPI-scaled monitor. Starting the application on a non-DPI-scaled monitor and then moving it to one that is, results in a correct client area size.

Minimal example:

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

    // Actually set the appropriate window size
    RECT scaled_size;
    scaled_size.left = 0;
    scaled_size.top = 0;
    scaled_size.right = 300;
    scaled_size.bottom = 150;

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

    if (!SetWindowPos(window, nullptr, 0, 0, scaled_size.right - scaled_size.left, scaled_size.bottom - scaled_size.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);
}

The example requires at least Windows 10 1607 to run and Windows SDK 14393 to compile.

How do I properly scale the client area size, when the application is started on a DPI-scaled monitor?

1

1 Answers

3
votes

AdjustWindowRectExForDpi doesn't adjust the client area size, though it does account for the increased size of window borders and the title bar, when the non-client area of a window is DPI-scaled. This means that you must scale the client area size yourself.

The example's code can be modified to scale the client area size manually like this:

// Calculate the scaling factor. 96 is the default Windows DPI, unless DPI scaling is enabled.
float scaling_factor = static_cast<float>(dpi) / 96;

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

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

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