473,325 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Custom Paint & disposing

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
5 1498
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Sagaert Johan | last post by:
I have made a custom control that draws a rectangle when the mouse is down, and does nothing when the mouse is up. I set/reset a flag in MouseDown/Mouse up and use this to do the drawing in the...
3
by: Bill C. | last post by:
Hello, I know this has been discussed a lot already because I've been searching around for information the last few weeks. I'm trying to implement a DataGridComboBoxColumn class. I've found...
0
by: Duncan Mole | last post by:
Hi, I have created a control which draws a title bar and provides a drop down menu for a Smart Device Application. It seemed to work fine until I came to add an event handler to act on Paint...
2
by: Serge Klokov | last post by:
Hi! 1. Please, help with example "paint on form by mouse" 2. Below is my example, but it clear the line after each Refresh()... how to fix? 3. How to draw the line in Mouse_Move event? ...
5
by: ross kerr | last post by:
Hi All, I am extending the combobox to create a control that selects an item based on the text the user is typing into the text area of the control. I have an issue that occurs only when i...
1
by: Brian Henry | last post by:
I am trying to make a custom user control that gets a list of users from our database and populates the list, its an owner drawn control also, the problem is, I placed the item onto a form and...
2
by: AMDRIT | last post by:
Hello everyone, I have created a custom component and one of its properties is a class object with it's own properties. During runtime, I can assign values to the class object properties just...
0
by: Brian Henry | last post by:
Ok I've never implemented a snap location before so I dont really know what im doing wrong here... anyways, I am making a custom slider control that takes dates as its values instead of integers......
0
by: ChopStickr | last post by:
I have a custom control that is embedded (using the object tag) in an html document. The control takes a path to a local client ini file. Reads the file. Executes the program specified in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.