当前位置: 代码迷 >> C# >> 重绘控件时的奇怪状况
  详细解决方案

重绘控件时的奇怪状况

热度:383   发布时间:2016-05-05 05:34:03.0
重绘控件时的奇怪情况
我自己写了一个滚动字的控件,OnPaint方法是这样写的:

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {           
                base.OnPaint(e);
                Graphics g = e.Graphics;
                s = g.MeasureString(this.Content, TextFont);//测量文字长度  

                Brush brush = new SolidBrush(TextColor);
                //g.Clear(Color.Transparent);//清除背景  
                g.DrawString(this.Content, TextFont, brush, p);
                this.Invalidate(e.ClipRectangle);
                //绘图
                
         }

可是运行后发现,加载了这个控件的panel下面的panel不见了。经调试发现是因为 this.Invalidate(e.ClipRectangle);造成的,求解答
------解决思路----------------------
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TestWeldSymbolUI
{
    public class myPanel : Panel
    {
        private ImageList imgs = new ImageList();
        // constructor
        public myPanel()
        {
            // set draw mode to owner draw
            //this.DrawMode = DrawMode.OwnerDrawFixed; 
        }

        PointF p = new PointF();
        Font TextFont = new Font("宋体", 16);

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            string Content = "这就是个测试";
            SizeF s = g.MeasureString(Content, TextFont);//测量文字长度  

            Brush brush = new SolidBrush(Color.Blue);
            //g.Clear(Color.Transparent);//清除背景  
            g.DrawString(Content, TextFont, brush, p);
            this.Invalidate(e.ClipRectangle);

            //绘图
            p.X = p.X + 1;
            if (p.X > this.Width)
            {
                p.X = 0;
            }
            System.Threading.Thread.Sleep(20);

        }

    }
}
 
  相关解决方案