Sign in to follow this  
stam112

Very basic question - Paint event

Recommended Posts

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 !

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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 ?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 !

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this