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>
Any ideas?
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 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).
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.
Thanks again, Jonathan and Barmak!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With