how to stop flickering C# winforms

前端 未结 16 2378
故里飘歌
故里飘歌 2020-11-28 04:16

I have a program that is essentially like a paint application. However, my program has some flickering issues. I have the following line in my code (which should get rid of

16条回答
  •  半阙折子戏
    2020-11-28 04:35

    here is the program of moving circle in .net, that doesn't flicker.

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Threading;
    namespace CircleMove
    {
        /// 
        /// Description of MainForm.
        /// 
        public partial class MainForm : Form
        {
            int x=0,y=0;
            Thread t;
    
            public MainForm()
            {
    
                //
                // The InitializeComponent() call is required for Windows Forms designer support.
                //
                InitializeComponent();
    
                //
                // TODO: Add constructor code after the InitializeComponent() call.
                //
            }
            void MainFormPaint(object sender, PaintEventArgs e)
            {
                Graphics g=e.Graphics;
                Pen p=new Pen(Color.Orange);
                Brush b=new SolidBrush(Color.Red);
            //  g.FillRectangle(b,0,0,100,100);
                g.FillEllipse(b,x,y,100,100);
            }
            void MainFormLoad(object sender, EventArgs e)
            {
                t=new Thread(  new ThreadStart(
    
                    ()=>{
                        while(true)
                        {
                            Thread.Sleep(10);
                            x++;y++;
                            this.Invoke(new Action(
                                ()=>{
    
                                    this.Refresh();
                                    this.Invalidate();
                                    this.DoubleBuffered=true;
                                    }
                                                )
                                            );
                        }
                        }
                                                )
    
                            );
    
                t.Start();
            }
        }
    }
    

提交回复
热议问题