Skip to content

Commit 0fd6fd1

Browse files
committed
(#136) Limit calls to SetThreadDpiAwarenessContext to Windows 10 clients (#137)
1 parent 52d9097 commit 0fd6fd1

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/win32/screengrab.c

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
11
#include "../screengrab.h"
22
#include "../endian.h"
33
#include <stdlib.h> /* malloc() */
4+
#include <VersionHelpers.h>
45

56
MMRect getScaledRect(MMRect input)
67
{
7-
// Configure DPI awareness to fetch unscaled display size
8-
DPI_AWARENESS_CONTEXT initialDpiAwareness = SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
98
size_t scaledDesktopWidth = (size_t)GetSystemMetrics(SM_CXSCREEN);
109
size_t scaledDesktopHeight = (size_t)GetSystemMetrics(SM_CYSCREEN);
11-
// Reset DPI awareness to avoid inconsistencies on future calls to copyMMBitmapFromDisplayInRect
12-
SetThreadDpiAwarenessContext(initialDpiAwareness);
10+
// Configure DPI awareness to fetch unscaled display size
11+
if (IsWindows10OrGreater() && !IsWindowsServer()) {
12+
// Re-query desktop dimensions after setting the DPI awareness context
13+
// Only to this on Windows 10 client platforms, since earlier versions of Windows and Windows Server do not support this call
14+
DPI_AWARENESS_CONTEXT initialDpiAwareness = SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
15+
scaledDesktopWidth = (size_t)GetSystemMetrics(SM_CXSCREEN);
16+
scaledDesktopHeight = (size_t)GetSystemMetrics(SM_CYSCREEN);
17+
// Reset DPI awareness to avoid inconsistencies on future calls to copyMMBitmapFromDisplayInRect
18+
SetThreadDpiAwarenessContext(initialDpiAwareness);
19+
}
1320
size_t desktopWidth = (size_t)GetSystemMetrics(SM_CXSCREEN);
1421
size_t desktopHeight = (size_t)GetSystemMetrics(SM_CYSCREEN);
1522

1623
double scaleX = (double)(desktopWidth / (double)scaledDesktopWidth);
1724
double scaleY = (double)(desktopHeight / (double)scaledDesktopHeight);
1825

1926
return MMRectMake(
20-
input.origin.x / scaleX,
21-
input.origin.y / scaleY,
22-
input.size.width / scaleX,
27+
input.origin.x / scaleX,
28+
input.origin.y / scaleY,
29+
input.size.width / scaleX,
2330
input.size.height / scaleY
2431
);
2532
}

0 commit comments

Comments
 (0)