zoukankan      html  css  js  c++  java
  • winform控件大小改变是防止背景重绘导致的闪烁(转载)

    在工作中需要做一个伸缩控件,这个自定义控件继承于Panel。这个伸缩控件分为两个部分,头部是一个自定义组件,伸缩控件的背景为灰色,头部背景要求白色。伸缩控件在点击按钮时会重绘,同时他内部的控件也会重绘,这样就导致伸缩时界面会闪烁。

    设置双缓存不能满足要求。

    有一个解决方案的思路是使得某个控件的绘制背景图方法(OnPaintBackground方法)直接放回,不调用基类的该方法,同时重写绘制方法(OnPaint方法)用图片一次性绘制到背景。要求设置背景图片。

    /// <summary>
    /// 加强版 Panel
    /// </summary>
    class PanelEnhanced : Panel
    {
        /// <summary>
        /// OnPaintBackground 事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            // 重载基类的背景擦除函数,
            // 解决窗口刷新,放大,图像闪烁
            return;
        }
    
        /// <summary>
        /// OnPaint 事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            // 使用双缓冲
            this.DoubleBuffered = true;
            // 背景重绘移动到此
            if (this.BackgroundImage != null)
            {
                e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                e.Graphics.DrawImage(
                    this.BackgroundImage,
                    new System.Drawing.Rectangle(0, 0, this.Width, this.Height),
                    0,
                    0,
                    this.BackgroundImage.Width,
                    this.BackgroundImage.Height,
                    System.Drawing.GraphicsUnit.Pixel);
            }
            base.OnPaint(e);
        }
    }
    

      

    基于这个思路,如果控件的背景是纯色,可以自己绘制一个位图作为背景图片,进行填充

     protected override void OnPaintBackground(PaintEventArgs e)
            {
                // 基类的方法不能用, 防止点击伸缩时重绘背景导致闪烁
                return;
            }
    
            protected override void OnPaint(PaintEventArgs e)
            {
                /* 创建一个白色背景的位图作为背景,防止点击伸缩时重绘背景导致闪烁。
                 * 不可在初始化的时候绘制该背景位图,
                 * 因为容器自适应于父控件的长宽,初始化时候的长宽不一定是最终长宽。
                 * 所以每次触发绘制方法的时候都要重新读取控件的长宽以便重绘位图
                 */
                Bitmap bmp = new Bitmap(this.Width, this.Height);
                using (Graphics graph = Graphics.FromImage(bmp))
                {
                    Rectangle ImageSize = new Rectangle(0, 0, this.Width, this.Height);
                    graph.FillRectangle(Brushes.White, ImageSize);
                }
    
                e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                e.Graphics.DrawImage(bmp, new System.Drawing.Rectangle(0, 0, this.Width, this.Height),
                    0, 0, this.Width, this.Height,
                    System.Drawing.GraphicsUnit.Pixel);
    
                ////基类的OnPaint方法不能使用,防止点击伸缩时重绘背景导致闪烁
                //base.OnPaint(e);
            }
    

      

     本文转载:http://www.cnblogs.com/firstdown/p/6420534.html

  • 相关阅读:
    4G DTU在城市景观照明中的应用解决方案
    物联网在物业管理和智慧楼宇中的应用解决方案
    4G工业路由器等物联网设备在食品安全检测中的应用
    NB-IoT网络在农业和畜牧业中的物联网智能灌溉应用案例
    串口服务器等应用于污水处理厂的自动监控和控制管理
    插卡式双卡4G工业路由器在数控机床远程控制中的应用
    4G工业路由器在水电站远程监控中的应用案例
    4G工业路由器在供水系统和道路交通远程检测中的应用案例
    HDU 6188 Duizi and Shunzi 贪心
    HDU 6185 Covering 矩阵快速幂
  • 原文地址:https://www.cnblogs.com/xqaizx/p/6598132.html
Copyright © 2011-2022 走看看