gpt4 book ai didi

c++ - AdjustWindowRectExForDpi 似乎没有调整客户区域大小

转载 作者:行者123 更新时间:2023-11-30 02:24:55 25 4
gpt4 key购买 nike

在尝试创建正确支持每个显示器 DPI 感知版本 2 的应用程序时,我遇到了一个问题:在启用了 DPI 缩放的显示器上启动时,我的应用程序窗口的客户区大小不正确。

我选择为窗口选择适当的位置,直到Windows,所以我不知道将在哪个显示器上创建窗口,因此我也无法知道创建之前应该缩放的DPI window 的。
解决方案是,在创建窗口后,我使用 GetDpiForWindow 获取显示器的 DPI。并设置大小,使其符合我想要的客户区域大小。在本例中,我希望缩放客户区域 - 例如,在 125% 显示屏上的 300x150 客户区域应为 375x187。

DPI 已正确获取(在我的例子中为 120),但使用 SetWindowPos意味着我必须考虑窗口边框、标题栏等。为此,我使用 AdjustWindowRectExForDpi ,它考虑了窗口边框的 DPI 缩放。

令我惊讶的是,当应用程序在 DPI 缩放的显示器上启动时,最终的客户端区域大小仍然是 300x150。在非 DPI 缩放的显示器上启动应用程序,然后将其移动到一个会产生正确的客户区域大小的显示器。

最小示例:

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

该示例至少需要 Windows 10 1607 才能运行,并需要 Windows SDK 14393 才能编译。

当应用程序在 DPI 缩放的显示器上启动时,如何正确缩放客户区域大小?

最佳答案

AdjustWindowRectExForDpi不调整客户区域大小,但当窗口的非客户区域按 DPI 缩放时,它确实会增加窗口边框和标题栏的尺寸。这意味着您必须自行缩放客户区域大小。

可以修改示例的代码以手动缩放客户区域大小,如下所示:

// 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;
}

关于c++ - AdjustWindowRectExForDpi 似乎没有调整客户区域大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45012325/

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