我正在尝试在 Windows C++ 应用程序中获取真实的屏幕分辨率(以像素为单位)。当更改 Windows dpi 设置时,我得到虚拟(调整后)分辨率而不是真实分辨率。我尝试在 list 中使用 SetProcessDPIAware、SetProcessDpiAwareness(将所有三个枚举值作为参数)和 true 设置。在这三种情况下,代码在我的 Windows 7 PC 中运行良好(即显示真实分辨率),但在 Win 10 PC 中却不行(这里它忽略 DPI Aware 设置并返回调整后的分辨率)。
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h>
#include <winuser.h>
#include <VersionHelpers.h>
#include <ShellScalingAPI.h>
#include <stdlib.h>
#include <stdio.h>
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
char *cBuffer2 ;
cBuffer2 = (char *)malloc(3000) ;
if (IsWindowsVistaOrGreater())
{
// SetProcessDpiAwareness(PROCESS_SYSTEM_DPI_AWARE);
int result = SetProcessDPIAware();
sprintf(cBuffer2,"SetProcessDPIAware() result: [%i]\n",result) ;
int height = GetSystemMetrics(SM_CYSCREEN);
int width = GetSystemMetrics(SM_CXSCREEN);
sprintf(cBuffer2,"%s#1:\nHeight: [%i]\nwidth: [%i]\n",cBuffer2,height,width) ;
HWND hwnd = (HWND)atoi(lpCmdLine) ;
HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
MONITORINFO info;
info.cbSize = sizeof(MONITORINFO);
GetMonitorInfo(monitor, &info);
int monitor_width = info.rcMonitor.right - info.rcMonitor.left;
int monitor_height = info.rcMonitor.bottom - info.rcMonitor.top;
sprintf(cBuffer2,"%s#2:\nHeight: [%i]\nwidth: [%i]\n",cBuffer2,monitor_height,monitor_width) ;
}
MessageBox(0,cBuffer2,"SHOWRES.EXE",MB_OK) ;
return 0 ;
}
我尝试使用的 list 如下:
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" >
<asmv3:application>
<asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
<dpiAware>true</dpiAware>
</asmv3:windowsSettings>
</asmv3:application>
</assembly>
有什么想法吗?
最佳答案
在 Jonathan Potter 和 Barmak Shemirani 的帮助下,我终于发现了发生了什么:与以前版本的 Windows 不同,Windows 10 允许用户“即时”更改 dpi 设置,而无需注销并再次登录。我在通常具有标准 (100%) 设置的 win 10 机器上运行测试。所以我将设置更改为 150%,运行应用程序并得到错误的结果。
Jonathan 和 Barmak 的回答表明是在特定 PC 的设置中,而不是在程序或一般的 win 10 中,导致了我的问题。所以我尝试了以下方法:
- changed DPI settings to 150%
- logged out
- logged in again
- ran the program
我得到了正确的结果(实际屏幕分辨率与调整后的屏幕分辨率对比)。
因此看来,为了使 SetProcessDPIAware(以及相关方法:SetProcessDpiAwareness() 和 manifest with true)正常工作,必须在更改 DPI 设置之后和运行程序之前注销并再次登录。
再次感谢 Jonathan 和 Barmak!
关于c++ - SetProcessDPIAware 似乎在 Windows 10 下不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36134072/