Skip to content

Commit 1c667f8

Browse files
authored
Merge pull request #12726 from AvaloniaUI/win32_dpi_awareness_option
Add win32 platform options to select process DPI awareness
2 parents 05d348b + fc4f778 commit 1c667f8

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

src/Windows/Avalonia.Win32/Win32Platform.cs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ internal class Win32Platform : IWindowingPlatform, IPlatformIconLoader, IPlatfor
4949

5050
public Win32Platform()
5151
{
52-
SetDpiAwareness();
5352
CreateMessageWindow();
5453
_dispatcher = new Win32DispatcherImpl(_hwnd);
5554
}
@@ -80,6 +79,9 @@ public static void Initialize()
8079
public static void Initialize(Win32PlatformOptions options)
8180
{
8281
s_options = options;
82+
83+
SetDpiAwareness();
84+
8385
var renderTimer = options.ShouldRenderOnUIThread ? new UiThreadRenderTimer(60) : new DefaultRenderTimer(60);
8486

8587
AvaloniaLocator.CurrentMutable
@@ -264,12 +266,31 @@ private static void SetDpiAwareness()
264266
var user32 = LoadLibrary("user32.dll");
265267
var method = GetProcAddress(user32, nameof(SetProcessDpiAwarenessContext));
266268

269+
var dpiAwareness = Options.DpiAwareness;
270+
267271
if (method != IntPtr.Zero)
268272
{
269-
if (SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2) ||
270-
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE))
273+
if (dpiAwareness == Win32DpiAwareness.Unaware)
271274
{
272-
return;
275+
if (SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_UNAWARE))
276+
{
277+
return;
278+
}
279+
}
280+
else if (dpiAwareness == Win32DpiAwareness.SystemDpiAware)
281+
{
282+
if (SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_SYSTEM_AWARE))
283+
{
284+
return;
285+
}
286+
}
287+
else if (dpiAwareness == Win32DpiAwareness.PerMonitorDpiAware)
288+
{
289+
if (SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2) ||
290+
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE))
291+
{
292+
return;
293+
}
273294
}
274295
}
275296

@@ -278,11 +299,20 @@ private static void SetDpiAwareness()
278299

279300
if (method != IntPtr.Zero)
280301
{
281-
SetProcessDpiAwareness(PROCESS_DPI_AWARENESS.PROCESS_PER_MONITOR_DPI_AWARE);
302+
var awareness = (dpiAwareness) switch
303+
{
304+
Win32DpiAwareness.Unaware => PROCESS_DPI_AWARENESS.PROCESS_DPI_UNAWARE,
305+
Win32DpiAwareness.SystemDpiAware => PROCESS_DPI_AWARENESS.PROCESS_SYSTEM_DPI_AWARE,
306+
Win32DpiAwareness.PerMonitorDpiAware => PROCESS_DPI_AWARENESS.PROCESS_PER_MONITOR_DPI_AWARE,
307+
_ => PROCESS_DPI_AWARENESS.PROCESS_PER_MONITOR_DPI_AWARE,
308+
};
309+
310+
SetProcessDpiAwareness(awareness);
282311
return;
283312
}
284313

285-
SetProcessDPIAware();
314+
if (dpiAwareness != Win32DpiAwareness.Unaware)
315+
SetProcessDPIAware();
286316
}
287317
}
288318
}

src/Windows/Avalonia.Win32/Win32PlatformOptions.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,27 @@ public enum Win32RenderingMode
2525
Wgl = 3
2626
}
2727

28+
/// <summary>
29+
/// Represents the DPI Awareness for the application.
30+
/// </summary>
31+
public enum Win32DpiAwareness
32+
{
33+
/// <summary>
34+
/// The application is DPI unaware.
35+
/// </summary>
36+
Unaware,
37+
38+
/// <summary>
39+
/// The application is system DPI aware. It will query DPI once and will not adjust to new DPI changes
40+
/// </summary>
41+
SystemDpiAware,
42+
43+
/// <summary>
44+
/// The application is per-monitor DPI aware. It adjust its scale factor whenever DPI changes.
45+
/// </summary>
46+
PerMonitorDpiAware
47+
}
48+
2849
/// <summary>
2950
/// Represents the Win32 window composition mode.
3051
/// </summary>
@@ -137,4 +158,9 @@ public class Win32PlatformOptions
137158
/// and <see cref="CompositionMode"/> only accepts null or <see cref="Win32CompositionMode.RedirectionSurface"/>.
138159
/// </summary>
139160
public IPlatformGraphics? CustomPlatformGraphics { get; set; }
161+
162+
/// <summary>
163+
/// Gets or sets the application's DPI awareness.
164+
/// </summary>
165+
public Win32DpiAwareness DpiAwareness { get; set; } = Win32DpiAwareness.PerMonitorDpiAware;
140166
}

0 commit comments

Comments
 (0)