Home >Backend Development >C++ >How Can I Fix Blurry Text in My Windows Forms Application at High DPI Settings?

How Can I Fix Blurry Text in My Windows Forms Application at High DPI Settings?

Patricia Arquette
Patricia ArquetteOriginal
2025-02-03 01:16:09432browse

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

  1. Add a new "Application Manifest File" to your project.
  2. Replace the contents with the following XML:
<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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn