Achieving Crisp Text Rendering in High-DPI Windows Applications
High-resolution displays present a challenge for Windows applications: scaled interfaces often lead to blurry text. This is a consequence of Windows' bitmap-based DPI virtualization. To resolve this and ensure sharp text, applications must explicitly declare their ability to handle high DPI settings.
Enabling High-DPI Compatibility: The Manifest Approach
The solution involves modifying your application's manifest file. Adding the <dpiaware>
element signals to Windows that your application supports high DPI. Here's the necessary manifest code:
<code class="language-xml"><?xml version="1.0" encoding="utf-8"?> <assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3"> <assemblyIdentity name="MyApplication.app" version="1.0.0.0" /> <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2"> <security> <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3"> <requestedExecutionLevel level="asInvoker" uiAccess="false" /> </requestedPrivileges> </security> </trustInfo> <application> <windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings"> <dpiAware>true</dpiAware> </windowsSettings> </application> </assembly></code>
Alternative: Programmatic Approach (ClickOnce Deployments)
For ClickOnce deployments, you can use SetProcessDPIAware()
within the Main()
method:
<code class="language-csharp">[STAThread] static void Main() { if (Environment.OSVersion.Version.Major >= 6) SetProcessDPIAware(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); // Adjust as needed } [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern bool SetProcessDPIAware();</code>
Visual Studio 2015 Update 1 and Later:
Newer versions of Visual Studio (2015 Update 1 and later) simplify this. The manifest file is included in new projects; simply uncomment the <dpiaware>
tag.
Summary:
By declaring your application DPI-aware (via the manifest or SetProcessDPIAware()
), you bypass Windows' DPI virtualization limitations. This ensures sharp, scalable text rendering on high-DPI screens, greatly improving the user experience.
The above is the detailed content of How Can I Make My Windows Applications Display Crisp Text on High-DPI Screens?. For more information, please follow other related articles on the PHP Chinese website!