I am trying to implement GetWindowDpiAwarenessContext in a C# application with no success.
The relevent header files are:
windef.h
DECLARE_HANDLE(DPI_AWARENESS_CONTEXT);
typedef enum DPI_AWARENESS {
DPI_AWARENESS_INVALID = -1,
DPI_AWARENESS_UNAWARE = 0,
DPI_AWARENESS_SYSTEM_AWARE = 1,
DPI_AWARENESS_PER_MONITOR_AWARE = 2
} DPI_AWARENESS;
#define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
#define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT)-2)
#define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT)-3)
#define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
WinUser.h
WINUSERAPI
DPI_AWARENESS_CONTEXT
WINAPI
GetWindowDpiAwarenessContext(
_In_ HWND hwnd);
I am using:
/// <summary>
/// Class for native methods.
/// </summary>
internal static class NativeMethods {
[DllImport("user32.dll")]
internal static extern IntPtr GetWindowDpiAwarenessContext(IntPtr hWnd);
...
// Dpi awareness context
IntPtr dpiAwarenessContext = NativeMethods.GetWindowDpiAwarenessContext(process.Handle);
if (dpiAwarenessContext == (IntPtr)(-1)) {
sb.AppendLine(" DPI Awareness Context: DPI_AWARENESS_CONTEXT_UNAWARE");
} else if (dpiAwarenessContext == (IntPtr)(-2)) {
sb.AppendLine(" DPI Awareness Context: DPI_AWARENESS_CONTEXT_SYSTEM_AWARE");
} else if (dpiAwarenessContext == (IntPtr)(-3)) {
sb.AppendLine(" DPI Awareness Context: DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE");
} else if (dpiAwarenessContext == (IntPtr)(-4)) {
sb.AppendLine(" DPI Awareness Context: DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2");
} else {
sb.AppendLine(" DPI Awareness Context failed: " + dpiAwarenessContext);
}
It returns one of the following: 0, 18, 34, 24592, 61457, not the expected -1, -2, -3, or -4. In addition, in subsequent calls to the same window, the return value varies. (The windows are in other processes than this application.)
The question is what is the proper way to define and call GetWindowDpiAwarenessContext.
Thanks in advance.
DPI_AWARENESS_CONTEXT
is a handle, and so is pointer sized – UnoDPI_AWARENESS
. How is that relevant? – Uno