Skip to content

Commit 1003d8c

Browse files
committed
chore: Refactor DPI awareness initialization logic
Move DPI awareness setup to a dedicated `DpiBootstrap` class using a module initializer for improved clarity and reuse. This ensures the correct DPI context is applied before any window initialization, replacing redundant logic in `Win32Host`.
1 parent f0c8d93 commit 1003d8c

2 files changed

Lines changed: 36 additions & 38 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Runtime.CompilerServices;
2+
using Windows.Win32;
3+
using Windows.Win32.UI.HiDpi;
4+
5+
#pragma warning disable CA2255
6+
7+
internal static class DpiBootstrap
8+
{
9+
[ModuleInitializer]
10+
internal static void Init()
11+
{
12+
// This call ensures that the application is properly marked as per-monitor DPI aware (PerMonitorV2).
13+
// When launched via `dotnet.exe` (e.g., `dotnet MyApp.dll` or `dotnet run`), the host process is the .NET CLI runtime,
14+
// which does NOT contain a DPI-awareness declaration in its embedded application manifest (RT_MANIFEST).
15+
// Consequently, Windows assumes the process is DPI-unaware, applies bitmap scaling, and reports a logical DPI of 96,
16+
// leading to incorrect pointer coordinates, layout glitches, and drag-and-drop offsets.
17+
//
18+
// In contrast, when launching a native application executable (e.g., `MyApp.exe`), the process inherits the
19+
// DPI-awareness setting defined in the manifest block:
20+
//
21+
// <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
22+
// <asmv3:application>
23+
// <asmv3:windowsSettings>
24+
// <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
25+
// </asmv3:windowsSettings>
26+
// </asmv3:application>
27+
// </assembly>
28+
//
29+
// More details on DPI Awareness contexts and behaviors:
30+
// - https://learn.microsoft.com/en-us/windows/win32/hidpi/setting-the-default-dpi-awareness-for-a-process
31+
//
32+
// This call must be made BEFORE any window is initialized.
33+
34+
PInvoke.SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
35+
}
36+
}

‎src/Uno.UI.Runtime.Skia.Win32/Win32Host.cs‎

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,6 @@ public Win32Host(Func<Application> appBuilder)
122122

123123
protected override void Initialize()
124124
{
125-
SetProcessDpiAwareness();
126-
127125
CoreDispatcher.DispatchOverride = Win32EventLoop.Schedule;
128126
CoreDispatcher.HasThreadAccessOverride = () => _isDispatcherThread;
129127

@@ -132,42 +130,6 @@ protected override void Initialize()
132130
CompositionTargetTimer.Start();
133131
}
134132

135-
private void SetProcessDpiAwareness()
136-
{
137-
// This call ensures that the application is properly marked as per-monitor DPI aware (PerMonitorV2).
138-
// When launched via `dotnet.exe` (e.g., `dotnet MyApp.dll` or `dotnet run`), the host process is the .NET CLI runtime,
139-
// which does NOT contain a DPI-awareness declaration in its embedded application manifest (RT_MANIFEST).
140-
// Consequently, Windows assumes the process is DPI-unaware, applies bitmap scaling, and reports a logical DPI of 96,
141-
// leading to incorrect pointer coordinates, layout glitches, and drag-and-drop offsets.
142-
//
143-
// In contrast, when launching a native application executable (e.g., `MyApp.exe`), the process inherits the
144-
// DPI-awareness setting defined in the manifest block:
145-
//
146-
// <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
147-
// <asmv3:application>
148-
// <asmv3:windowsSettings>
149-
// <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
150-
// </asmv3:windowsSettings>
151-
// </asmv3:application>
152-
// </assembly>
153-
//
154-
// Important: Calling SetThreadDpiAwarenessContext() affects only the DPI behavior of user32/GDI calls **on that thread**,
155-
// but it does NOT update the global DPI-awareness context of the process, nor does it affect how the OS scales
156-
// the window or reports DPI-related metrics to frameworks like Skia or WinUI. To enable proper DPI scaling and
157-
// accurate coordinate transformation, the process DPI-awareness must be set at launch or via SetProcessDpiAwarenessContext().
158-
//
159-
// NOTE: DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 (-4) provides the most accurate and modern DPI behavior available
160-
// on Windows 10+ with improved scaling, child-window consistency, and multi-monitor support.
161-
//
162-
// More details on DPI Awareness contexts and behaviors:
163-
// - https://learn.microsoft.com/en-us/windows/win32/hidpi/setting-the-default-dpi-awareness-for-a-process
164-
//
165-
// This call must be made BEFORE any window is initialized.
166-
167-
var success = PInvoke.SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2) != 0;
168-
if (!success) { this.LogError()?.Error($"{nameof(PInvoke.SetProcessDpiAwarenessContext)} failed: {Win32Helper.GetErrorMessage()}"); }
169-
}
170-
171133
protected override Task RunLoop()
172134
{
173135
Win32EventLoop.Schedule(() => Application.Start(_ =>

0 commit comments

Comments
 (0)