Skip to content

Commit fce19db

Browse files
committed
(nut-tree/nut.js#249) Enable DPI awareness when fetching screen content to always capture the full screen content even with scaling applied
1 parent 8575edb commit fce19db

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/win32/screengrab.c

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

55
MMRect getScaledRect(MMRect input, HDC imageSource) {
6-
BITMAP structBitmapHeader;
7-
memset( &structBitmapHeader, 0, sizeof(BITMAP) );
8-
9-
HGDIOBJ hBitmap = GetCurrentObject(imageSource, OBJ_BITMAP);
10-
GetObject(hBitmap, sizeof(BITMAP), &structBitmapHeader);
11-
126
size_t desktopWidth = (size_t)GetSystemMetrics(SM_CXSCREEN);
137
size_t desktopHeight = (size_t)GetSystemMetrics(SM_CYSCREEN);
148

15-
double scaleX = (double)(structBitmapHeader.bmWidth / desktopWidth);
16-
double scaleY = (double)(structBitmapHeader.bmHeight / desktopHeight);
9+
double scaleX = (double)(desktopWidth / (double)input.size.width);
10+
double scaleY = (double)(desktopHeight / (double)input.size.height);
1711

1812
return MMRectMake(input.origin.x, input.origin.y, input.size.width * scaleX, input.size.height * scaleY);
1913
}
2014

2115
MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect)
2216
{
17+
// Configure DPI awareness to fetch unscaled display size
18+
DPI_AWARENESS_CONTEXT initialDpiAwareness = SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
2319
MMBitmapRef bitmap;
2420
void *data;
2521
HDC screen = NULL, screenMem = NULL;
@@ -84,6 +80,8 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect)
8480
ReleaseDC(NULL, screen);
8581
DeleteObject(dib);
8682
DeleteDC(screenMem);
83+
// Reset DPI awareness to avoid inconsistencies on future calls to copyMMBitmapFromDisplayInRect
84+
SetThreadDpiAwarenessContext(initialDpiAwareness);
8785

8886
return bitmap;
8987
}

0 commit comments

Comments
 (0)