ControlStyles.OptimizedDoubleBuffer increases my flicker problems
-
I'm new to form programming in C#. Just for learning purposes, I tried double buffering to get rid of my flicker problems with a simple test program. The problem is, that enabling double buffering made increased my flicker exponentially. What should I do to solve this? Here's the code for reference
public partial class DoubleBufferPractice : Form { Timer timer; Rectangle rect; public DoubleBufferPractice() { InitializeComponent(); this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.UserPaint, true); rect = new Rectangle(0, this.Height/3, 50, 50); InitializeTimer(); } private void InitializeTimer() { timer = new Timer(); timer.Interval = 10; timer.Tick += new EventHandler(timer_Tick); timer.Start(); } private void timer_Tick(object sender, EventArgs e) { if(rect.X + rect.Width < this.Width) rect.X += 10; else rect.X = 0; this.Invalidate(); } protected override void OnPaint(PaintEventArgs e) { using(Graphics g = this.CreateGraphics()) { using(Brush brush = new SolidBrush(Color.Magenta)) { g.FillRectangle(brush, rect); } } base.OnPaint(e); } }
Also, I'm testing this on my laptop with a 2.0ghz Dual Core Pentium, 2gigs ram, and a 256meg nonshared vram NVidia Geforce 8600 M GT, so I don't think that hardware is the problem.
-
I'm new to form programming in C#. Just for learning purposes, I tried double buffering to get rid of my flicker problems with a simple test program. The problem is, that enabling double buffering made increased my flicker exponentially. What should I do to solve this? Here's the code for reference
public partial class DoubleBufferPractice : Form { Timer timer; Rectangle rect; public DoubleBufferPractice() { InitializeComponent(); this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.UserPaint, true); rect = new Rectangle(0, this.Height/3, 50, 50); InitializeTimer(); } private void InitializeTimer() { timer = new Timer(); timer.Interval = 10; timer.Tick += new EventHandler(timer_Tick); timer.Start(); } private void timer_Tick(object sender, EventArgs e) { if(rect.X + rect.Width < this.Width) rect.X += 10; else rect.X = 0; this.Invalidate(); } protected override void OnPaint(PaintEventArgs e) { using(Graphics g = this.CreateGraphics()) { using(Brush brush = new SolidBrush(Color.Magenta)) { g.FillRectangle(brush, rect); } } base.OnPaint(e); } }
Also, I'm testing this on my laptop with a 2.0ghz Dual Core Pentium, 2gigs ram, and a 256meg nonshared vram NVidia Geforce 8600 M GT, so I don't think that hardware is the problem.
Hi, first of all, do NOT you this.CreateGraphics(). Just use e.Graphics available from parameter like this:
protected override void OnPaint(PaintEventArgs e) { //using(Graphics g = this.CreateGraphics()) //{ using(Brush brush = new SolidBrush(Color.Magenta)) { e.Graphics.FillRectangle(brush, rect); } //} base.OnPaint(e); }
second of all, interval is in miliseconds so thistimer.Interval = 10;
will redraw whole form 100 times per second. It may flicker even on good computer. hth -
Hi, first of all, do NOT you this.CreateGraphics(). Just use e.Graphics available from parameter like this:
protected override void OnPaint(PaintEventArgs e) { //using(Graphics g = this.CreateGraphics()) //{ using(Brush brush = new SolidBrush(Color.Magenta)) { e.Graphics.FillRectangle(brush, rect); } //} base.OnPaint(e); }
second of all, interval is in miliseconds so thistimer.Interval = 10;
will redraw whole form 100 times per second. It may flicker even on good computer. hthThank you so much. changing it to e.Graphics fixed everything. Also, it went from extreme flicker (practically disappearing for a full second or two) to incredibly minor flicker at a 10ms interval.