By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,426 Members | 1,721 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,426 IT Pros & Developers. It's quick & easy.

Custom Paint & disposing

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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 :)
"Chris" <no@spam.com> wrote in message
news:eB**************@TK2MSFTNGP14.phx.gbl...
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

Nov 21 '05 #2

P: n/a
One day, Chris wrote:
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
///

--
HTH, Tom Spink
Nov 21 '05 #3

P: n/a
Robin Tucker wrote:
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 :)
"Chris" <no@spam.com> wrote in message
news:eB**************@TK2MSFTNGP14.phx.gbl...
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



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
Nov 21 '05 #4

P: n/a
Tom Spink wrote:
One day, Chris wrote:

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
///


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
Nov 21 '05 #5

P: n/a
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.

"Chris" <no@spam.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Tom Spink wrote:
One day, Chris wrote:

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
///


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

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.