与WM_DPICHANGED消息一起发送的建议窗口大小太大



我的应用程序支持每显示器 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);
}

为简洁起见,删除了错误检查。
该示例需要 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;
}

请注意,使用此方法时,必须使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;
}

还值得注意的是,似乎还有另一个错误,如果您在上面显示的任何一条消息中出现断点,则传递给WM_DPICHANGED的建议矩形将不正确。

最新更新