|
36 | 36 | import java.util.List;
|
37 | 37 |
|
38 | 38 | import com.sun.jna.Native;
|
| 39 | +import com.sun.jna.Pointer; |
39 | 40 | import com.sun.jna.win32.StdCallLibrary;
|
40 | 41 | import com.sun.jna.win32.W32APIOptions;
|
41 | 42 |
|
@@ -250,15 +251,54 @@ interface ExtUser32 extends StdCallLibrary, com.sun.jna.platform.win32.User32 {
|
250 | 251 | ExtUser32 INSTANCE = (ExtUser32) Native.loadLibrary("user32", ExtUser32.class, W32APIOptions.DEFAULT_OPTIONS);
|
251 | 252 |
|
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); |
253 | 267 | }
|
254 | 268 |
|
| 269 | + private static int detected = detectSystemDPI(); |
| 270 | + |
255 | 271 | @Override
|
256 | 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 | + int res = ExtUser32.INSTANCE.SetProcessDpiAwareness(ExtUser32.DPI_AWARENESS_SYSTEM_AWARE); |
| 281 | + System.out.println("SetProcessDpiAwareness returned " + res); |
| 282 | + } catch (Throwable e) { |
| 283 | + System.out.println("SetProcessDpiAwareness failed!"); |
| 284 | + // Ignore error |
| 285 | + } |
| 286 | + try { |
| 287 | + System.out.println("before any SetThreadDpiAwarenessContext(...) -> dpi " + ExtUser32.INSTANCE.GetDpiForSystem()); |
| 288 | + ExtUser32.INSTANCE.SetThreadDpiAwarenessContext(ExtUser32.DPI_AWARENESS_CONTEXT_UNAWARE); |
| 289 | + System.out.println("SetThreadDpiAwarenessContext(UNAWARE) -> dpi " + ExtUser32.INSTANCE.GetDpiForSystem()); |
| 290 | + ExtUser32.INSTANCE.SetThreadDpiAwarenessContext(ExtUser32.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE); |
| 291 | + System.out.println("SetThreadDpiAwarenessContext(SYSTEM) -> dpi " + ExtUser32.INSTANCE.GetDpiForSystem()); |
| 292 | + } catch (Throwable e) { |
| 293 | + System.out.println("SetThreadDpiAwarenessContext failed!"); |
| 294 | + // Ignore error (call valid only on Windows 10) |
| 295 | + } |
257 | 296 | try {
|
258 | 297 | return ExtUser32.INSTANCE.GetDpiForSystem();
|
259 | 298 | } catch (Throwable e) {
|
260 | 299 | // DPI detection failed, fall back with default
|
261 |
| - return super.getSystemDPI(); |
| 300 | + System.out.println("DPI detection failed, fallback to 96 dpi"); |
| 301 | + return -1; |
262 | 302 | }
|
263 | 303 | }
|
264 | 304 | }
|
0 commit comments