S
S
Semyon Novikov2018-04-12 17:57:21
WPF
Semyon Novikov, 2018-04-12 17:57:21

How to fix blurry text in Windows Forms?

In any win forms projects, the text is blurry, and everything is fine in the editor. Even when transferring a project from another computer where the text is clear, everything becomes bad on mine. I am attaching screenshots of the editor and the running application.
I have Windows 10, Visual Studio 2017 . Changing the version of net.framework doesn't help. Maybe remove the anti-aliasing of the text in the settings, I climbed everything, I didn’t find it.
5acf73521f0ed515042302.png5acf73565c413096745749.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
VoidVolker, 2018-04-12
@semennovikov123

Google DpiAware - study the problems and choose the best solution for you. Here the problem is in the wild legacy heritage, implemented through crutches and which they are trying to save by adding fixes in the form of crutches that generate new bugs and in new versions they try to fix it again with crutches. In addition, the scaling algorithm is slightly different in different OS versions. So I highly recommend checking the operation of the application in different OS versions (7, 8.1, 10 Home, 10 Enterprise LTS) and with scaling turned off and on. Yes, yes, in the home ten and in the corporate long-term ten you can get a different result.
Option 1 - call a special function in the code (the real code I use - plus a second function for scaling fonts, tyk- something like this happens in 4k):

const int WinDefaultDPI = 96;

/// <summary>
/// Исправление блюра при включенном масштабировании в ОС windows 8 и выше
/// </summary>
public static void DpiFix()
{
    if (Environment.OSVersion.Version.Major >= 6)
    {
        SetProcessDPIAware();
    }
}

/// <summary>
/// WinAPI SetProcessDPIAware 
/// </summary>
/// <returns></returns>
[DllImport("user32.dll")]
private static extern bool SetProcessDPIAware();

/// <summary>
/// Исправление размера шрифтов
/// </summary>
/// <param name="c"></param>
public static float DpiFixFonts(Control c)
{
    Graphics g = c.CreateGraphics();
    float dx = g.DpiX
        , dy = g.DpiY
        , fontsScale = Math.Max(dx, dy) / WinDefaultDPI
    ;
    g.Dispose();
    return fontsScale;
}

Plus all forms have the following config (code called in form constructors):
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;

Please note that AutoScaleDimensions- in the constructor is set automatically depending on the resolution and DPI of your monitor. That's why I don't use constructors. Otherwise, when you run the application on different machines with different DPI, you can get very unexpected results.
Option 2 (v4.7):
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
  </startup>
  <System.Windows.Forms.ApplicationConfigurationSection>
    <add key="DpiAwareness" value="PerMonitorV2" />
    <add key="EnableWindowsFormsHighDpiAutoResizing" value="true" />
  </System.Windows.Forms.ApplicationConfigurationSection>
</configuration>

Option 3 (v4.7):
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
  </startup>
  <System.Windows.Forms.ApplicationConfigurationSection>
    <add key="DpiAwareness" value="PerMonitorV2" />
    <add key="EnableWindowsFormsHighDpiAutoResizing" value="false" />
  </System.Windows.Forms.ApplicationConfigurationSection>
<configuration>

Option 4:
<!-- Указывает, что приложение поддерживает определение DPI и не будет автоматически масштабироваться Windows при более высоких
       значениях DPI. Приложения Windows Presentation Foundation (WPF) по умолчанию поддерживают определение DPI, им не нужно 
       специально включать параметр для этого. Для приложений Windows Forms на платформе .NET Framework 4.6, для которых задан этот параметр, необходимо 
       также задать для "EnableWindowsFormsHighDpiAutoResizing" значение "true" в файле app.config.-->
  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
    </windowsSettings>
  </application>

A
alexalexes, 2018-04-12
@alexalexes

Type "ClearType" into the Start menu search. This is the same option setting. She is?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question