Added DPI awareness methods

master
Robinson 2023-11-22 10:12:03 +01:00
parent 390e65d845
commit 1110afa379
No known key found for this signature in database
GPG Key ID: 8E7DB78588BD6F5C
4 changed files with 56 additions and 4 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2021 dorkbox, llc
* Copyright 2023 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -38,7 +38,14 @@ import dorkbox.os.OS;
*/
public
class ShCore {
public static final int DPI_AWARENESS_INVALID = -1;
public static final int DPI_AWARENESS_UNAWARE = 0;
public static final int DPI_AWARENESS_SYSTEM_AWARE = 1;
public static final int DPI_AWARENESS_PER_MONITOR_AWARE = 2;
private static Function GetDpiForMonitor = null;
private static Function SetProcessDpiAwareness = null;
static {
if (OS.Windows.INSTANCE.isWindows8_1_plus()) {
@ -47,6 +54,7 @@ class ShCore {
// Abusing static fields this way is not proper, but it gets the job done nicely.
// GetDpiForMonitor is NOT always available! (Windows 8.1+)
GetDpiForMonitor = library.getFunction("GetDpiForMonitor");
SetProcessDpiAwareness = library.getFunction("SetProcessDpiAwareness");
}
}
@ -82,4 +90,18 @@ class ShCore {
return null;
}
}
/**
* https://learn.microsoft.com/en-us/windows/win32/api/shellscalingapi/nf-shellscalingapi-setprocessdpiawareness
*/
public static int SetProcessDpiAwareness(int value) {
if (SetProcessDpiAwareness != null) {
// HRESULT SetProcessDpiAwareness(
// [in] PROCESS_DPI_AWARENESS value
// );
return (int) SetProcessDpiAwareness.invoke(Integer.class, new Object[]{value});
} else {
return -1;
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2015 dorkbox, llc
* Copyright 2023 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -44,6 +44,10 @@ interface User32 {
int WM_LBUTTONUP = 0x202;
int WM_RBUTTONUP = 0x205;
Pointer DPI_AWARENESS_CONTEXT_UNAWARE = new Pointer(-1);
Pointer DPI_AWARENESS_CONTEXT_SYSTEM_AWARE = new Pointer(-2);
Pointer DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = new Pointer(-3);
/**
* This is overridden by the 64-bit version to be SetWindowLongPtr instead.
*/
@ -170,4 +174,14 @@ interface User32 {
* https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-monitorfrompoint
*/
HMONITOR MonitorFromPoint(POINT.ByValue pt, int dwFlags);
/**
* https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getdpiforsystem
*/
int GetDpiForSystem();
/**
* https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setthreaddpiawarenesscontext
*/
Pointer SetThreadDpiAwarenessContext(Pointer dpiContext);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2017 dorkbox, llc
* Copyright 2023 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -122,4 +122,12 @@ class User32_32 implements User32 {
@Override
public native
HMONITOR MonitorFromPoint(POINT.ByValue pt, int dwFlags);
@Override
public native
Pointer SetThreadDpiAwarenessContext(Pointer dpiContext);
@Override
public native
int GetDpiForSystem();
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2017 dorkbox, llc
* Copyright 2023 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -127,4 +127,12 @@ class User32_64 implements User32 {
@Override
public native
HMONITOR MonitorFromPoint(POINT.ByValue pt, int dwFlags);
@Override
public native
Pointer SetThreadDpiAwarenessContext(Pointer dpiContext);
@Override
public native
int GetDpiForSystem();
}