Skip to content

Commit d72d0d2

Browse files
committed
Add DPI awareness debug logging
1 parent f6b4539 commit d72d0d2

3 files changed

Lines changed: 76 additions & 0 deletions

File tree

‎QuickLook/App.xaml.cs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ static App()
7070
);
7171
}
7272

73+
#if DEBUG
74+
if (SHCore.GetProcessDpiAwareness(0, out SHCore.PROCESS_DPI_AWARENESS awareness) == 0)
75+
Debug.WriteLine($"Process DPI awareness: {awareness} ({(int)awareness})");
76+
else
77+
Debug.WriteLine("GetProcessDpiAwareness failed");
78+
79+
if (User32.GetProcessDpiAwarenessContext(out User32.DPI_AWARENESS_CONTEXT dpiContext))
80+
Debug.WriteLine($"Process DPI awareness context: {dpiContext} ({(int)dpiContext})");
81+
else
82+
Debug.WriteLine("GetProcessDpiAwarenessContext failed");
83+
#endif
84+
7385
// Occurs when the resolution of an assembly fails
7486
AppDomain.CurrentDomain.AssemblyResolve += (_, e) =>
7587
{

‎QuickLook/NativeMethods/SHCore.cs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,11 @@ public enum PROCESS_DPI_AWARENESS
3030

3131
[DllImport("shcore.dll")]
3232
public static extern uint SetProcessDpiAwareness(PROCESS_DPI_AWARENESS awareness);
33+
34+
/// <summary>
35+
/// Gets the DPI awareness of a process. Pass <c>0</c> for the current process.
36+
/// Requires Windows 8.1 or later.
37+
/// </summary>
38+
[DllImport("shcore.dll")]
39+
public static extern uint GetProcessDpiAwareness(nint hprocess, out PROCESS_DPI_AWARENESS value);
3340
}

‎QuickLook/NativeMethods/User32.cs‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,61 @@ public enum MessageBoxResult : int
7272
IDTRYAGAIN = 10,
7373
IDCONTINUE = 11
7474
}
75+
76+
public enum DPI_AWARENESS_CONTEXT
77+
{
78+
DPI_AWARENESS_CONTEXT_UNAWARE = -1,
79+
DPI_AWARENESS_CONTEXT_SYSTEM_AWARE = -2,
80+
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = -3,
81+
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 = -4,
82+
DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED = -5,
83+
}
84+
85+
/// <summary>
86+
/// Requires Windows 10 version 1703 (build 15063) or later for
87+
/// <see cref="DPI_AWARENESS_CONTEXT.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2"/>.
88+
/// Call this before any windows are created.
89+
/// </summary>
90+
[DllImport("user32.dll")]
91+
public static extern bool SetProcessDpiAwarenessContext(nint dpiContext);
92+
93+
[DllImport("user32.dll")]
94+
public static extern nint GetThreadDpiAwarenessContext();
95+
96+
[DllImport("user32.dll")]
97+
public static extern bool AreDpiAwarenessContextsEqual(nint dpiContextA, nint dpiContextB);
98+
99+
/// <summary>
100+
/// Gets the current thread DPI awareness context.
101+
/// Requires Windows 10 version 1607 (build 14393) or later.
102+
/// <see cref="DPI_AWARENESS_CONTEXT.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2"/> is only reported on Windows 10 version 1703 or later.
103+
/// </summary>
104+
public static bool GetProcessDpiAwarenessContext(out DPI_AWARENESS_CONTEXT dpiContext)
105+
{
106+
dpiContext = default;
107+
108+
nint context = GetThreadDpiAwarenessContext();
109+
if (context == 0)
110+
return false;
111+
112+
DPI_AWARENESS_CONTEXT[] known =
113+
[
114+
DPI_AWARENESS_CONTEXT.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2,
115+
DPI_AWARENESS_CONTEXT.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE,
116+
DPI_AWARENESS_CONTEXT.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE,
117+
DPI_AWARENESS_CONTEXT.DPI_AWARENESS_CONTEXT_UNAWARE,
118+
DPI_AWARENESS_CONTEXT.DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED,
119+
];
120+
121+
foreach (DPI_AWARENESS_CONTEXT candidate in known)
122+
{
123+
if (AreDpiAwarenessContextsEqual(context, (nint)candidate))
124+
{
125+
dpiContext = candidate;
126+
return true;
127+
}
128+
}
129+
130+
return false;
131+
}
75132
}

0 commit comments

Comments
 (0)