GDI+自定义控件解决闪烁问题

/// <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();

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值