关于C#双缓存绘图有残留的问题

.Net技术 码拜 9年前 (2016-05-06) 1446次浏览
本人想做一个自定义控件,使用双缓存绘制一条可以在刻度中移动的水平直线,定义了一个方法Usepaint来实现,使用定时器测试时(设置100ms),控件显示会出现这种问题,怎么解决,专家们给点解决方法吧,
关于C#双缓存绘图有残留的问题
而本人想要的是
关于C#双缓存绘图有残留的问题

public void Usepaint(double jd)
        {
            Bitmap bmp = new Bitmap(100, 100);//构造一块虚拟画布
            Graphics g= Graphics.FromImage(bmp);//获得虚拟画布的设备
            float cw = 100;//控件的大小
            float ch = 100;
            float L = 100;
            float N = 8;
            this.Width = (int)cw;
            this.Height = (int)ch;
            Pen pen = new Pen(Color.MidnightBlue, 3);//画标尺线的笔
            Point point1 = new Point(40, 0);
            Point point2 = new Point(40, 100);
            Point point3 = new Point(60, 0);
            Point point4 = new Point(60, 100);
            g.DrawLine(pen, point1, point2);
            g.DrawLine(pen, point3, point4);
            PointF pointx = new PointF();
            PointF pointy = new PointF();
            for (float i = 0; i < N + 1; i++)//画刻度
            {
                if (!(i == 0 || i == N))
                {
                    pointx.X = 40;
                    pointx.Y = 0 + L / N * i;
                    pointy.X = 35;
                    pointy.Y = 0 + L / N * i;
                    g.DrawLine(pen, pointx, pointy);
                    pointx.X = 60;
                    pointx.Y = 0 + L / N * i;
                    pointy.X = 65;
                    pointy.Y = 0 + L / N * i;
                    g.DrawLine(pen, pointx, pointy);
                }
                else
                {
                    pointx.X = 40;
                    pointx.Y = 0 + L / N * i;
                    pointy.X = 30;
                    pointy.Y = 0 + L / N * i;
                    g.DrawLine(pen, pointx, pointy);
                    pointx.X = 60;
                    pointx.Y = 0 + L / N * i;
                    pointy.X = 70;
                    pointy.Y = 0 + L / N * i;
                    g.DrawLine(pen, pointx, pointy);
                }
            }
            PointF point11 = new PointF();//画指示标尺
            PointF point22 = new PointF();
            if (jd>=0)
            {
                point11.X = 45;
                point11.Y = (float)(50 - 50 * jd / 90);
                point22.X = 55;
                point22.Y = (float)(50 - 50 * jd / 90);
            }
            else
            {
                point11.X = 45;
                point11.Y = (float)(50 + 50 * Math.Abs(jd) / 90);
                point22.X = 55;
                point22.Y = (float)(50 + 50 * Math.Abs(jd) / 90);
            }
            Pen pen0 = new Pen(Color.Black);
            g.DrawLine(pen0, point11, point22);
            Font F = new Font("System", 9);
            g.DrawString("90", F, Brushes.Black, 10, 0);
            g.DrawString("90", F, Brushes.Black, 68, 0);
            g.DrawString("0", F, Brushes.Black, 15, 42);
            g.DrawString("0", F, Brushes.Black, 73, 42);
            g.DrawString("-90", F, Brushes.Black, 7, 86);
            g.DrawString("-90", F, Brushes.Black, 63, 86); 
            this.CreateGraphics().DrawImage(bmp, 0, 0);
            bmp.Dispose();    
        }
private void timer1_Tick(object sender, EventArgs e)
        {
           pitching++;
            Usepaint(pitching); 
        }
解决方案

20

可以将Usepaint(pitching)放到窗体的Paint事件中,定时时间到则用this.Invalidate()来重绘窗体内容

20

引用:

可以将Usepaint(pitching)放到窗体的Paint事件中,定时时间到则用this.Invalidate()来重绘窗体内容

是的,你应该是没有在Paint中绘制,这样以前画的没有清除。你应该Paint中实现绘图,然后在计时器中pitching++,然后通过Invalidate触发重绘。Invalidate有一个带参数的重载,表示重绘指定区域,你调用那个会效率更高些。


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于C#双缓存绘图有残留的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)