Skip to content

Commit e93d1f0

Browse files
committed
(nut-tree/nut.js#249) Refactor scale calculation to work correctly with custom regions
1 parent fce19db commit e93d1f0

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/win32/screengrab.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@
33
#include <stdlib.h> /* malloc() */
44

55
MMRect getScaledRect(MMRect input, HDC imageSource) {
6+
// Configure DPI awareness to fetch unscaled display size
7+
DPI_AWARENESS_CONTEXT initialDpiAwareness = SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
8+
size_t scaledDesktopWidth = (size_t)GetSystemMetrics(SM_CXSCREEN);
9+
size_t scaledDesktopHeight = (size_t)GetSystemMetrics(SM_CYSCREEN);
10+
// Reset DPI awareness to avoid inconsistencies on future calls to copyMMBitmapFromDisplayInRect
11+
SetThreadDpiAwarenessContext(initialDpiAwareness);
612
size_t desktopWidth = (size_t)GetSystemMetrics(SM_CXSCREEN);
713
size_t desktopHeight = (size_t)GetSystemMetrics(SM_CYSCREEN);
814

9-
double scaleX = (double)(desktopWidth / (double)input.size.width);
10-
double scaleY = (double)(desktopHeight / (double)input.size.height);
15+
double scaleX = (double)(desktopWidth / (double)scaledDesktopWidth);
16+
double scaleY = (double)(desktopHeight / (double)scaledDesktopHeight);
1117

1218
return MMRectMake(input.origin.x, input.origin.y, input.size.width * scaleX, input.size.height * scaleY);
1319
}
1420

1521
MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect)
1622
{
17-
// Configure DPI awareness to fetch unscaled display size
18-
DPI_AWARENESS_CONTEXT initialDpiAwareness = SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
1923
MMBitmapRef bitmap;
2024
void *data;
2125
HDC screen = NULL, screenMem = NULL;
@@ -80,8 +84,6 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect)
8084
ReleaseDC(NULL, screen);
8185
DeleteObject(dib);
8286
DeleteDC(screenMem);
83-
// Reset DPI awareness to avoid inconsistencies on future calls to copyMMBitmapFromDisplayInRect
84-
SetThreadDpiAwarenessContext(initialDpiAwareness);
8587

8688
return bitmap;
8789
}

0 commit comments

Comments
 (0)