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

我试图在 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",monitor_height,monitor_width) ;

  }

  MessageBox(0,"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>

有任何想法吗?

解决方法

在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!

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...