C# 控件双缓冲控制 ControlStyles 枚举详解

ControlStyles 枚举
.NET Framework 4
  

指定控件的样式和行为。

此枚举有一个 FlagsAttribute 特性,通过该特性可使其成员值按位组合。

 

命名空间:  System.Windows.Forms
程序集:  System.Windows.Forms(在 System.Windows.Forms.dll 中)
C# 
[FlagsAttribute]
public enum ControlStyles
成员名称说明
ContainerControl 如果为 true,则控件是类似容器的控件。
UserPaint 如果为 true,控件将自行绘制,而不是通过操作系统来绘制。 如果为 false,将不会引发 Paint 事件。 此样式仅适用于派生自 Control 的类。
Opaque 如果为 true,则控件被绘制为不透明的,不绘制背景。
ResizeRedraw 如果为 true,则在调整控件大小时重绘控件。
FixedWidth 如果为 true,则自动缩放时,控件具有固定宽度。 例如,如果布局操作尝试重新缩放控件以适应新的Font,则控件的 Width 将保持不变。
FixedHeight 如果为 true,则自动缩放时,控件具有固定高度。 例如,如果布局操作尝试重新缩放控件以适应新的Font,则控件的 Height 将保持不变。
StandardClick 如果为 true,则控件将实现标准 Click 行为。
Selectable 如果为 true,则控件可以接收焦点。
UserMouse 如果为 true,则控件完成自己的鼠标处理,因而鼠标事件不由操作系统处理。
SupportsTransparentBackColor 如果为 true,控件接受 alpha 组件小于 255 的 BackColor 以模拟透明。 仅在 UserPaint 位设置为 true并且父控件派生自 Control 时才模拟透明。
StandardDoubleClick 如果为 true,则控件将实现标准 DoubleClick 行为。 如果 StandardClick 位未设置为 true,则忽略此样式。
AllPaintingInWmPaint 如果为 true,控件将忽略 WM_ERASEBKGND 窗口消息以减少闪烁。 仅当 UserPaint 位设置为 true 时,才应当应用该样式。
CacheText 如果为 true,控件保留文本的副本,而不是在每次需要时从 Handle 获取文本副本。 此样式默认为false。 此行为提高了性能,但使保持文本同步变得困难。
EnableNotifyMessage 如果为 true,则为发送到控件的 WndProc 的每条消息调用 OnNotifyMessage 方法。 此样式默认为false。 EnableNotifyMessage 在部分可信的情况下不工作。
DoubleBuffer 如果为 true,则绘制在缓冲区中进行,完成后将结果输出到屏幕上。 双重缓冲区可防止由控件重绘引起的闪烁。 如果将 DoubleBuffer 设置为 true,则还应当将 UserPaint 和 AllPaintingInWmPaint 设置为true。
OptimizedDoubleBuffer 如果为 true,则该控件首先在缓冲区中绘制,而不是直接绘制到屏幕上,这样可以减少闪烁。 如果将此属性设置为 true,则还应当将 AllPaintingInWmPaint 设置为 true。
UseTextForAccessibility 指定该控件的 Text 属性的值,如果已设置,则可确定该控件的默认 Active Accessibility 名称和快捷键。

控件在各种属性和方法中使用此枚举指定功能。 控件可以通过调用 SetStyle 方法并传入适当的 ControlStyles 位以及设置该位的 Boolean 值来启用样式。 例如,下面的一行 Visual Basic 代码将会启用双重缓冲。

 myControl.SetStyle(UserPaint Or AllPaintingInWmPaint Or DoubleBuffer, True)

如果将 AllPaintingInWmPaint 位设置为 true,则将忽略 WM_ERASEBKGND 窗口消息,而直接从 WM_PAINT 窗口消息调用OnPaintBackground 和 OnPaint 方法。 这通常可减少闪烁,除非其他控件将 WM_ERASEBKGND 窗口消息发送到该控件。 可以发送 WM_ERASEBKGRND 窗口消息以达到与 SupportsTransparentBackColor 相似的假透明效果;例如,具有平面外观的 ToolBar 就采用这种方法。

若要完全启用双缓冲,可以将 OptimizedDoubleBuffer 和 AllPaintingInWmPaint 位设置为 true。 但是,启用双缓冲的首选方法是将该控件的DoubleBuffered 属性设置为 true,这会产生同样的结果。

如果 SupportsTransparentBackColor 位设置为 true,并且 BackColor 被设置为 alpha 组件小于 255 的颜色,则 OnPaintBackground 将通过请求其父控件绘制背景来模拟透明。 但这不是真正的透明。

注意注意

如果在控件与其父控件之间还有另一个控件,则当前控件不会显示中间的控件。

当 UserMouse 位设置为 true 时,仍将调用以下方法:Control.OnMouseDownControl.OnMouseUpControl.OnMouseEnterControl.OnMouseMoveControl.OnMouseHoverControl.OnMouseLeave 和 Control.OnMouseWheel

单击控件时,如果 StandardClick 位设置为 true,则 Control.OnClick 方法被调用,它将引发 Control.Click 事件。 双击控件并且StandardClick 和 StandardDoubleClick 位都设置为 true 时,会将此次单击传递给 DoubleClick 事件。 随后,Control.OnDoubleClick 方法被调用,此方法将引发 Control.DoubleClick 事件。 然而,无论 StandardClick 和 StandardDoubleClick 位为何值,控件都可直接调用 OnClick或 OnDoubleClick。 有关控件单击和双击行为的更多信息,请参见 Control.Click 和 Control.DoubleClick 这两个主题。

当已设置 UseTextForAccessibility 位和该控件的 Text 属性值时,该控件的 Text 属性值可确定控件的默认的 Active Accessibility 名称和快捷键。 否则,将改用前面的 Label 控件的文本。 此样式为默认设置。 某些内置控件类型(如 TextBox 和 ComboBox)会重置此样式,因此 Active Accessibility 不会使用那些控件的 Text 属性。

对继承者的说明

如果该控件不支持 Click 或 DoubleClick 事件,那么从标准 Windows 窗体控件继承并将 StandardClick 或 StandardDoubleClick 位值更改为true 会导致意外的行为,或者根本不会产生任何效果。

下面的示例演示如何通过 StyleChanged 事件使用 ControlStyles。

// Set the 'FixedHeight' and 'FixedWidth' styles to false.
private void MyForm_Load(object sender, EventArgs e)
{
   this.SetStyle(ControlStyles.FixedHeight, false);
   this.SetStyle(ControlStyles.FixedWidth, false);
}

private void RegisterEventHandler()
{
   this.StyleChanged += new EventHandler(MyForm_StyleChanged);
}

// Handle the 'StyleChanged' event for the 'Form'.
private void MyForm_StyleChanged(object sender, EventArgs e)
{
   MessageBox.Show("The style releated to the 'Form' has been changed");
}
posted @ 2016-07-19 11:31  龙骑科技  阅读(3816)  评论(0编辑  收藏  举报