screen flickering

B

benben

I created a form and overrided OnPaint, OnClick and OnResize methods. My
OnPaint calls base.OnPaint then repaints the whole screen.

The screen flickers a lot! It didn't happen when the app was written in C++.

What is the general strategy to reduce screen flickering with C# Forms?
Perhaps saving the screen as a bitmap and just bitblip when painted, and
only change the bitmap when necessary? is it possible?

ben
 
V

Vijaye Raji

Try this in your Form's constructor:

this.SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer, true);

-vJ
 
B

benben

What does it buffer?

Plus, I do not use standard controls in this project, instead I paint
directly to the form using GDI+.

ben
 
S

Sean Hederman

benben said:
What does it buffer?

Instead of drawing directly onto the screen, it buffers the image to an
in-memory bitmap, and then blits the bitmap onto the screen. This does
reduce flicker, although your problem might also be caused by the parent
classes OnPaintBackground implementation. You can override this, and not do
anything in it, or you could set the control Style AllPaintingInWmPaint. I
personally prefer this method, in fact, I usually set the following:

SetStyle(ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint,
ControlStyles.UserPaint, true);

HTH
 
B

benben

I don't think I can do that to my user even if it is applicable. but thanks
anyway for your suggestion!

ben
 
B

benben

Thanks Sean!! The problem does improved with your suggestion!!

Just for a matter of interest: if the form buffers the screen, how can I get
access to this bitmap?

Another question: I have been using the Graphics object as the painting
surface. Instead of a Graphics object associated with the actual screen, can
i create other Graphics objects that are associated to 1. a printing surface
2. a bitmap image?

ben
 
F

Frank Hileman

Yes, you can do that. You can use the same code for both. If you want to get
the buffer, it is best to do the double buffering yourself, writing to a
Graphics created from a bitmap, and transferring the bitmap to the
paint-provided Graphics when done.

Regards,
Frank Hileman

check out VG.net: http://www.vgdotnet.com
Animated vector graphics system
Integrated Visual Studio .NET graphics editor
 
B

benben

I found a trick!

In my original tests I used newly created Graphics objects, e.g.

Graphics paintSurface = Graphics.FromHwnd(this.Handle);

And there was a lot of screen flickering.

Once I found that there is already a Graphics object in the PaintEventArgs
parameter passed into my OnPaint, I started to use it and the flickering
disappeared!

Why does creating a new Graphics object out of the window handle create such
an overhead?

ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top