Speeding up the Label_Paint event

J

jcrouse

I have a program that is an external viewer. It is launched with a hotkey
from within another application. The program has 30 labels that can be
displayed. Since the labels may not always be oriented the same as the
Operating System I needed to use the Label_Paint event to PAINT the labels
text if it not oriented the same as the operating system. I am trying to
figure out a way to make this event happen faster or optimize it if
possible. I am new to this code. I think the idea was Herfreid's, can't even
remember. Here is the code:

Public Sub myPaint1(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim strLabelName As String

Select Case myPLabel.Name

Case "lblP1JoyUp"

strLabelName = Label1.Text

Case "lblP1JoyLeft"

strLabelName = Label2.Text

Case "lblP1JoyDown"

strLabelName = Label3.Text

Case "lblP1JoyRight"

strLabelName = Label4.Text

Case "lblP1B1"

strLabelName = Label5.Text

Case "lblP1B2"

strLabelName = Label6.Text

Case "lblP1B3"

strLabelName = Label7.Text

Case "lblP1B4"

strLabelName = Label8.Text

Case "lblP1B5"

strLabelName = Label9.Text

Case "lblP1B6"

strLabelName = Label10.Text

Case "lblP1B7"

strLabelName = Label11.Text

Case "lblP1B8"

strLabelName = Label12.Text

Case "lblP1JoyType"

strLabelName = Label25.Text

Case "lblGameName"

strLabelName = Label27.Text

Case "lblNumPlayers"

strLabelName = Label28.Text

Case "lblHistory"

strLabelName = Label29.Text

Case "lblUndocumented"

strLabelName = Label30.Text

End Select

If myPLabel.Visible = True Then

Dim myFontBrush As New SolidBrush(myPLabel.ForeColor)

If frm1.intPriHP1 = frm1.intPriVP1 Then

If strLay1RotAngle = "0" Then

myPLabel.Text = strLabelName

ElseIf strLay1RotAngle = "90" Then

Dim y As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)

Dim x As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)

myPLabel.Text = ""

e.Graphics.TranslateTransform((myPLabel.ClientSize.Width
+ x) \ 2, (myPLabel.ClientSize.Height + y) \ 2)

e.Graphics.RotateTransform(90)

e.Graphics.DrawString(strLabelName, myPLabel.Font,
myFontBrush, -y, 0)

ElseIf strLay1RotAngle = "180" Then

Dim sf As New StringFormat

sf.Alignment = StringAlignment.Center

sf.LineAlignment = StringAlignment.Center

myPLabel.Text = ""

e.Graphics.TranslateTransform(myPLabel.ClientSize.Width,
myPLabel.ClientSize.Height)

e.Graphics.RotateTransform(180)

e.Graphics.DrawString(strLabelName, myPLabel.Font,
myFontBrush, RectangleF.op_Implicit(myPLabel.ClientRectangle), sf)

ElseIf strLay1RotAngle = "-90" Then

Dim y As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)

Dim x As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)

lblP1JoyUp.Text = ""



e.Graphics.TranslateTransform((myPLabel.ClientSize.Width -
x) \ 2, (myPLabel.ClientSize.Height - y) \ 2)

e.Graphics.RotateTransform(270)

e.Graphics.DrawString(strLabelName, myPLabel.Font,
myFontBrush, -y, 0)

End If

ElseIf frm1.intPriHP1 <> frm1.intPriVP1 Then

If strGameO = "h" Then

If strLay1RotAngle = "0" Then

myPLabel.Text = strLabelName

ElseIf strLay1RotAngle = "90" Then

Dim y As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)

Dim x As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)

myPLabel.Text = ""


e.Graphics.TranslateTransform((myPLabel.ClientSize.Width + x) \ 2,
(myPLabel.ClientSize.Height + y) \ 2)

e.Graphics.RotateTransform(90)

e.Graphics.DrawString(strLabelName, myPLabel.Font,
myFontBrush, -y, 0)

ElseIf strLay1RotAngle = "180" Then

Dim sf As New StringFormat

sf.Alignment = StringAlignment.Center

sf.LineAlignment = StringAlignment.Center

myPLabel.Text = ""


e.Graphics.TranslateTransform(myPLabel.ClientSize.Width,
myPLabel.ClientSize.Height)

e.Graphics.RotateTransform(180)

e.Graphics.DrawString(strLabelName, myPLabel.Font,
myFontBrush, RectangleF.op_Implicit(myPLabel.ClientRectangle), sf)

ElseIf strLay1RotAngle = "-90" Then

Dim y As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)

Dim x As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)

myPLabel.Text = ""


e.Graphics.TranslateTransform((myPLabel.ClientSize.Width - x) \ 2,
(myPLabel.ClientSize.Height - y) \ 2)

e.Graphics.RotateTransform(270)

e.Graphics.DrawString(strLabelName, myPLabel.Font,
myFontBrush, -y, 0)

End If

ElseIf strGameO = "v" Then

If strLay2RotAngle = "0" Then

myPLabel.Text = strLabelName

ElseIf strLay2RotAngle = "90" Then

Dim y As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)

Dim x As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)

myPLabel.Text = ""


e.Graphics.TranslateTransform((myPLabel.ClientSize.Width + x) \ 2,
(myPLabel.ClientSize.Height + y) \ 2)

e.Graphics.RotateTransform(90)

e.Graphics.DrawString(strLabelName, myPLabel.Font,
myFontBrush, -y, 0)

ElseIf strLay2RotAngle = "180" Then

Dim sf As New StringFormat

sf.Alignment = StringAlignment.Center

sf.LineAlignment = StringAlignment.Center

myPLabel.Text = ""


e.Graphics.TranslateTransform(myPLabel.ClientSize.Width,
myPLabel.ClientSize.Height)

e.Graphics.RotateTransform(180)

e.Graphics.DrawString(strLabelName, myPLabel.Font,
myFontBrush, RectangleF.op_Implicit(myPLabel.ClientRectangle), sf)

ElseIf strLay2RotAngle = "-90" Then

Dim y As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)

Dim x As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)

myPLabel.Text = ""


e.Graphics.TranslateTransform((myPLabel.ClientSize.Width - x) \ 2,
(myPLabel.ClientSize.Height - y) \ 2)

e.Graphics.RotateTransform(270)

e.Graphics.DrawString(strLabelName, myPLabel.Font,
myFontBrush, -y, 0)

End If

End If

End If

myFontBrush.Dispose()

End If

End Sub



Any ides how I might speed this up? Currently, if then user is displaying
all 30 labels I need am calling this code 30 times.

Thnaks,
John
 
J

Jay B. Harlow [MVP - Outlook]

John,
The paint event should be doing as little as possible.

How is myPaint1 sub being called?
How is myPLabel being set?

Based on what you posted I would make the following recommendations:
1. Use variables of the correct type (I would expect strLay1RotAngle &
strLay2RotAngle would be integers not strings).
2. Seriously (very seriously) consider using a class derived from Label
instead of all the code in your form.

For example rather then having 30 Labels on my form I would have 30
SpecializedLabels on my form. Where SpecializedLabel inherits from Label,
and has properties, methods & event handlers specific to each label.

Within the SpecializedLabel class I would override the OnPaint method to
handle painting that specific label. I would have properties on
SpecializedLabel to handle the rotation & what to display.

I would use the ControlStyles.UserPaint to handle all the painting myself,
then simply use the Text property for what to paint (rather then the select
case myPLabel.Name). Alternatively I would have a property for LabelName to
display...

I will try to post a start of a sample later today.

Hope this helps
Jay

jcrouse said:
I have a program that is an external viewer. It is launched with a hotkey
from within another application. The program has 30 labels that can be
displayed. Since the labels may not always be oriented the same as the
Operating System I needed to use the Label_Paint event to PAINT the labels
text if it not oriented the same as the operating system. I am trying to
figure out a way to make this event happen faster or optimize it if
possible. I am new to this code. I think the idea was Herfreid's, can't even
remember. Here is the code:

Public Sub myPaint1(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim strLabelName As String

Select Case myPLabel.Name

Case "lblP1JoyUp"

strLabelName = Label1.Text

Case "lblP1JoyLeft"

strLabelName = Label2.Text

Case "lblP1JoyDown"

strLabelName = Label3.Text

Case "lblP1JoyRight"

strLabelName = Label4.Text

Case "lblP1B1"

strLabelName = Label5.Text

Case "lblP1B2"

strLabelName = Label6.Text

Case "lblP1B3"

strLabelName = Label7.Text

Case "lblP1B4"

strLabelName = Label8.Text

Case "lblP1B5"

strLabelName = Label9.Text

Case "lblP1B6"

strLabelName = Label10.Text

Case "lblP1B7"

strLabelName = Label11.Text

Case "lblP1B8"

strLabelName = Label12.Text

Case "lblP1JoyType"

strLabelName = Label25.Text

Case "lblGameName"

strLabelName = Label27.Text

Case "lblNumPlayers"

strLabelName = Label28.Text

Case "lblHistory"

strLabelName = Label29.Text

Case "lblUndocumented"

strLabelName = Label30.Text

End Select

If myPLabel.Visible = True Then

Dim myFontBrush As New SolidBrush(myPLabel.ForeColor)

If frm1.intPriHP1 = frm1.intPriVP1 Then

If strLay1RotAngle = "0" Then

myPLabel.Text = strLabelName

ElseIf strLay1RotAngle = "90" Then

Dim y As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)

Dim x As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)

myPLabel.Text = ""

e.Graphics.TranslateTransform((myPLabel.ClientSize.Width
+ x) \ 2, (myPLabel.ClientSize.Height + y) \ 2)

e.Graphics.RotateTransform(90)

e.Graphics.DrawString(strLabelName, myPLabel.Font,
myFontBrush, -y, 0)

ElseIf strLay1RotAngle = "180" Then

Dim sf As New StringFormat

sf.Alignment = StringAlignment.Center

sf.LineAlignment = StringAlignment.Center

myPLabel.Text = ""

e.Graphics.TranslateTransform(myPLabel.ClientSize.Width,
myPLabel.ClientSize.Height)

e.Graphics.RotateTransform(180)

e.Graphics.DrawString(strLabelName, myPLabel.Font,
myFontBrush, RectangleF.op_Implicit(myPLabel.ClientRectangle), sf)

ElseIf strLay1RotAngle = "-90" Then

Dim y As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)

Dim x As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)

lblP1JoyUp.Text = ""
e.Graphics.TranslateTransform((myPLabel.ClientSize.Width -
 
J

jcrouse

The myPaint1 sub is being called 30 times. Once from each Label_Paint event.
I agree about the user defined controls. You guys suggested that a month or
two ago, however, it is over my head and I'm still trying to figure out how
to do it. Here is one of my Label_Paint events:

Private Sub lblP1JoyUp_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles lblP1JoyUp.Paint

myPLabel = lblP1JoyUp

myPaint1(e)

End Sub



Thanks,
John

Jay B. Harlow said:
John,
The paint event should be doing as little as possible.

How is myPaint1 sub being called?
How is myPLabel being set?

Based on what you posted I would make the following recommendations:
1. Use variables of the correct type (I would expect strLay1RotAngle &
strLay2RotAngle would be integers not strings).
2. Seriously (very seriously) consider using a class derived from Label
instead of all the code in your form.

For example rather then having 30 Labels on my form I would have 30
SpecializedLabels on my form. Where SpecializedLabel inherits from Label,
and has properties, methods & event handlers specific to each label.

Within the SpecializedLabel class I would override the OnPaint method to
handle painting that specific label. I would have properties on
SpecializedLabel to handle the rotation & what to display.

I would use the ControlStyles.UserPaint to handle all the painting myself,
then simply use the Text property for what to paint (rather then the select
case myPLabel.Name). Alternatively I would have a property for LabelName to
display...

I will try to post a start of a sample later today.

Hope this helps
Jay
e.Graphics.TranslateTransform((myPLabel.ClientSize.Width -
 
J

jcrouse

I'm sure here I could have also use some sort of DirectCast(sender, Label)
but don't really understand that syntax either. I'll get there eventually.
It's just a slow process.

Thanks,
John

jcrouse said:
The myPaint1 sub is being called 30 times. Once from each Label_Paint event.
I agree about the user defined controls. You guys suggested that a month or
two ago, however, it is over my head and I'm still trying to figure out how
to do it. Here is one of my Label_Paint events:

Private Sub lblP1JoyUp_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles lblP1JoyUp.Paint

myPLabel = lblP1JoyUp

myPaint1(e)

End Sub



Thanks,
John
e.Graphics.TranslateTransform((myPLabel.ClientSize.Width -
 
J

Jay B. Harlow [MVP - Outlook]

John,
Unfortunately I do not have time to come up with a meaningful sample for
you.

You can shorten the following to:
Private Sub lblP1JoyUp_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles lblP1JoyUp.Paint

myPLabel = DirectCast(sender, Label)

myPaint1(e)

End Sub

If you do the above all 30 times, then you can simply reduce it to once,
with 30 controls on the Handles.

Private Sub lblP1JoyUp_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles lblP1JoyUp.Paint,
lblP2JoyUp.Paint...

myPLabel = DirectCast(sender, Label)

myPaint1(e)

End Sub

I would recommend you start with Robin A. Reynolds-Haertle's book "OOP -
with Microsoft Visual Basic .NET and Microsoft Visual C# .NET - Step by
Step" from MS Press. As its a good book on the how of OO. Taking an OO
approach with your app should significantly reduce the amount of code you
have, which should also cause it to run quicker (you would not be jumping
through hopes to get to select case statements to get back to the control
that you started with.


Hope this helps
Jay
 

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