# 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.

## 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 on other sites
Quote:
 Original post by stam1121) 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 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 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 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 !

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
17
5. 5

• 9
• 12
• 9
• 33
• 13
• ### Forum Statistics

• Total Topics
632595
• Total Posts
3007267

×