Application.EnableVisualStyle
启用应用程序的可视样式。
命名空间: System.Windows.Forms
程序集: System.Windows.Forms(位于 System.Windows.Forms.dll)
语法:
public static void EnableVisualStyles()
This method enables visual styles for the application. Visual styles are the colors, fonts, and other visual elements that form an operating system theme. Controls will draw with visual styles if the control and the operating system support it. To have an effect, M:System.Windows.Forms.Application.EnableVisualStyles must be called before creating any controls in the application; typically, M:System.Windows.Forms.Application.EnableVisualStyles is the first line in the Main function. A separate manifest is not required to enable visual styles when calling M:System.Windows.Forms.Application.EnableVisualStyles.
Prior to the .NET Framework 2.0, the FlatStyle property of some controls, such as controls that derive from T:System.Windows.Forms.ButtonBase, had to be set to F:System.Windows.Forms.FlatStyle.System in order for the controls to be drawn with visual styles. In applications written with the .NET Framework 2.0, this is no longer necessary.
This method will have no effect for controls hosted in Internet Explorer.
WinXpFamily
Visual styles are only supported on these platforms.
The following code example demonstrates calling M:System.Windows.Forms.Application.EnableVisualStyles in the Main function to enable visual styles for the application.
using System; using System.Drawing; using System.Windows.Forms; namespace VStyles { public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); } public Form1() { this.button1 = new System.Windows.Forms.Button(); this.button1.Location = new System.Drawing.Point(24, 16); this.button1.Size = new System.Drawing.Size(120, 100); this.button1.FlatStyle = FlatStyle.System; this.button1.Text = "I am themed."; // Sets up how the form should be displayed and adds the controls to the form. this.ClientSize = new System.Drawing.Size(300, 286); this.Controls.Add(this.button1); this.Text = "Application.EnableVisualStyles Example"; } } }
**********************************************
Application.SetCompatibleTextRenderingDefault(Boolean)方法
将某些控件上定义的 UseCompatibleTextRendering 属性设置为应用程序范围内的默认值。
命名空间: System.Windows.Forms
程序集: System.Windows.Forms(位于 System.Windows.Forms.dll)
语法:
public static void SetCompatibleTextRenderingDefault( bool defaultValue )
参数
defaultValue
Type: System.Boolean
用于新控件的默认值。 如果为 true,则支持 UseCompatibleTextRendering 的新控件使用基于 GDI+ 的 Graphics 类进行文本呈现;如果为 false,则新控件使用基于 GDI 的 TextRenderer 类。
Exception | Condition |
---|---|
InvalidOperationException | Windows 窗体应用程序创建的第一个窗口之前,仅可以调用此方法。 |
某些 Windows 窗体控件可以呈现使用其文本 TextRenderer 类,该类基于 GDI 图形库或 Graphics 类,该类基于 GDI+ 图形库。 此更改为了 .NET Framework 2.0 由于性能和本地化问题 GDI+。 使用 SetCompatibleTextRenderingDefault 若要设置的默认值 UseCompatibleTextRendering 支持它的控件的属性。
UseCompatibleTextRendering 属性旨在提供 Windows 窗体控件之间的可视化兼容性,呈现文本使用 TextRenderer 类和 .NET Framework 1.0 和 .NET Framework 1.1 执行自定义文本呈现使用的应用程序 Graphics 类。 在大多数情况下,如果您的应用程序未从升级 .NET Framework 1.0 或 .NET Framework 1.1, ,建议您保留 UseCompatibleTextRendering 设置的默认值为 false。
GDI 基于 TextRenderer 类中引入了 .NET Framework 2.0 为了提高性能,使文本看起来不错,和加强对国际字体支持。 在早期版本的 .NET Framework, 、 GDI+ 基于 Graphics 类用于执行所有文本呈现。GDI 计算字符间距和自动换行中的不同 GDI+。 在 Windows 窗体应用程序中使用 Graphics 类来呈现文本,这可能导致使用的控件的文本 TextRenderer 才会显示不同的应用程序中的其他文本。 若要解决此不兼容性,您可以设置 UseCompatibleTextRendering 属性设置为 true。 若要设置 UseCompatibleTextRendering 到 true 对于应用程序中所有支持的控件,调用 SetCompatibleTextRenderingDefault 方法的参数替换 true。
如果在另一个应用程序,如 Internet Explorer 中承载 Windows 窗体代码,您应该永远不会调用此方法。 仅在独立的 Windows 窗体应用程序中调用此方法。
![]() |
---|
若要设置的默认值为 UseCompatibleTextRendering 中 Visual Basic 2005 或更高版本,请参阅 WindowsFormsApplicationBase.UseCompatibleTextRendering。 |
在 Visual C# 2005 或更高版本,对的调用 SetCompatibleTextRenderingDefault 在 Program.cs 文件中自动生成。 若要更改文本呈现默认值,修改所生成的代码。
static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } }
*****************************************************
转载于:https://blog.51cto.com/jiaojusuimu/1881263