This article is just to give you a brief idea about SetCompatibleTextRenderingDefault and what happens if we pass true instead of false.
Table of Contents
If you’re using Visual C# 2005 or later, the Program.cs file will generate an automatic call to SetCompatibleTextRenderingDefault.
In order to ensure visual consistency between Windows Forms controls that use the TextRenderer class and applications built for.NET Framework 1.0 and 1.1 that perform custom text rendering using the Graphics class, the UseCompatibleTextRendering property was introduced.
Keep UseCompatibleTextRendering at its default value of false unless you are specifically upgrading your application from.NET Framework 1.0 or 1.1.
SetCompatibleTextRenderingDefault sets the application-wide default for the UseCompatibleTextRendering
property defined on certain controls.
public static void SetCompatibleTextRenderingDefault (bool value);
Read further on about What Is SetCompatibleTextRenderingDefault in detail.
SetCompatibleTextRenderingDefault (false) is an automatic method generated by visual studio in the Main() method of Program.cs file. Since it is automatically generated we normally don’t give any attention to this method.
Certain controls in Windows Forms have a property called UseCompatibleTextRendering property.These Controls in Windows Forms can render their text using either the TextRenderer class or the Graphics class.
TextRenderer class is GDI graphics library based and Graphics class is based on the GDI+ graphics library.
The method SetCompatibleTextRenderingDefault is used to set the default for the UseCompatibleTextRendering property of that controls across the application.
If the parameter value of the SetCompatibleTextRenderingDefault method is true, new controls that support UseCompatibleTextRendering use the GDI+ based Graphics class for text rendering.If the parameter value is false, new controls use the GDI based TextRenderer class.
SetCompatibleTextRenderingDefault is in System.Windows.Forms namespace from assembly System.Windows.Forms.dll
static class Program
{
/// <summary>
/// The main entry point for the C# application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
In .NET Framework version 1.0 and .NET Framework version 1.1 applications Windows Forms controls render their text using Graphics class which is based on GDI+ graphics.
GDI+ based graphics was having some performance and localization issues.
From .NET Framework version 2.0 onwards TextRenderer class got introduced which is based on GDI graphics library to overcome the performance and localization issues of GDI+ based text rendering.
Also, the text looks better in GDI based rendering and has improved support for international fonts.
Calculation of character spacing, word wrapping etc. are different in GDI based rendering and GDI+ based rendering.
By default, UseCompatibleTextRendering property takes the value false. But In a Windows Forms application which uses the Graphics class to render text and having certain controls that use TextRenderer class to render text to appear different from the other text in the application, then to overcome this incompatibility issue we can set the UseCompatibleTextRendering property to true.Otherwise, you don’t need to change the method call at all.
Hope this post helped you in understanding what is the relevance of UseCompatibleTextRendering property? Provide your queries and feedback about this UseCompatibleTextRendering property in the comments section below.
OWIN (Open Web Interface for .NET) is an interface between web servers and web applications…
JSON (JavaScript Object Notation) is a commonly used data exchange format that facilitates data exchange…
The CAP theorem is also known as Brewer's theorem. What is CAP Theorem? CAP theorem…
Some of the Key factors that need to consider while architecting or designing a software…
The Interface Segregation Principle (ISP) is one of the SOLID principles of object-oriented design. The…
The Single Responsibility Principle (SRP), also known as the Singularity Principle, is a software design…