Home >Backend Development >C++ >How Can I Fix Blurry Text in My Windows Forms Application at High DPI Settings?
Sharp Text Rendering in High-DPI Windows Forms Applications
High DPI settings (150% and above) can cause blurry text in Windows Forms applications due to Windows' default scaling mechanism. To achieve clear text rendering, your application must explicitly declare its high-DPI compatibility. This is done by modifying the application manifest or using a P/Invoke call.
Method 1: Modifying the Application Manifest
<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>
Method 2: Using P/Invoke for ClickOnce Deployments
For ClickOnce deployments, add the following code to your 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>
By implementing either method, your application will correctly handle high-DPI scaling, ensuring sharp text display on high-resolution screens.
The above is the detailed content of How Can I Fix Blurry Text in My Windows Forms Application at High DPI Settings?. For more information, please follow other related articles on the PHP Chinese website!