Paul,
Thanks for your continued help.
Firstly, correction, the OEM have provided an OCX, not an OCX utility.
Actually, Im having a problem with the SPEED of my custom picturebutton
control refresh. A picturebutton is derived from the control class. I have
35 picturebuttons in my panel.
Basically, at times Its necessary to read some data from the SQLCE database
and RESIZE and refresh some or all of the custom buttons. THe code that gets
executed simply reads the values from the database(button
backcolor,forecolor,text) using the forward only fast datareader, sets the
values for each cusom button in a while loop (computing the width,height,X,Y)
and leaves the onPaint of each control to redraw the button. Also the only
other task inside the while loop is that I set the Bounds of each custom
button. I finally call Application.doEvents().
The whole process takes average 1450ms and appears slow when using the
application.
I was slowing moving onto the idea that maybe I could learn API programming
to maybe write a native button that will be much quicker to execute.
Unless you have other ideas?
Here is my PictureButton Code:
Public Class PictureButton
Inherits Control
'Private ButtonType As String 'Button as Staydown or OneShot - some
MENU'S are staydown or oneshot
'Public buttonImg As Bitmap
Public pressed As Boolean = False
Public funcType As Integer
Public FuncValue As Integer
Public m_MultiUse As Integer
Private linesoftext As Int16 = 0
Private Shared verticalOffSet As Single
Private Shared horizontalOffSet As Single
Private str As String
Public myText As String = String.Empty
'Ensure each text line has max 8 characters
Private Shared c As Integer = 0
'Constructor with parameters - Takes RGB as colour
'Must use Sub MakeBitmap(R,G,B,width,height)
Public Sub New(ByVal MyParent As Object, _
ByVal x As Integer, _
ByVal y As Integer, _
ByVal width As Integer, _
ByVal height As Integer, _
ByVal R As Integer, _
ByVal G As Integer, _
ByVal B As Integer, _
ByVal butttext As String)
Me.Parent = MyParent
Me.Bounds = New Rectangle(x, y, width, height)
'Sets the Background Colour using RGB
Me.BackColor = Color.FromArgb(R, G, B)
'Sets the Text to be displayed onto the screen
Me.Text = butttext
'AddHandler Me.Click, AddressOf button_Click
End Sub
' Override the OnPaint method so we can draw the background image and
the text.
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
'Adjust so that new width & Height
'are used
If Me.pressed Then
Else
'Background color already set
End If
c = 0
linesoftext = 0
verticalOffSet = 0
horizontalOffSet = 0
str = String.Empty
If myText.Length > 0 Then
ButtonsText = myText.Split("|")
'Ensure each text line has max 8 characters
For c = 0 To ButtonsText.Length - 1
If ButtonsText(c) <> String.Empty Then
'If ButtonsText(c).Length > 8 Then
' ButtonsText(c) = ButtonsText(c).Substring(0, 8)
'End If
'count no of lines in button text
linesoftext += 1
End If
Next
'Directly use the text hieght without holding
'it into a variable.
'Calculated only once
If Me.Width < 70 Then
verticalOffSet = ((Me.ClientSize.Height - (linesoftext *
12)) / 2) - 1
Else
verticalOffSet = ((Me.ClientSize.Height - (linesoftext *
21)) / 2) - 1
End If
'Calculated horizontalOffSet for each line of text
For Each str In ButtonsText
'Try
' str.TrimEnd(" ")
'Catch ex As Exception
' 'DO NOTHING
'End Try
If str.Length > 0 Then
If Me.Width < 70 Then
'Use Small Font
'Calculate with of text
gBtnTxtSize = e.Graphics.MeasureString(str,
gSmallFont)
'calculate horizontal offset
horizontalOffSet = (Me.ClientSize.Width -
Convert.ToInt32(gBtnTxtSize.Width)) / 2
e.Graphics.DrawString(str, _
gSmallFont, _
New SolidBrush(Me.ForeColor), _
horizontalOffSet, _
verticalOffSet)
verticalOffSet += 15
ElseIf Me.Width < 82 Then
'Use Medium Font
'Calculate with of text
gBtnTxtSize = e.Graphics.MeasureString(str, gMedFont)
'calculate horizontal offset
horizontalOffSet = (Me.ClientSize.Width -
Convert.ToInt32(gBtnTxtSize.Width)) / 2
e.Graphics.DrawString(str, _
gMedFont, _
New SolidBrush(Me.ForeColor), _
horizontalOffSet, _
verticalOffSet)
verticalOffSet += 18
Else
'Use Large Font
'Calculate with of text
gBtnTxtSize = e.Graphics.MeasureString(str, gFont)
'calculate horizontal offset
horizontalOffSet = (Me.ClientSize.Width -
Convert.ToInt32(gBtnTxtSize.Width)) / 2
e.Graphics.DrawString(str, _
gFont, _
New SolidBrush(Me.ForeColor), _
horizontalOffSet, _
verticalOffSet)
verticalOffSet += 18
End If
End If
Next
End If
'====================================
If Me.pressed Then
MyPen.Color = Color.Black
e.Graphics.DrawLine(MyPen, 0, 0, 0, Me.ClientSize.Height)
e.Graphics.DrawLine(MyPen, 1, 1, 1, Me.ClientSize.Height - 2)
e.Graphics.DrawLine(MyPen, 0, 0, Me.ClientSize.Width, 0)
e.Graphics.DrawLine(MyPen, 1, 1, Me.ClientSize.Width - 1, 1)
Else
MyPen.Color = Color.White
e.Graphics.DrawLine(MyPen, 0, 0, 0, Me.ClientSize.Height)
e.Graphics.DrawLine(MyPen, 1, 1, 1, Me.ClientSize.Height - 1)
e.Graphics.DrawLine(MyPen, 0, 0, Me.ClientSize.Width, 0)
e.Graphics.DrawLine(MyPen, 1, 1, Me.ClientSize.Width - 4, 1)
MyPen.Color = Color.Black
e.Graphics.DrawLine(MyPen, 0, Me.ClientSize.Height - 1,
Me.ClientSize.Width, Me.ClientSize.Height - 1)
e.Graphics.DrawLine(MyPen, 1, Me.ClientSize.Height - 2,
Me.ClientSize.Width, Me.ClientSize.Height - 2)
e.Graphics.DrawLine(MyPen, 2, Me.ClientSize.Height - 3,
Me.ClientSize.Width, Me.ClientSize.Height - 3)
e.Graphics.DrawLine(MyPen, Me.ClientSize.Width - 1, 0,
Me.ClientSize.Width - 1, Me.ClientSize.Height)
e.Graphics.DrawLine(MyPen, Me.ClientSize.Width - 2, 0,
Me.ClientSize.Width - 2, Me.ClientSize.Height)
e.Graphics.DrawLine(MyPen, Me.ClientSize.Width - 3, 2,
Me.ClientSize.Width - 3, Me.ClientSize.Height - 3)
MyPen.Color = Color.White
End If
'MyBase.OnPaint(e)
End Sub
End Class