3

I have tried three ways to obtain the screen size on Windows 10 with my 4k display (which is 3840x2160).

Based on SO answers I have tried the code from:

  1. https://stackoverflow.com/a/50205813/356726
  2. https://stackoverflow.com/a/10845821/356726
  3. How to get screen resolution in C++?

For convenience I will copy the three parts at the bottom. However, in all 3 cases the values are always 1536/864. So how can I get my physical screen resolution?

How to get screen resolution in C++?

RECT desktop;
// Get a handle to the desktop window
const HWND hDesktop = GetDesktopWindow();
// Get the size of screen to the variable desktop
GetWindowRect(hDesktop, &desktop);
// The top left corner will have coordinates (0,0)
// and the bottom right corner will have coordinates
// (horizontal, vertical)
const int horizontal = desktop.right;
const int vertical   = desktop.bottom;
return QSize(vertical, horizontal);

How to get the Monitor Screen Resolution from a hWnd?

const HWND hDesktop = GetDesktopWindow();
HMONITOR monitor = MonitorFromWindow(hDesktop, MONITOR_DEFAULTTONEAREST);
MONITORINFO info;
info.cbSize = sizeof(MONITORINFO);
GetMonitorInfo(monitor, &info);
const int monitor_width  = info.rcMonitor.right  - info.rcMonitor.left;
const int monitor_height = info.rcMonitor.bottom - info.rcMonitor.top;
return QSize(monitor_height, monitor_width);

Portable way in C++ to get desktop resolution

const int width  = static_cast<int>(GetSystemMetrics(SM_CXSCREEN));
const int height = static_cast<int>(GetSystemMetrics(SM_CYSCREEN));
return QSize(width, height);

--- Edit ----

Good point with the scaling, indeed I use 250%. So obviously I have to multiply by that if there is no other function.

Scaling

--- More findings ---

At my place it also depends when/where I query the resolution. In my very case, if I run the check as very first line, I get the 1536/864 values. Calling it later after my first windows are open, I get the 3840x2160 pair (i.e. the correct ones).

For some odd reasons I obtain SCALE_180_PERCENT (0n180) as value from

DEVICE_SCALE_FACTOR pScale;
GetScaleFactorForMonitor(monitor, &pScale);

while my setting is 250%.

Horst Walter
  • 13,663
  • 32
  • 126
  • 228
  • 3
    Looks like you have 250% scaling turned on. – user4581301 Feb 07 '20 at 19:00
  • You've copied all the way to include the `return` - but you omitted the actual signatures. – Ted Lyngmo Feb 07 '20 at 19:00
  • 2
    Note that getting screen resolution (and dimensions / other calibration data) is *not* reliable. Many manufacturers don't provide correct information and Windows just trusts what the hardware says - which may be garbage). – Jesper Juhl Feb 07 '20 at 19:07
  • _"the values are always 1536/864"_ this seems pretty even to me :D – YSC Feb 07 '20 at 19:17
  • 2
    [Setting the default DPI awareness for a process](https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/mt846517(v=vs.85)) "These modes enable different DPI scaling behavior and can use different coordinate spaces." – Roman R. Feb 07 '20 at 19:49
  • @YSC that's why I always avoid using word "odd" in non-mathematical way when talking to fellow smart-ass ptogrammers – Swift - Friday Pie Feb 07 '20 at 23:40
  • I as a non-native speaker have crosschecked, https://english.stackexchange.com/a/8323 (so odd appears to be neutral). But of course, that is really off topic now. I meant to say I do get unexpected values, no offence. – Horst Walter Feb 09 '20 at 14:32

0 Answers0