/// <summary>
/// 自定义双缓存控件
/// </summary>
namespace APS.CustomControls
{
/// <summary>
/// 双缓存面板
/// </summary>
public class DoubleBufferdPanel : Panel
{
public DoubleBufferdPanel() : base()
{
base.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
base.UpdateStyles();
}
}
/// <summary>
/// 双缓存图片
/// </summary>
public class DoubleBufferdPictureBox : PictureBox
{
public DoubleBufferdPictureBox() : base()
{
base.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
base.UpdateStyles();
}
}
/// <summary>
/// 双缓存用户控件
/// </summary>
public class DoubleBufferdControl : UserControl
{
public DoubleBufferdControl() : base()
{
base.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
base.UpdateStyles();
}
}
}
项目中,控件在大小变化时会有闪烁现象。
以双缓冲技术解决。
public Form1()
{
InitializeComponent();
//this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
//this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
//this.SetStyle(ControlStyles.ResizeRedraw, true);
//this.UpdateStyles();
this.SetStyle(ControlStyles.OptimizedDoubleBuffer
| ControlStyles.ResizeRedraw
| ControlStyles.Selectable
| ControlStyles.AllPaintingInWmPaint
| ControlStyles.UserPaint
| ControlStyles.SupportsTransparentBackColor,
true);
//或:
this.SetStyle(ControlStyles.UserPaint, true);//自绘
this.SetStyle(ControlStyles.DoubleBuffer, true);// 双缓冲
this.SetStyle(ControlStyles.ResizeRedraw, true);//调整大小时重绘
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);// 双缓冲
this.SetStyle(ControlStyles.SupportsTransparentBackColor, 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 名称和快捷键。 |
Reference: MSDN
this.SetStyle(
ControlStyles.UserPaint//使用自定义的绘制方式
|ControlStyles.ResizeRedraw//当控件大小发生变化时就重新绘制
|ControlStyles.SupportsTransparentBackColor//则控件接受 alpha 组件数小于 255 个的 BackColor 来模拟透明度
| ControlStyles.AllPaintingInWmPaint//则控件忽略窗口消息 WM_ERASEBKGND 以减少闪烁
| ControlStyles.OptimizedDoubleBuffer//则控件将首先绘制到缓冲区而不是直接绘制到屏幕,这可以减少闪烁
, true);
this.UpdateStyles();