Joh*_*ker 5 c++ windows winapi dpi
我正在创建一个程序,该程序可以使用SetWindowPos(). 我自己的程序是PROCESS_PER_MONITOR_DPI_AWARE. 其他程序可以是PROCESS_DPI_UNAWARE、PROCESS_SYSTEM_DPI_AWARE或中的任何程序PROCESS_PER_MONITOR_DPI_AWARE。
因为我自己的程序是PROCESS_PER_MONITOR_DPI_AWARE,所以我传递给的坐标SetWindowPos()是物理坐标。我现在想做的是将客户区域的大小调整为逻辑坐标中的特定大小。
我尝试做的是
screenDPI。windowDPI。scaleFactor为screenDPI / windowDPI.scaleFactor这在大多数情况下都有效,但是当我使用具有不同显示缩放比例的两个屏幕时,
PROCESS_SYSTEM_DPI_AWARE当窗口位于辅助屏幕上(辅助屏幕使用 96dpi,而主屏幕使用 120dpi)时,这对于使用 的应用程序会失败。这与窗口框架大小无关,我还不确定为什么它会失败,但是目标x和y坐标被放大,以便窗口移到屏幕外。screenDPI就不再正确了,对吗?我该如何处理这个案子?我知道还有这个功能AdjustWindowRectExForDpi,但不知何故我无法让它正常工作。dpi我应该传递给它什么价值?目标屏幕的dpi、目标窗口的dpi还是我自己程序的dpi?另外,该功能仅在Windows 10以上版本可用,那么在旧版Windows客户端上如何处理呢?
我希望得到一些帮助。谢谢!
我应该传递给它的 dpi 值是多少?目标屏幕的dpi、目标窗口的dpi还是我自己程序的dpi?
您需要从一个屏幕移动到下一个屏幕的窗口的 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);
float scaling_factor = static_cast<float>(dpi) / 96;
// Actually set the appropriate window size
RECT scale;
scale.left = 0;
scale.top = 0;
scale.right = static_cast<LONG>(300 * scaling_factor);
scale.bottom = static_cast<LONG>(150 * scaling_factor);
if (!AdjustWindowRectExForDpi(&scale, window_style, false, 0, dpi))
{
return 1;
}
if (!SetWindowPos(window, nullptr, 0, 0, scale.right - scale.left, scale.bottom - scale.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);
}
Run Code Online (Sandbox Code Playgroud)
窗口可以从一个屏幕移动到下一个屏幕,并成功重新计算窗口大小。
| 归档时间: |
|
| 查看次数: |
1997 次 |
| 最近记录: |