Unexpected DPI Awareness Changes After Application Updates
Some applications, originally designed as DPI-unaware (relying on Windows' UI scaling), may unexpectedly become DPI-aware (System Aware) following minor updates. This occurs even if the application manifest and external dependencies remain unchanged.
Root Cause:
This behavior is often caused by DPI-aware third-party components or dependencies. Even with an explicit DPI-Unaware declaration in the application manifest, inclusion of such components can override the setting, forcing DPI-awareness.
Solutions for Defining DPI Awareness:
Several approaches can explicitly control an application's DPI awareness:
1. Modifying the Application Manifest:
app.manifest
to include (or uncomment) the following:<code class="language-xml"><dpiaware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">false</dpiaware></code>
2. Utilizing Windows API Functions:
The appropriate API call depends on the Windows version:
<code class="language-csharp">[DllImport("user32.dll", SetLastError = true)] static extern bool SetProcessDPIAware();</code>
<code class="language-csharp">[DllImport("shcore.dll")] static extern int SetProcessDpiAwareness(ProcessDPIAwareness value); enum ProcessDPIAwareness { DPI_Unaware = 0, System_DPI_Aware = 1, Per_Monitor_DPI_Aware = 2 }</code>
<code class="language-csharp">[DllImport("user32.dll", SetLastError = true)] static extern int SetProcessDpiAwarenessContext(DpiAwarenessContext value); enum DpiAwarenessContext { Context_Unaware = (DPI_AWARENESS_CONTEXT)-1, Context_SystemAware = (DPI_AWARENESS_CONTEXT)-2, Context_PerMonitorAware = (DPI_AWARENESS_CONTEXT)-3, Context_PerMonitorAwareV2 = (DPI_AWARENESS_CONTEXT)-4 }</code>
3. Using AssemblyInfo.cs
:
Add this attribute to override system-defined DPI awareness based on component references:
<code class="language-csharp">[assembly: System.Windows.Media.DisableDpiAwareness]</code>
Important Considerations:
The above is the detailed content of Why Does My DPI-Unaware Application Suddenly Become DPI-Aware After an Update?. For more information, please follow other related articles on the PHP Chinese website!