some basic question about which event handler to use when drawing

T

Tony Johansson

Hi!

Below I have two event handlers.
I just wonder is it just a matter of tast which one to use.
I mean if perhaps one is better then the other.

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = CreateGraphics();
Pen pen = new Pen(Color.Maroon, 0);
Brush b = new LinearGradientBrush(new Point(1, 1),
new Point(100, 100),
Color.White,
Color.Red);
Point[] points = new Point[]
{new Point(10,10),
new Point(10,100),
new Point(50,65),
new Point(100,100),
new Point(85,40)};
g.DrawPolygon(pen, points);
g.FillPolygon(b, points);
g.DrawPolygon(pen, points);
}


protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Maroon, 0);
Brush b = new LinearGradientBrush(new Point(1, 1),
new Point(100, 100),
Color.White,
Color.Red);

Point[] points = new Point[]
{new Point(10,10),
new Point(10,100),
new Point(50,65),
new Point(100,100),
new Point(85,40)};
g.DrawPolygon(pen, points);
g.FillPolygon(b, points);
g.DrawPolygon(pen, points);
}

//Tony
 
K

Konrad Neitzel

Hi Tony!

Below I have two event handlers.
I just wonder is it just a matter of tast which one to use.
I mean if perhaps one is better then the other.

Both can be used. I like to use the first one, but that is just my taste.
To use the 2nd way, you have to create your own class that derives the Form
class but the first way could also be used without that.
(e.g. example on
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx)
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = CreateGraphics();
Pen pen = new Pen(Color.Maroon, 0);
Brush b = new LinearGradientBrush(new Point(1, 1),
new Point(100, 100),
Color.White,
Color.Red);
Point[] points = new Point[]
{new Point(10,10),
new Point(10,100),
new Point(50,65),
new Point(100,100),
new Point(85,40)};
g.DrawPolygon(pen, points);
g.FillPolygon(b, points);
g.DrawPolygon(pen, points);
}

Graphics implements the IDisposable interface, so you should Dispose it (Or
better: Just use the using statement!).
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Maroon, 0);
Brush b = new LinearGradientBrush(new Point(1, 1),
new Point(100, 100),
Color.White,
Color.Red);

Point[] points = new Point[]
{new Point(10,10),
new Point(10,100),
new Point(50,65),
new Point(100,100),
new Point(85,40)};
g.DrawPolygon(pen, points);
g.FillPolygon(b, points);
g.DrawPolygon(pen, points);
}

If you override OnPaint, you should make sure, that you call the base
Method! So a base.OnPain(e) should be added!

With kind regards,

Konrad
 
F

Family Tree Mike

Hi!

Below I have two event handlers.
I just wonder is it just a matter of tast which one to use.
I mean if perhaps one is better then the other.

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = CreateGraphics();
Pen pen = new Pen(Color.Maroon, 0);
Brush b = new LinearGradientBrush(new Point(1, 1),
new Point(100, 100),
Color.White,
Color.Red);
Point[] points = new Point[]
{new Point(10,10),
new Point(10,100),
new Point(50,65),
new Point(100,100),
new Point(85,40)};
g.DrawPolygon(pen, points);
g.FillPolygon(b, points);
g.DrawPolygon(pen, points);
}


protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Maroon, 0);
Brush b = new LinearGradientBrush(new Point(1, 1),
new Point(100, 100),
Color.White,
Color.Red);

Point[] points = new Point[]
{new Point(10,10),
new Point(10,100),
new Point(50,65),
new Point(100,100),
new Point(85,40)};
g.DrawPolygon(pen, points);
g.FillPolygon(b, points);
g.DrawPolygon(pen, points);
}

//Tony

I agree with Konrad in preferring to handle the paint event with the
first method. I will say however, I see no benefit to calling the
CreateGraphics method in that routine since the PaintEventArgs contains
the graphics object that should be used.
 
P

Peter Duniho

Konrad said:
Hi Tony!



Both can be used. I like to use the first one, but that is just my taste.
To use the 2nd way, you have to create your own class that derives the
Form class but the first way could also be used without that.
(e.g. example on
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx)

The override approach wouldn't generally be used unless you're already
subclassing the base class. I agree that there's no point in inheriting
a class just so that you can override its OnPaint() method, but in the
situation where your code already is in the derived class, you might as
well override IMHO.

Pete
 
P

Peter Duniho

Family said:
[...]
I agree with Konrad in preferring to handle the paint event with the
first method. I will say however, I see no benefit to calling the
CreateGraphics method in that routine since the PaintEventArgs contains
the graphics object that should be used.

Not only should one not call CreateGraphics(), if the code is not
actually handling one's own event, doing so could very well cause
unexpected results, because the Graphics instance would be for a
different Control instance than the one being drawn at the moment.

Pete
 
T

Tony Johansson

Peter Duniho said:
Family said:
[...]
I agree with Konrad in preferring to handle the paint event with the
first method. I will say however, I see no benefit to calling the
CreateGraphics method in that routine since the PaintEventArgs contains
the graphics object that should be used.

Not only should one not call CreateGraphics(), if the code is not actually
handling one's own event, doing so could very well cause unexpected
results, because the Graphics instance would be for a different Control
instance than the one being drawn at the moment.

Pete

Hi!

I must say that I very new to use GDI+.

One more thing if I add both the Form1_Paint event handler method and the
OnPaint event handler method to the Form1 class only the Form1_Paint event
handler method is called.
So my first question is why is it not any call to the OnPaint event handler
method ?
A call is made to the OnPaint event handler method if I remove the
Form1_Paint event handler method.

I know when the Form1_Paint event is raised.
My second question is.When is the OnPaint event raised ?

My third question is.
If I look strictly to this example with Form1 class which event handler
method is the best choice ?

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = CreateGraphics();
Pen pen = new Pen(Color.Maroon, 0);
Brush b = new LinearGradientBrush(new Point(1, 1),
new Point(100, 100),
Color.White,
Color.Red);
Point[] points = new Point[]
{new Point(10,10),
new Point(10,100),
new Point(50,65),
new Point(100,100),
new Point(85,40)};
g.DrawPolygon(pen, points);
g.FillPolygon(b, points);
g.DrawPolygon(pen, points);
}


protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Maroon, 0);
Brush b = new LinearGradientBrush(new Point(1, 1),
new Point(100, 100),
Color.White,
Color.Red);
Point[] points = new Point[]
{new Point(10,10),
new Point(10,100),
new Point(50,65),
new Point(100,100),
new Point(85,40)};
g.DrawPolygon(pen, points);
g.FillPolygon(b, points);
g.DrawPolygon(pen, points);
}
}
}
 
P

Peter Duniho

Tony said:
[...]
One more thing if I add both the Form1_Paint event handler method and the
OnPaint event handler method to the Form1 class only the Form1_Paint event
handler method is called.

The way you wrote it, that is correct.
So my first question is why is it not any call to the OnPaint event handler
method ?

I explained that in my previous reply to you. If there was something
about that answer that you are having trouble understanding, you should
reply to it, being specific about what you're having trouble with.
A call is made to the OnPaint event handler method if I remove the
Form1_Paint event handler method.

I know when the Form1_Paint event is raised.
My second question is.When is the OnPaint event raised ?

There is no "OnPaint" event. There is only the "Paint" event. So,
either your question is "when is the 'Paint' event raised?", or it's
"when is the 'OnPaint()' method called?"

Since I'm not sure which you're asking…

The "Paint" event is raised by the Control.OnPaint() method.

The Control.OnPaint() method is called by the Control's window
procedure, when it receives the WM_PAINT message, if the method is not
overridden. If it is overridden, then it is called if and when a
derived class calls it.
My third question is.
If I look strictly to this example with Form1 class which event handler
method is the best choice ?

As I wrote before: you only have one event handler in that code. And
which is best is mainly a question of personal preference. My own
preference is to override the "On…" method when possible. It's not
always possible, and when it's not then you must subscribe to the event
instead. You don't have a choice in that case.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top