473,805 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 17849
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******** ********@TK2MSF TNGP14.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******** ********@TK2MSF TNGP14.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
1736
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 | ControlStyles.UserPaint, true);
8
12283
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 Invalidate(Rectangle)) flickers in a nasty way when repainting. This does not happen in the Solitare (PC + WinCe) Versions a well as in a Card game where I have the C++ Source. The use of an empty OnPaintBackground brings no visable results.
3
3786
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 first time it is displayed. When I resize the window, it keeps the remnants of the first window and redraws only in "new" areas of the window. Is there a way to invalidate the entire window when OnPaint is invoked? I
3
7524
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. Here is my OnPaint code: protected override void OnPaint( PaintEventArgs paintEvent ) { Graphics g = paintEvent.Graphics;
3
3432
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 unhandled exception of type 'System.NullReferenceException' occurred in OCRTicker.exe Additional information: Object reference not set to an instance of an object.
3
6200
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 why he has done something. My question is how does this work <pseudo> Function To Override OnPaint(...) Call Base Class OnPaint(...)
17
8556
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 Invalidate and an Update. Can someone shed some light?
4
4641
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 program is like so: static void Main() {
1
3618
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), OnPaint is not getting called... Here is the relevent code: I'm trying to create a "rubber band" rectangle effect on my form. Private Sub HighlightSectionOfTrack(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles...
0
9718
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10614
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10109
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3847
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.