我的应用程序支持每个显示器 DPI 感知版本 2。我有两台显示器 - 一台缩放为 100%,另一台缩放为 125%。当使用 DPI 缩放将应用程序的窗口移动到显示器并使用消息中给出的建议大小设置新大小时WM_DPICHANGED
,生成的客户端区域大小比应有的大小大了几个像素。
例如,在我的例子中,窗口的客户区域大小为 300x200 像素。在缩放率为 125% 的显示器上,缩放系数为 1.25,因此生成的客户区域大小应为 375x250。当我使用消息中收到的推荐窗口大小设置窗口大小时WM_DPICHANGED
,结果客户区大小为 377x252。Windows文档声称缩放是线性的,但它并不能像那样工作。
最小的例子:
#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);
}
Run Code Online (Sandbox Code Playgroud)
为了简洁起见,删除了错误检查。
该示例需要 Windows 10 SDK 14393+ 才能编译,并需要 Windows 10 1607+ 才能运行。
如何修复消息中给出的不正确的建议窗口大小WM_DPICHANGED
?
该问题的发生是由于 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;
}
Run Code Online (Sandbox Code Playgroud)
请注意,使用此方法时,您必须使window_width
和window_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;
}
Run Code Online (Sandbox Code Playgroud)
另外值得注意的是,似乎还有另一个错误,如果您在上述任一消息中设置断点,则传递到的推荐矩形WM_DPICHANGED
将不正确。