Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,869 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi,
I have a requirement to change the thumb colour of trackbar from blue to orange. Is it possible to change the thumb colour and please give a solution for that.
It can be customized with Custom Draw
I have just drawn an orange rectangle for this test to simplify, but you can draw any shape or any bitmap : Trackbar.jpg
Public Class TrackBarCustom
Inherits TrackBar
Private Const TBCD_TICS = &H1
Private Const TBCD_THUMB = &H2
Private Const TBCD_CHANNEL = &H3
<StructLayout(LayoutKind.Sequential)>
Private Structure NMHDR
Public hwndFrom As IntPtr
Public idFrom As IntPtr
Public code As Integer
End Structure
<StructLayout(LayoutKind.Sequential)>
Friend Structure RECT
Public left As Integer
Public top As Integer
Public right As Integer
Public bottom As Integer
End Structure
<StructLayout(LayoutKind.Sequential)>
Private Structure NMCUSTOMDRAW
Public hdr As NMHDR
Public dwDrawStage As Integer
Public hdc As IntPtr
Public rc As RECT
Public dwItemSpec As IntPtr
Public uItemState As UInteger
Public lItemlParam As IntPtr
End Structure
<FlagsAttribute>
Friend Enum CDRF
CDRF_DODEFAULT = &H0
CDRF_NEWFONT = &H2
CDRF_SKIPDEFAULT = &H4
CDRF_DOERASE = &H8
CDRF_SKIPPOSTPAINT = &H100
CDRF_NOTIFYPOSTPAINT = &H10
CDRF_NOTIFYITEMDRAW = &H20
CDRF_NOTIFYSUBITEMDRAW = &H20
CDRF_NOTIFYPOSTERASE = &H40
End Enum
<FlagsAttribute>
Friend Enum CDDS
CDDS_PREPAINT = &H1
CDDS_POSTPAINT = &H2
CDDS_PREERASE = &H3
CDDS_POSTERASE = &H4
CDDS_ITEM = &H10000
CDDS_ITEMPREPAINT = (CDDS_ITEM Or CDDS_PREPAINT)
CDDS_ITEMPOSTPAINT = (CDDS_ITEM Or CDDS_POSTPAINT)
CDDS_ITEMPREERASE = (CDDS_ITEM Or CDDS_PREERASE)
CDDS_ITEMPOSTERASE = (CDDS_ITEM Or CDDS_POSTERASE)
CDDS_SUBITEM = &H20000
End Enum
<DllImport("User32.dll", SetLastError:=True)>
Private Shared Function FillRect(ByVal hDC As IntPtr, ByRef lpRect As RECT, ByVal hBR As IntPtr) As IntPtr
End Function
<DllImport("Gdi32.dll", SetLastError:=True)>
Public Shared Function CreateSolidBrush(crColor As Integer) As Integer
End Function
<DllImport("Gdi32.dll", SetLastError:=True)>
Public Shared Function DeleteObject(hObject As IntPtr) As Boolean
End Function
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_REFLECT + WM_NOFITY Then
Dim pnmhdr = CType(m.GetLParam(GetType(NMHDR)), NMHDR)
If pnmhdr.code = NM_CUSTOMDRAW Then
Dim pnmlv = CType(m.GetLParam(GetType(NMCUSTOMDRAW)), NMCUSTOMDRAW)
Select Case pnmlv.dwDrawStage
Case CDDS.CDDS_PREPAINT
m.Result = New IntPtr(CDRF.CDRF_NOTIFYITEMDRAW)
Case CDDS.CDDS_ITEMPREPAINT
If (pnmlv.dwItemSpec = TBCD_THUMB) Then
Dim hBrush As IntPtr = CreateSolidBrush(ColorTranslator.ToWin32(Color.Orange))
FillRect(pnmlv.hdc, pnmlv.rc, hBrush)
DeleteObject(hBrush)
m.Result = New IntPtr(CDRF.CDRF_SKIPDEFAULT)
Else
m.Result = New IntPtr(CDRF.CDRF_NOTIFYPOSTPAINT)
End If
Case CDDS.CDDS_ITEMPOSTPAINT
m.Result = New IntPtr(CDRF.CDRF_DODEFAULT)
End Select
End If
Return
Else
MyBase.WndProc(m)
End If
End Sub
Private Const NM_FIRST As Integer = 0
Private Const NM_CLICK As Integer = NM_FIRST - 2
Private Const NM_CUSTOMDRAW As Integer = NM_FIRST - 12
Private Const WM_REFLECT As Integer = &H2000
Private Const WM_NOFITY As Integer = &H4E
End Class