Overriding OnPaint doesn't work at all for whatever reason but I have been successful in handling painting more directly through the Control's Window Procedure.
vbnet Code:
Public Class TrackBarEx Inherits TrackBar Protected Overrides Sub WndProc(ByRef m As Message) Const WM_PAINT As Integer = &HF If m.Msg = WM_PAINT Then 'Draw the control as normal MyBase.WndProc(m) 'Add a border around it DrawControl() Exit Sub End If MyBase.WndProc(m) End Sub Private Sub DrawControl() Using g As Graphics = Me.CreateGraphics DrawBorder(g, Color.Red, 3) End Using End Sub Private Sub DrawBorder(ByVal g As Graphics, ByVal c As Color, ByVal thickness As Integer) Dim rect As Rectangle = New Rectangle(0, 0, Me.ClientRectangle.Width - 1, Me.ClientRectangle.Height - 1) Using p As Pen = New Pen(c) For i = 0 To thickness - 1 g.DrawRectangle(p, New Rectangle(i, i, rect.Width - i * 2, rect.Height - i * 2)) Next End Using End Sub End Class
Which produces this:-
I've created a derived class that draws a border around the control. There is a slight quirk though. When dragging the control on a Form in design mode, the control renders normally for the duration of the drag operation, effectively ignoring my custom painting code. I have no idea why. However, it seems to work acceptably otherwise.





Reply With Quote