c – SetProcessDPIAware似乎无法在Windows 10下运行

前端之家收集整理的这篇文章主要介绍了c – SetProcessDPIAware似乎无法在Windows 10下运行前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在 @L_403_0@ C应用程序中获得真正的屏幕分辨率(以像素为单位).当Windows dpi设置改变时,我得到虚拟(调整)分辨率而不是真实分辨率.我尝试使用SetProcessDPIAware,SetProcessDpiAwareness(所有三个枚举值作为参数)和清单中的真实设置.在所有三种情况下,代码在我的Windows 7 PC中工作正常(即显示真实分辨率)但在Win 10中没有(这里它忽略了DPI Aware设置并返回调整后的分辨率).
  1. #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
  2.  
  3. // Windows Header Files:
  4. #include <windows.h>
  5. #include <winuser.h>
  6. #include <VersionHelpers.h>
  7. #include <ShellScalingAPI.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10.  
  11. int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
  12. {
  13. char *cBuffer2 ;
  14.  
  15. cBuffer2 = (char *)malloc(3000) ;
  16. if (IsWindowsVistaOrGreater())
  17. {
  18. // SetProcessDpiAwareness(PROCESS_SYSTEM_DPI_AWARE);
  19. int result = SetProcessDPIAware();
  20.  
  21. sprintf(cBuffer2,"SetProcessDPIAware() result: [%i]\n",result) ;
  22.  
  23. int height = GetSystemMetrics(SM_CYSCREEN);
  24. int width = GetSystemMetrics(SM_CXSCREEN);
  25. sprintf(cBuffer2,"%s#1:\nHeight: [%i]\nwidth: [%i]\n",cBuffer2,height,width) ;
  26.  
  27.  
  28. HWND hwnd = (HWND)atoi(lpCmdLine) ;
  29. HMONITOR monitor = MonitorFromWindow(hwnd,MONITOR_DEFAULTTONEAREST);
  30. MONITORINFO info;
  31. info.cbSize = sizeof(MONITORINFO);
  32. GetMonitorInfo(monitor,&info);
  33. int monitor_width = info.rcMonitor.right - info.rcMonitor.left;
  34. int monitor_height = info.rcMonitor.bottom - info.rcMonitor.top;
  35. sprintf(cBuffer2,"%s#2:\nHeight: [%i]\nwidth: [%i]\n",monitor_height,monitor_width) ;
  36.  
  37. }
  38.  
  39. MessageBox(0,"SHOWRES.EXE",MB_OK) ;
  40. return 0 ;
  41. }

我尝试使用的清单如下:

  1. <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" >
  2. <asmv3:application>
  3. <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
  4. <dpiAware>true</dpiAware>
  5. </asmv3:windowsSettings>
  6. </asmv3:application>
  7. </assembly>

有任何想法吗?

解决方法

在Jonathan Potter和Barmak Shemirani的帮助下,我终于发现了正在发生的事情:Windows 10与先前版本的Windows不同,允许用户“即时”更改dpi设置,而无需注销并再次登录.我在win 10机器上运行测试,通常具有标准(100%)设置.所以我会将设置更改为150%,运行应用程序并得到错误的结果.

Jonathan和Barmak的回答表明,特定PC的设置中有一些东西,而不是程序或一般的胜利10,这导致了我的问题.所以我尝试了以下方法

  1. - changed DPI settings to 150%
  2. - logged out
  3. - logged in again
  4. - ran the program

我得到了正确的结果(真正的屏幕分辨率,与调整后的一个).

因此,为了使SetProcessDPIAware(以及相关方法:SetProcessDpiAwareness()和manifest with true)正常工作,必须在更改DPI设置之后和运行程序之前注销并再次登录.

再次感谢Jonathan和Barmak!

猜你在找的C&C++相关文章