gpt4 book ai didi

c++ - 与 WM_DPICHANGED 消息一起发送的推荐窗口大小太大

转载 作者:搜寻专家 更新时间:2023-10-31 02:10:31 27 4
gpt4 key购买 nike

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

例如,在我的例子中窗口的客户区大小是 300x200 像素。在缩放比例为 125% 的显示器上,缩放系数为 1.25,因此生成的客户区大小应为 375x250。当我使用 WM_DPICHANGED 消息中收到的推荐大小设置窗口大小时,生成的客户区大小为 377x252。 window 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 消息中给出的错误推荐窗口大小?

最佳答案

出现此问题是由于 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将是不正确的。

关于c++ - 与 WM_DPICHANGED 消息一起发送的推荐窗口大小太大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45110655/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com