Custom Paint & disposing

C

Chris

I have a form that requires drawing custom lines on it. The color of
the lines is suppose to be the same as the forcolor of the form. Am I
doing this the most efficent and correct way? Thanks....

Protected mPen As Pen

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
If mPen Is Nothing Then mPen = New Pen(Me.ForeColor)
e.Graphics.DrawRectangle(mPen, New Rectangle(5, 5, Me.Width -
10, Me.Height - 10))
e.Graphics.DrawRectangle(mPen, New Rectangle(8, 8, Me.Width -
16, Me.Height - 16))
End Sub

Private Sub PopupTemplate_ForeColorChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles MyBase.ForeColorChanged
If Not mPen Is Nothing Then mPen.Dispose()
mPen = New Pen(Me.ForeColor)
End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
if not mPen is Nothing then mPen.dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
 
T

Tom Spink

I have a form that requires drawing custom lines on it. The color of
the lines is suppose to be the same as the forcolor of the form. Am I
doing this the most efficent and correct way? Thanks....
<snippedy-doo-dah>

Hi Chris,

Strange as it may sound, it may be better to create your pen and dispose of
it *during* the painting...

///
Protected Overrides Sub OnPaint (...)
MyBase.OnPaint(e) ' Call base method implementation.

' Instantiate your pen.
Dim penForeColour As Pen = New Pen(Me.ForeColor)

' Use your brand-new pen here.
e.Graphics.DrawRectangle(...)
e.Graphics.DrawRectangle(...)

penForeColour.Dispose() ' Dispose of your pen.
penForeColour = Nothing ' Just to stay sane :)
End Sub
///
 
R

Robin Tucker

Not really, unless your intention is to use "mPen" in the OnPaint method.
At the moment, you are continually creating a new pen there.

Happy coding :)
 
C

Chris

Robin said:
Not really, unless your intention is to use "mPen" in the OnPaint method.
At the moment, you are continually creating a new pen there.

Happy coding :)

I'm not continually creating a new pen in the OnPaint... it only creates
it if it doesn't exist. I did it that way to stop from recreating the
pen over and over and over.

Chris
 
C

Chris

Tom said:
One day, Chris wrote:



<snippedy-doo-dah>

Hi Chris,

Strange as it may sound, it may be better to create your pen and dispose of
it *during* the painting...

///
Protected Overrides Sub OnPaint (...)
MyBase.OnPaint(e) ' Call base method implementation.

' Instantiate your pen.
Dim penForeColour As Pen = New Pen(Me.ForeColor)

' Use your brand-new pen here.
e.Graphics.DrawRectangle(...)
e.Graphics.DrawRectangle(...)

penForeColour.Dispose() ' Dispose of your pen.
penForeColour = Nothing ' Just to stay sane :)
End Sub
///

I remember reading about how graphic object such as pen's should be used
as little as possible as they have some overhead... that's why I didn't
create it in the onpaint method. Is there a reason, other than simplity
that I should do it your way?

Chris
 
B

Bob Powell [MVP]

Pens and brushes are are not complex can be created and disposed of in the
draw routing with very little overhead. Pens that use complex brushes and
the brushes themselves, such as LinearGradient' / PathGradient' /
TextureBrush can be made more efficient by keeping them around as long as
you don't have to mess with their parameters.

Fonts in particular can be made more efficient by storing them if you expect
to use several fonts per draw-cycle.

There is an article in Windows Forms Tips and Tricks that illustrates
speeding up drawing by cacheing graphical objects. This is really only
useful in very intensive operations though. For simple stuff, don't bother.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 

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