Rectangle rect = new Rectangle(new Point(100, 100), new Size(100, 150));
        GraphicsPath gp = new GraphicsPath();
        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            X = e.X;
            Y = e.Y;
            Invalidate();
        }        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = panel1.CreateGraphics();
            g.SmoothingMode = SmoothingMode.HighQuality;
            gp.AddLine(new Point(10, 10), new Point(100, 100));            gp.AddLine(new Point(100, 100),new Point(150, 90));
            gp.AddLines(new Point[] { new Point(150, 70), new Point(100, 90), new Point(50, 160) });//(new Point(150, 90), new Size(120, 120), 0, 50);
            gp.CloseAllFigures();
            if (gp.IsVisible(X, Y))
            {
                g.FillPath(Brushes.Red, gp);
            }
            else
            {
                g.FillPath(Brushes.DeepSkyBlue, gp);
            }
            g.DrawPath(Pens.Blue, gp);
            g.DrawRectangle(Pens.Blue,rect);            if (rect.Contains(X, Y))
            {
                g.FillRectangle(Brushes.LavenderBlush, rect);
            }
            else
            {
                g.FillRectangle(Brushes.DarkCyan, rect);
            }
        }实在是闪得没办法了

解决方案 »

  1.   

      this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
                this.SetStyle(ControlStyles.DoubleBuffer, true);
      

  2.   

    你倒低是在this里画,还是在panel1里画???
      

  3.   

    如果你在this里画,在Paint事件里画就要用e.Graphics画,如果起用了this.DoubleBuffered=true则会避免闪烁.如查你在panel1里画,就在它的Paint事件里使用e.Graphis画,除非控件是ControlStyles.OptimizedDoubleBuffer设置为true,否则不能保证不闪烁。
      

  4.   

    Invalidate();的问题  当Invalidate()方法回执行 OnPaint 先去绘制一次..然后再执行你自己的绘制  private void panel1_MouseMove(object sender, MouseEventArgs e)
            {  X = e.X;
                Y = e.Y;
           Graphics g = panel1.CreateGraphics();
                g.SmoothingMode = SmoothingMode.HighQuality;
                gp.AddLine(new Point(10, 10), new Point(100, 100));            gp.AddLine(new Point(100, 100),new Point(150, 90));
                gp.AddLines(new Point[] { new Point(150, 70), new Point(100, 90), new Point(50, 160) });//(new Point(150, 90), new Size(120, 120), 0, 50);
                gp.CloseAllFigures();
                if (gp.IsVisible(X, Y))
                {
                    g.FillPath(Brushes.Red, gp);
                }
                else
                {
                    g.FillPath(Brushes.DeepSkyBlue, gp);
                }
                g.DrawPath(Pens.Blue, gp);
                g.DrawRectangle(Pens.Blue,rect);            if (rect.Contains(X, Y))
                {
                    g.FillRectangle(Brushes.LavenderBlush, rect);
                }
                else
                {
                    g.FillRectangle(Brushes.DarkCyan, rect);
                }
    }
      

  5.   

    直接画肯定闪的,而且不效率(每次Paint都要处理),可以用二级缓存,将图预先画到Image里,然后Paint的时候直接将Image Paint上去就行了
      

  6.   

    不要一看到重绘就想到onpaint,不要一需要重绘就用paint,能用背景图的尽量用背景,能放背景图的尽量用双缓冲绘制.
      

  7.   

    把代码放在paint事件里它就执行一次,然后就不变了我要在move里调用paint方法吗
      

  8.   

    Control.DoubleBuffered属性可以开启双缓冲,但是这是个受保护属性,需要在子类里调用。也就是说除非你的Panel是被重写的了且DoubleBuffered是True的才可以是双缓冲。如果是默认的控件,不是打开双缓冲的。如果这个Panel专用于绘图,不防从Control或UserControl或者Panel等控件继承下来一个新的控件,打开双缓冲专用来绘图。
      

  9.   

    1 改变算法,只是重绘上次和本次中不一致的区域,而不是全部重绘
    2 通过API获得graphics不使用onpaint事件。
      

  10.   

    这个好像是没有问题,但是不觉得move事件处理的东西太多了吗?我只是需要得到一个坐标
      

  11.   

    要重画是指在move里调用panal_paint事件吗
      

  12.   

    闪烁的原因就是你在involidate的时候重画Panle,而重画Panle要先画背景,,,,,
    不要用involidate自己写函数在需要时调用
      

  13.   


    pribate Graphics g = panel1.CreateGraphics();
    private void myPaint(){
                g.SmoothingMode = SmoothingMode.HighQuality;
                gp.AddLine(new Point(10, 10), new Point(100, 100));            gp.AddLine(new Point(100, 100),new Point(150, 90));
                gp.AddLines(new Point[] { new Point(150, 70), new Point(100, 90), new Point(50, 160) });//(new Point(150, 90), new Size(120, 120), 0, 50);
                gp.CloseAllFigures();
                if (gp.IsVisible(X, Y))
                {
                    g.FillPath(Brushes.Red, gp);
                }
                else
                {
                    g.FillPath(Brushes.DeepSkyBlue, gp);
                }
                g.DrawPath(Pens.Blue, gp);
                g.DrawRectangle(Pens.Blue,rect);            if (rect.Contains(X, Y))
                {
                    g.FillRectangle(Brushes.LavenderBlush, rect);
                }
                else
                {
                    g.FillRectangle(Brushes.DarkCyan, rect);
                }
    }private void panel1_MouseMove(object sender, MouseEventArgs e)
            {
                X = e.X;
                Y = e.Y;
                myPaint();
            }
    protected override void OnPaint(PaintEventArgs e)
            {
    myPaint();
    }
      

  14.   

    几年前毕业设计(Java的泡泡龙)时,我记得我老师说有个什么双缓冲技术,你查下这方面资料了,
    应该C#也可以解决.
      

  15.   

    要避免闪屏,就绘制成一个固定的图像,然后设置为其背景,这样就不需要时不时paint了.自然也就没闪屏了.
      

  16.   

    以下是两个关于双缓冲及闪烁的文章:
    你参考一下:http://blog.sina.com.cn/s/blog_59cea87f0100d7it.html
    http://blog.joycode.com/5drush/archive/2004/01/06/10701.joy
      

  17.   

     
    dylike(专注GDI+) 等 级:  
    恭喜了哈哈。。
      

  18.   

    先在bitmap 里画好,反正是要在某个对象中线画好,然后将图片往界面上贴,这就是双缓冲了。可以减少 UI的刷新而已