about using Pens and Brushes

T

Tony Johansson

Hi!

If I have these code lines I can skip creating the instances for Pen and
Brush by using the static Pens and Brushes.
So my question is if there actually is any point to create an instance for
Pen and Brush as my example shows ?
I have this code

Brush lb = new SolidBrush(Color.White);
Pen lp = new Pen(Color.Black, 1);
g.FillRectangle(lb, lRect);
g.DrawRectangle(lp, lRect);

but I can use the static Pens and Brushes instead in this way
g.FillRectangle(Brushes.White, lRect);
g.DrawRectangle(Pens.Black, lRect);

//Tony
 
J

Jeff Johnson

If I have these code lines I can skip creating the instances for Pen and
Brush by using the static Pens and Brushes.
So my question is if there actually is any point to create an instance for
Pen and Brush as my example shows ?
I have this code

Brush lb = new SolidBrush(Color.White);
Pen lp = new Pen(Color.Black, 1);
g.FillRectangle(lb, lRect);
g.DrawRectangle(lp, lRect);

but I can use the static Pens and Brushes instead in this way
g.FillRectangle(Brushes.White, lRect);
g.DrawRectangle(Pens.Black, lRect);

If you want a pen with a width of 1 then no, there is no reason to create
your own; use <static collection>.<known color> like in your second code
sample. Personally, I almost always want to be positive that my pens will
draw with a single-pixel line, so I end up creating my own with the -1
width.
 
P

Peter Duniho

Jeff said:
If I have these code lines I can skip creating the instances for Pen and
Brush by using the static Pens and Brushes.
So my question is if there actually is any point to create an instance for
Pen and Brush as my example shows ?
[...]

If you want a pen with a width of 1 then no, there is no reason to create
your own;

Amend that to: if you want a pen with a characteristic different from
the built-in ones, not just pen width. This would include things like
dash style, end caps, and even color (there's only a finite number of
built-in pen colors available).

In fact, while it's great that the built-in pens exist, it's fairly
common to still need to create one's own pen.

Likewise brushes, of course.

Pete
 
C

Chris Dunaway

Jeff said:
If I have these code lines I can skip creating the instances for Pen and
Brush by using the static Pens and Brushes.
So my question is if there actually is any point to create an instance for
Pen and Brush as my example shows ?
[...]
If you want a pen with a width of 1 then no, there is no reason to create
your own;

Amend that to: if you want a pen with a characteristic different from
the built-in ones, not just pen width. This would include things like
dash style, end caps, and even color (there's only a finite number of
built-in pen colors available).

In fact, while it's great that the built-in pens exist, it's fairly
common to still need to create one's own pen.

Likewise brushes, of course.

Pete

One other comment, if you create the pen or brush yourself, you
should also dispose them, since I believe they use a gdi handle.

Chris
 

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