The Entry point method of C# Winform application looks as below,
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
Here [STAThread] attribute decoration is essential to mark your application state as single-threaded apartment style.For details of STAThread attribute read What STAThread Attribute does?
Also check what is SetCompatibleTextRenderingDefault(false) ?
Here we look into Application.EnableVisualStyles()
Table of Contents
What is EnableVisualStyles()?
Application.EnableVisualStyle() method empowers the app’s visual presentation with new visual appearance features of Operating systems such as Windows XP and later.. Colors, fonts, and other graphical elements come together to form a visual style for an OS. If both the control and the operating system permit it, controls will draw using visual styles.
Why Application.EnableVisualStyle Method Required?
This section explains about the need of Application.EnableVisualStyles() method. If you comment this line Application.EnableVisualStyles() and run the application you will see all the controls in your form are rendered with the classic Windows look only.
If you uncomment Application.EnableVisualStyles() and run the application you can see all your controls rendered with current operating system theme settings.
Means, If your operating system is Windows it will use the built-in Windows theming to style controls and classic Windows look and feel if EnableVisualStyle() is commented.
So it is not a method which if commented will cause the application to crash or stop working. It is essential to apply the visual styles like colors, fonts, and other visual elements to your form controls from the current operating system theme. If the control and the operating system support this controls will draw with visual styles if this method is used.
Also Application.EnableVisualStyles() method must be called as the first line in the Main method before calling any controls in the application to have the effect.
Summary
This post covered the relevance of Application.EnableVisualStyles() method. Application.EnableVisualStyles() must be called as the first line in Main() before invoking any controls.Your comments and feedback are valuable for us. So please provide your suggestions in the comments section below.
What is Application.SetCompatibleTextRenderingDefault(false)?
Leave a Reply