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?