|
35 | 35 | import java.util.LinkedList; |
36 | 36 | import java.util.List; |
37 | 37 |
|
| 38 | +import com.sun.jna.Native; |
| 39 | +import com.sun.jna.Pointer; |
| 40 | +import com.sun.jna.win32.StdCallLibrary; |
| 41 | +import com.sun.jna.win32.W32APIOptions; |
38 | 42 |
|
39 | 43 | public class Platform extends processing.app.Platform { |
40 | 44 |
|
@@ -240,4 +244,54 @@ public void fixSettingsLocation() throws Exception { |
240 | 244 |
|
241 | 245 | Files.move(oldSettingsFolder, settingsFolder.toPath()); |
242 | 246 | } |
| 247 | + |
| 248 | + // Need to extend com.sun.jna.platform.win32.User32 to access |
| 249 | + // Win32 function GetDpiForSystem() |
| 250 | + interface ExtUser32 extends StdCallLibrary, com.sun.jna.platform.win32.User32 { |
| 251 | + ExtUser32 INSTANCE = (ExtUser32) Native.loadLibrary("user32", ExtUser32.class, W32APIOptions.DEFAULT_OPTIONS); |
| 252 | + |
| 253 | + public int GetDpiForSystem(); |
| 254 | + |
| 255 | + public int SetProcessDpiAwareness(int value); |
| 256 | + |
| 257 | + public final int DPI_AWARENESS_INVALID = -1; |
| 258 | + public final int DPI_AWARENESS_UNAWARE = 0; |
| 259 | + public final int DPI_AWARENESS_SYSTEM_AWARE = 1; |
| 260 | + public final int DPI_AWARENESS_PER_MONITOR_AWARE = 2; |
| 261 | + |
| 262 | + public Pointer SetThreadDpiAwarenessContext(Pointer dpiContext); |
| 263 | + |
| 264 | + public final Pointer DPI_AWARENESS_CONTEXT_UNAWARE = new Pointer(-1); |
| 265 | + public final Pointer DPI_AWARENESS_CONTEXT_SYSTEM_AWARE = new Pointer(-2); |
| 266 | + public final Pointer DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = new Pointer(-3); |
| 267 | + } |
| 268 | + |
| 269 | + private static int detected = detectSystemDPI(); |
| 270 | + |
| 271 | + @Override |
| 272 | + public int getSystemDPI() { |
| 273 | + if (detected == -1) |
| 274 | + return super.getSystemDPI(); |
| 275 | + return detected; |
| 276 | + } |
| 277 | + |
| 278 | + public static int detectSystemDPI() { |
| 279 | + try { |
| 280 | + ExtUser32.INSTANCE.SetProcessDpiAwareness(ExtUser32.DPI_AWARENESS_SYSTEM_AWARE); |
| 281 | + } catch (Throwable e) { |
| 282 | + // Ignore error |
| 283 | + } |
| 284 | + try { |
| 285 | + ExtUser32.INSTANCE.SetThreadDpiAwarenessContext(ExtUser32.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE); |
| 286 | + } catch (Throwable e) { |
| 287 | + // Ignore error (call valid only on Windows 10) |
| 288 | + } |
| 289 | + try { |
| 290 | + return ExtUser32.INSTANCE.GetDpiForSystem(); |
| 291 | + } catch (Throwable e) { |
| 292 | + // DPI detection failed, fall back with default |
| 293 | + System.out.println("DPI detection failed, fallback to 96 dpi"); |
| 294 | + return -1; |
| 295 | + } |
| 296 | + } |
243 | 297 | } |
0 commit comments