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)
{
     [B]e.Graphics.DrawEllipse(Pens.Red, 0, 0, 50, 50);[/B]
}

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 !

The Paint event is raised when the Form is first shown, this is to give the Form its initial appearance. When you show a Form it enters a message loop. Messages are sent from the Operating System. If you look at the Windows API, there are some flags that determine when the window should be redrawn (e.g. horizontal or vertical resize). The paint event can also be called from within the application. The form creates a Graphics object, filling it with the background colour of the form, then when the Paint event has finished calling all of the delegates it can use the Graphics object to write the pixels to the monitor.

Hope this helps to clarify things.

1) When does Form1_Paint(..) called ?
Its called when the form needs to be repainted, see 2

2) When does the Paint event of the form is raised ?
The paint of a control, or form is called when a portion of that object is invalidated or refreshed, which simply means that when windows thinks that part of the screen is no longer accurate, either by passing the WM_PAINT message to the form, or by code on the form itself requesting that it happen.

3) Why the Paint event is raised when I run the application ?
Because the paint event is responsible for drawing the graphics that appear on the control or form, since the application was just opened, its obviously not painted, so It calls Paint()

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 reason the circle is drawn on the form is because you used the form's paint event. Which means that the PaintEventArgs class that is passed to the handler holds a graphics object that was created for that control (in this case form, forms inherit from control) so when you use the parm, "e" and get its Graphics object, that object is actually the Graphics object of the form, so when you call a drawing method from that object it will always relate to that form.

Alternatively, if you were create a graphics object from a bitmap Image in memory, drawing using that Graphics object would result in all drawing being done directly to the Bitmap in memory.

Not to complicated. Simply put, the paint event is called when the form needs to be redrawn because something has covered it or changed in it, The graphics object passed to the event belongs to the form so any drawing using it will result on the form.

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 !

Regarding this:

Alternatively, if you were create a graphics object from a bitmap Image in memory, drawing using that Graphics object would result in all drawing being done directly to the Bitmap in memory.

In your case, the Graphics object related to the Bitmap.
In my case, the Graphics object related to the Form (Control).
Is that true that the Graphics object always related to something ? Is that always Control ?
In your case, you are the one that create the relationship between the Bitmap and the Graphics.
Where is this relationship created in my case ? I would like to see the code.
Thanks in advance !

That I can't help you with, I have looked through all the code for the forms library, and I Can't find the definition for the class "Component".

The control class inherits from the component class and passes any subscribers to its paint event to that base class and also gets its graphics object from that class.

Wish I could be more help.

I read up a little, There is a native method that asks for a graphics object to be pulled from a graphics object pool and assigned to a window handle. Each control does this, as does anything that inherits from the Control class.

There are a finite amount of graphics objects available, and creating them is resource expensive.

if you wish to create a graphics object not from a image but from a window using its handle you can using the graphics class.

This article has been dead for over six months. Start a new discussion instead.