Click to See Complete Forum and Search --> : What exactly is happening in .Net when a form needs to be painted?


Mancora
07-10-2005, 09:44 PM
Ok, so i understand the point where the OnPaint method inside the form is called and raises the event by invoking the delegates registered with it, but i dont get what happens before that.

I would have been tempted to say painting the background and getting components to paint themselves would have been one of the delegates registered with it, but i can tell thats not the case because i overrode the OnPaint(...) method to do nothing and the form's background still painted fine.


Unlike my last posts i've actually been looking for this for a while, saturday night most of today today and been trying to dig up info at msdn.


I've found info talking about win32 and COM stuff (or at least thats where msdn puts it when i cross reference with them), that usually goes like this.

The prime suspect is usually the WM_ERASEBKGND message. This message is sent to a window when it's background needs to be erased. This happens because windows are usually painted using a 2-stage process:

* WM_ERASEBKGND: Clear the background
* WM_PAINT: Draw the contents on top

And this idea of drawing the background then drawing the the contents reminds me of other stuff i've read even if the names aren't right, but what is done with this message in .Net?

Does it call a method in the form object? What does this method do (is it the invalidate method?), who does it call? How can i stop it from painting the background and painting on top?

Also, can the equivelent of a system paint message be triggered from inside the form (and by whom?)?

Strogian
07-11-2005, 12:17 PM
Well I can tell you that in windows generally, you usually draw the background, using some background brush, (often just solid white) before you paint. The background brush is set when you register the window class.

When the window gets the WM_PAINT message, you are supposed to call BeginPaint(). When BeginPaint() returns, the background has been repainted. This is because BeginPaint sends the WM_ERASEBKGND and WM_NCERASEBKGND messages to your window. The default message handler for WM_ERASEBKGND is to erase the background using the window class's background brush, but you can change that, at least in win32.

So in short, it looks like in .NET, the OnPaint handler is actually what would be put in between BeginPaint and EndPaint, in a regular Win32 app.