Skip to content

Commit 20e1eaa

Browse files
amartya4256HeikoKlare
authored andcommitted
Extract LOGPIXELS* usage to FontDialog::getDPI #62
This change refactors the usage of LOGPIXELS* in FontDialog to make it clear why we depend on the System DPI aware value while using DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED and extracts it into a new method FontDialog::getDPI. Contributes to #62 and #127
1 parent 979522d commit 20e1eaa

File tree

1 file changed

+14
-5
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets

1 file changed

+14
-5
lines changed

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,7 @@ public FontData open () {
200200
if (fontData != null && fontData.data != null) {
201201
LOGFONT logFont = fontData.data;
202202
int lfHeight = logFont.lfHeight;
203-
long hDC = OS.GetDC (0);
204-
int pixels = -(int)(0.5f + (fontData.height * OS.GetDeviceCaps(hDC, OS.LOGPIXELSY) / 72));
205-
OS.ReleaseDC (0, hDC);
203+
int pixels = -(int)(0.5f + (fontData.height * getDPI() / 72));
206204
logFont.lfHeight = pixels;
207205
lpcf.Flags |= OS.CF_INITTOLOGFONTSTRUCT;
208206
OS.MoveMemory (lpLogFont, logFont, LOGFONT.sizeof);
@@ -256,9 +254,9 @@ public FontData open () {
256254
* This will not work on multiple screens or for printing. Should use DC for the
257255
* proper device.
258256
*/
259-
long hDC = OS.GetDC(0);
260-
int logPixelsY = OS.GetDeviceCaps(hDC, OS.LOGPIXELSY);
257+
int logPixelsY = getDPI();
261258
int pixels = 0;
259+
long hDC = OS.GetDC(0);
262260
if (logFont.lfHeight > 0) {
263261
/*
264262
* Feature in Windows. If the lfHeight of the LOGFONT structure is positive, the
@@ -308,6 +306,17 @@ public FontData open () {
308306
return fontData;
309307
}
310308

309+
private int getDPI() {
310+
long hDC = OS.GetDC (0);
311+
// We need to use OS.GetDeviceCaps, which is static throughout application
312+
// lifecycle (System DPI Aware), because we use
313+
// DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED which always depends on the DPI at
314+
// application startup
315+
int dpi = OS.GetDeviceCaps(hDC, OS.LOGPIXELSY);
316+
OS.ReleaseDC (0, hDC);
317+
return dpi;
318+
}
319+
311320
/**
312321
* Sets the effects selection controls in the dialog visible if the
313322
* argument is <code>true</code>, and invisible otherwise.

0 commit comments

Comments
 (0)