Home > Enterprise >  Win32 Get unscaled Virtual Desktop size in C
Win32 Get unscaled Virtual Desktop size in C

Time:05-10

Working on a C application, trying to map the mouse from the coordinates to the full screen windows.

Getting the mouse coordinates with GetPhysicalCursorPos(&mouse_point); which returns the coordinates relative to the origin 0,0. This is causing issues because the mouse is going "out of bounds" of the monitors because windows is scaling them down in size. E.g. it returns {3839, 1079} when it's at the bottom right corner, but Windows computes the total desktop size as 3200x1080. (this example two 1080p monitors, side by side, left has 100% scaling, right has 150% scaling)

Retrieving the virtual desktop size with:

unsigned int vScreenWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN);
unsigned int vScreenHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN);

But this is returning a desktop where the monitors with a higher DPI are smaller so for a dual 1080p monitor config of 3840x1080, it returns 3200x1080 (configuration above).

I've tried using GetSystemMetricsForDpi with 96 DPI (100%) but it returns the same as above:

unsigned int vScreenWidth = GetSystemMetricsForDpi(SM_CXVIRTUALSCREEN, 96 /* 100% scaling*/);
unsigned int vScreenHeight = GetSystemMetricsForDpi(SM_CYVIRTUALSCREEN, 96 /* 100% scaling*/);

The above also returns 3200x1080 in my mixed scaling example above, despite explicitly telling it to force use 100% scaling.

Is there any other way to get the un-scaled virtual desktop size?

Note the application is NOT DPI aware.

CodePudding user response:

Thanks to @RaymondChen I was able to fix it by calling SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); prior to GetSystemMetricsForDpi.

It now returns the full virtual desktop size.

  • Related