Skip to content

Commit 3957707

Browse files
committed
Use Win10 API SetThreadDpiAwarenessContext to force DPI Awareness
This commit contains debug messages used for testing purposes.
1 parent fceb1d7 commit 3957707

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

arduino-core/src/processing/app/windows/Platform.java

+41-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.List;
3737

3838
import com.sun.jna.Native;
39+
import com.sun.jna.Pointer;
3940
import com.sun.jna.win32.StdCallLibrary;
4041
import com.sun.jna.win32.W32APIOptions;
4142

@@ -250,15 +251,54 @@ interface ExtUser32 extends StdCallLibrary, com.sun.jna.platform.win32.User32 {
250251
ExtUser32 INSTANCE = (ExtUser32) Native.loadLibrary("user32", ExtUser32.class, W32APIOptions.DEFAULT_OPTIONS);
251252

252253
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);
253267
}
254268

269+
private static int detected = detectSystemDPI();
270+
255271
@Override
256272
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+
}
257296
try {
258297
return ExtUser32.INSTANCE.GetDpiForSystem();
259298
} catch (Throwable e) {
260299
// 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;
262302
}
263303
}
264304
}

0 commit comments

Comments
 (0)