Skip to content

Commit 78d23b7

Browse files
committed
Ensure Consistent DPI Awareness in getDPI() for Font Calculation
The DPI computed in getDPI() is used to determine the font height for the OS.ChooseFont dialog. Following the change in b2c80a4, the thread DPI awareness is set to OS.DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED before opening the ChooseFont dialog. This commit ensures the same DPI awareness is applied in getDPI() for consistent font size calculation. Fixes #2866
1 parent 9740516 commit 78d23b7

File tree

1 file changed

+18
-8
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets

1 file changed

+18
-8
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/FontDialog.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,24 @@ public FontData open () {
278278
}
279279

280280
private int getDPI() {
281-
long hDC = OS.GetDC (0);
282-
// We need to use OS.GetDeviceCaps, which is static throughout application
283-
// lifecycle (System DPI Aware), because we use
284-
// DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED which always depends on the DPI at
285-
// application startup
286-
int dpi = OS.GetDeviceCaps(hDC, OS.LOGPIXELSY);
287-
OS.ReleaseDC (0, hDC);
288-
return dpi;
281+
long currentDpiAwarenessContext = OS.GetThreadDpiAwarenessContext();
282+
try {
283+
if (parent.getDisplay().isRescalingAtRuntime()) {
284+
currentDpiAwarenessContext = OS.SetThreadDpiAwarenessContext(OS.DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED);
285+
}
286+
long hDC = OS.GetDC(0);
287+
// We need to use OS.GetDeviceCaps, which is static throughout application
288+
// lifecycle (System DPI Aware), because we use
289+
// DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED which always depends on the DPI at
290+
// application startup
291+
int dpi = OS.GetDeviceCaps(hDC, OS.LOGPIXELSY);
292+
OS.ReleaseDC(0, hDC);
293+
return dpi;
294+
} finally {
295+
if (parent.getDisplay().isRescalingAtRuntime()) {
296+
OS.SetThreadDpiAwarenessContext(currentDpiAwarenessContext);
297+
}
298+
}
289299
}
290300

291301
/**

0 commit comments

Comments
 (0)