question

AzeemCv-7038 avatar image
0 Votes"
AzeemCv-7038 asked XiaopoYang-MSFT commented

How to detect scale factor in Windows 10 in C++

I am trying to detect the display scale factor in Windows 10 in C++. Below is the code I am using.

 //Code to detect scale
 void getDisplayScale(double &h_Scale, double &v_Scale)
 {
     //auto activeWindow = GetActiveWindow();
     HWND activeWindow = GetDesktopWindow();
     HMONITOR monitor = MonitorFromWindow(activeWindow, MONITOR_DEFAULTTONEAREST);
    
     // Get the logical width and height of the monitor
     MONITORINFOEX monitorInfoEx;
     monitorInfoEx.cbSize = sizeof(monitorInfoEx);
     GetMonitorInfo(monitor, &monitorInfoEx);
     long cxLogical = monitorInfoEx.rcMonitor.right - monitorInfoEx.rcMonitor.left;
     long cyLogical = monitorInfoEx.rcMonitor.bottom - monitorInfoEx.rcMonitor.top;
    
     // Get the physical width and height of the monitor
     DEVMODE devMode;
     devMode.dmSize = sizeof(devMode);
     devMode.dmDriverExtra = 0;
     EnumDisplaySettings(monitorInfoEx.szDevice, ENUM_CURRENT_SETTINGS, &devMode);
     DWORD cxPhysical = devMode.dmPelsWidth;
     DWORD cyPhysical = devMode.dmPelsHeight;
    
     // Calculate the scaling factor
     h_Scale = ((double)cxPhysical / (double)cxLogical);
     v_Scale = ((double)cyPhysical / (double)cyLogical);
    
     // Round off to 2 decimal places
     h_Scale = round(h_Scale * 100.0) / 100.0;
     v_Scale = round(v_Scale * 100.0) / 100.0;
    
     std::cout << "Horizonzal scaling: " << h_Scale << "\n";
     std::cout << "Vertical scaling: " << v_Scale;
 }

It works properly as a standalone console project.

When I try to put in my MFC code, it returns 100% only. Even if the scale is 125%, it returns 100% only.

How to fix this?

Thanks



windows-apic++
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Castorix31 avatar image
1 Vote"
Castorix31 answered XiaopoYang-MSFT commented

An usual way is with GetDpiForWindow and a Manifest
(100% : 96, 125% : 120, 150% : 144, etc...)



· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It is not working for me. I use resolution, 1920x1080. For both 100% and 125% scale factor, it returns the same values. 100 and 120.

0 Votes 0 ·

What you mean by Manifest? How to add one?

0 Votes 0 ·

IIf you don"t use a Manifest, it is normal that it does not work

Add a Manifest file with

  <application xmlns="urn:schemas-microsoft-com:asm.v3">
     <windowsSettings>
       <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/PM</dpiAware>
       <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitor</dpiAwareness>
      </windowsSettings>
   </application>
1 Vote 1 ·

I am working on MFC based printer/fax driver. Where can I add this manifest file? and where? Project properties?

0 Votes 0 ·
Show more comments