Jump to content
  • Advertisement
Sign in to follow this  
stam112

Very basic question - Paint event

This topic is 3191 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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 :)

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!