Invalidate() vs OnPaint()

C

Chan

Invalidate() is a member of System.Windows.Forms.Form. It marks an area of
the client window as invalid and, therefore, in need of repainting, and then
makes sure a Paint event is raised.

But why we are doing it this way. If we know that something needs painting,
why don't
we just call OnPaint() or some other method to do the painting directly?

Thank you very much
 
B

Bob Powell [MVP]

The OnPaint method is run when the application recieves a WM_PAINT message.
The WM_PAINT message is sent when the application has invalid rectangles and
the application is about to enter an idle state.

Calling OnPaint directly may cause more painting than is needed and might be
completely useless because you'd still have to call BeginPaint and EndPaint
to clear the invalid rectangles out of the current list.

Call Invalidate with or without specifying the rectangles and allow the
system to do it's job without upsetting the flow.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
S

stork

A window can have more than one invalidated area, and multiple
invalidated areas get handled by a single call to OnPaint via the
framework. Think of Invalidate as a queue operation, and the system
periodically generates OnPaints (via WM_PAINT), to flush it.
 

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