我试图在Windows C ++应用程序中获得真正的屏幕分辨率(以像素为单位)。当Windows dpi设置改变时,我得到虚拟(调整)分辨率而不是真实分辨率。我尝试使用SetProcessDPIAware,SetProcessDpiAwareness(所有三个枚举值作为参数)和清单中的真实设置。在所有三种情况下,代码在我的Windows 7 PC中工作正常(即显示真实分辨率)但在Win 10中没有(这里它忽略了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 ;
}
我尝试使用的清单如下:
<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>
有什么想法吗?
答案 0 :(得分:9)
我终于在Jonathan Potter和Barmak Shemirani的帮助下发现了正在发生的事情:Windows 10与先前版本的Windows不同,允许用户“动态”更改dpi设置,而无需注销然后再次登录我在win 10机器上运行测试,通常具有标准(100%)设置。所以我会将设置更改为150%,运行应用程序并得到错误的结果。
Jonathan和Barmak的回答表明,特定PC的设置中有一些东西,而不是程序或一般的胜利10,这导致了我的问题。所以我尝试了以下内容:
- changed DPI settings to 150%
- logged out
- logged in again
- ran the program
我得到了正确的结果(实际屏幕分辨率,与调整后的分辨率相比)。
因此,为了使SetProcessDPIAware(以及相关方法:SetProcessDpiAwareness()和manifest with true)正常工作,必须在更改DPI设置之后和运行程序之前注销并再次登录。
再次感谢Jonathan和Barmak!