473,383 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Invalidate() vs OnPaint()

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
Nov 17 '05 #1
3 17718
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.

"Chan" <11*@111.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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

Nov 17 '05 #2
Thank you very much ^.^
"Chan" <11*@111.com> ¦b¶l¥ó news:%2****************@TK2MSFTNGP14.phx.gbl ¤¤
¼¶¼g...
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

Nov 17 '05 #3
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.

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alexander Jhin | last post by:
I have this very basic Custom Control: public class TestPanel : Panel { public TestPanel() : base() { this.SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque |...
8
by: Mark Johnson | last post by:
Using: VS 2003 NET C# for Framework and Framework Compact Trying : Moving a Card (Bitmap) as in Solitare (PC + WinCe) Version on OnMouseMove Problem : The affected drawing Area by Invalidate (or...
3
by: Marge Inoferror | last post by:
I am trying to display a number of images and have them scale when the window is resized. I have all the code to draw the window in an OnPaint() override. However, it only draws correctly the...
3
by: jerry chapman | last post by:
Based on a mouse click I change something on the screen. I then want to repaint the screen to show the change. When I tried to use .Invalidate in my mouse routine, I got a compilation error. ...
3
by: RS | last post by:
if i want to invalidate the client area of my Windows.Form and execute an overrided OnPaint i would use Form1.ActiveForm.Invalidate() ?? However, when i use this line of code i receive a : An...
3
by: Colin McGuire | last post by:
Hi, I am just learning "how-to" by reading some of the existing article solutions in this newsgroup. I happened to come across this one. Tom Spink knows what he is talking about and I am wondering...
17
by: SamSpade | last post by:
picDocument is a picturebox When I do picDocument.Invalidate() the box paints. But if instead I do picDocument.Refresh() the box does not paint. What does Refresh do. I guessed it did an...
4
by: grayaii | last post by:
Hi, I have a simple form that handles all its paint functionality like so: this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true); And the entry point to this...
1
by: sean | last post by:
I'm trying to create "rubber-band" rectangles by overriding the OnPaint method to place rectangles on top of all graphic controls, but when I call Me.Invalidate() (when the user moves the mouse),...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.