a lot of flickering


I found a major problem in this, it is flickering a lot eventhough it uses double buffering. After a lot of search I came to a conclusion that, this is may be due to calling of OnPaintBackground (EraseBackground event) function frequently and it is not a good idea using CreateGraphics inside the Paint Event. Hence I made two changes in the code. First I made changes in the vectShapes constructor as below ie added SetStyle
InitializeComponent();myInit();this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true); // added line
After that I added new function redraw(Graphics g,bool All) by exactly copying the redraw(bool All) function and commenting two lines Graphics g = this.CreateGraphics(); and g.Dispose();
public void redraw(Graphics g, bool All) { /* int startTime = System.DateTime.Now.Second * 10000 + System.DateTime.Now.Millisecond; System.Console.WriteLine("startTime: {0}", startTime.ToString()); */ //All=true : redraw all graphic //All=false : redraw only selected objects //TEST if (fit2grid & this.gridSize > 0) { this.startX = this.gridSize * (int)(startX / this.gridSize); this.startY = this.gridSize * (int)(startY / this.gridSize); } //Graphics g = this.CreateGraphics(); .... .... //g.Dispose(); /* int endTime = System.DateTime.Now.Second * 10000 + System.DateTime.Now.Millisecond; System.Console.WriteLine("endTime: {0}", endTime.ToString()); */ }
and changed the UserControl1_Paint function as follows
private void UserControl1_Paint(object sender, PaintEventArgs e) { this.redraw(e.Graphics,false); }
Closed Dec 15, 2010 at 10:40 AM by FrankVerheyen
hi Christian,the redrawing does not use a self-made memory buffer anymore, like it used to. In .Net this is not necessary anymore, since we have double buffering support etc.Also, with the SetStyle() call, this is all optimized, etc. I changed the drawing routines, and it should be pretty much optimal and nicely .Net compliant now.cheers!frank... closed the work item