Very basic question - Paint event

Started by
3 comments, last by stam112 14 years, 3 months ago
Hello, This is my first question in this forum :) I'm new in GDI+ and C# programming, though I have an experience in other programming languages. Here is my question: I started a new Windows Form Application project in Visual Studio 2008. Then I double-clicked the Paint event of the form and added the following line to the Form1_Paint(..) method:

private void Form1_Paint(object sender, PaintEventArgs e)
{
     e.Graphics.DrawEllipse(Pens.Red, 0, 0, 50, 50);
}
When I run the application I do see the circle, but I don't really understand what happened. More specifically: 1) When does Form1_Paint(..) called ? 2) When does the Paint event of the form is raised ? 3) Why the Paint event is raised when I run the application ? 4) Why the circle is actually drawn on the form ? i.e. what is the relationship between the form class, the "PaintEventArgs e" argument, and the "e.Graphics" object ? I will appreciate any help ! Thanks in advance !
Advertisement
Quote:Original post by stam112
1) When does Form1_Paint(..) called ?
2) When does the Paint event of the form is raised ?
3) Why the Paint event is raised when I run the application ?

Windows will send your window a WM_PAINT (which in C# eventually gets filtered down to this event) whenever a portion of it needs to be redrawn. This can happen whenever another window on top of yours is moved and reveals something that was previously hidden, when the user increases the size of the window, goes between the minimized/maximized/restored states, etc. It especially needs to be sent when the window is first shown on application startup, because there was nothing before! Suffice to say, if Windows can't redraw a portion of your window through its own devices (like on a simple mouse move), it will send you the message.

Quote:
4) Why the circle is actually drawn on the form ? i.e. what is the relationship between the form class, the "PaintEventArgs e" argument, and the "e.Graphics" object ?

The argument to the event contains a Graphics object, which is your interface to drawing on the form. As for why the ellipse it drawn, it's because you called DrawEllipse :)
Thanks a lot !

Could you elaborate on this:

"if Windows can't redraw a portion of your window through its own devices (like on a simple mouse move), it will send you the message."

What message ?
Which devices ?
Quote:What message ? Which devices ?

What Zipster said is true. What is meant is that Windows oftens redraws a portion of the window for you. What he meant by "the message" was WM_PAINT. E.g., during a mouse move, Windows takes care of drawing the mouse, moving the mouse and then restoring what was draw over by the mouse cursor.

So, if something has written over a portion of your window and Windows doesn't restore it, it sends your application a WM_PAINT message, along with the rectangle that needs to be restored.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Thank you both !
Now I understand this better.
Now I have several more questions :)

1) Is that true that the default implementation of what happened when Paint event is fired is to call the OnPaint(..) method ?

2) What is the default implementation of OnPaint(..) ?

3) What may happen if I override the OnPaint(..) method but will not call base.OnPaint(..) ?

4) Who and when calls OnPaintBackground(..) ?

Thank you very much !

This topic is closed to new replies.

Advertisement