SetProcessDPIAware似乎在Windows 10下不起作用 [英] SetProcessDPIAware seems not to work under windows 10

查看:1580
本文介绍了SetProcessDPIAware似乎在Windows 10下不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Windows C ++应用程序中获得真实的屏幕分辨率(以像素为单位)。更改Windows dpi设置后,我得到的是虚拟(调整后)分辨率,而不是真实分辨率。我尝试过使用SetProcessDPIAware,SetProcessDpiAwareness(将所有三个枚举值作为参数)和清单中的真实设置。在这三种情况下,该代码都可以在我的Windows 7 PC上正常运行(即显示实际分辨率),但在Win 10上却无法运行(此处忽略DPI Aware设置并返回调整后的分辨率)。

I am trying to get the real screen resolution (in pixels) in a windows C++ app. When the windows dpi setting is changed, I get the virtual (adjusted) resolution instead of the real one. I have tried using SetProcessDPIAware, SetProcessDpiAwareness (with all three enumerated values as arguments) and a true setting in a manifest. In all three cases, the code works fine (i.e. shows the real resolution) in my windows 7 PC but not in a Win 10 one (here it ignores the DPI Aware setting and returns the adjusted resolution).

#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 ;
}

我尝试使用的清单如下:

The manifest I tried using is the following one:

<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设置,而无需需要注销并再次登录。我在通常具有标准(100%)设置的Win 10机器上运行测试。因此,我将设置更改为150%,运行该应用程序并得到了错误的结果。

I finally found out what is happening, with the help of Jonathan Potter and Barmak Shemirani: Windows 10, unlike previous versions of windows, allows the user to change the dpi settings 'on the fly', without the need to log out and login again. I was running the tests on a win 10 machine which normally has standard (100%) settings. So I would change the setting to 150%, run the app and get the wrong results.

Jonathan和Barmak的回答表明特定PC的设置中有某些内容,而不是在程序中或总体上赢得10,这就是造成我的问题的原因。因此,我尝试了以下操作:

Jonathan and Barmak's answers indicated that there is something in the specific PC's settings, not in the program or win 10 in general, that was causing my problems. So I tried the following:

- changed DPI settings to 150%
- logged out
- logged in again
- ran the program

我得到了正确的结果(实际屏幕分辨率,与调整后的结果相对应。)

And I got the correct results (real screen resolution, vs adjusted one).

因此,为了使SetProcessDPIAware(及相关方法:SetProcessDpiAwareness()并显示为true)正确运行,必须记录一下更改DPI设置之后,然后再运行程序,然后再次登录。

So it seems that in order for SetProcessDPIAware (and the related approaches: SetProcessDpiAwareness() and manifest with true) to work correctly, one has to log out and login again after changing the DPI setting and before running the program.

再次感谢Jonathan和Barmak!

Thanks again, Jonathan and Barmak!

这篇关于SetProcessDPIAware似乎在Windows 10下不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆