Change the thumb colour of a trackbar in winforms C#

Hemanth B 886 Reputation points
2021-12-12T14:25:58.187+00:00

Hi, is there any way to change the thumb color of the trackbar in Winforms c#?
I've tried the following, but it is not working:

//Created a custom class and added this:
  protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            e.Graphics.FillRectangle(Brushes.Orange, ClientRectangle);
        }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,988 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 80,916 Reputation points
    2021-12-12T15:14:35.55+00:00

    You can use Custom Draw

    A minimal sample class where I draw it in orange (you can set the colors in properties or use bitmaps instead ) :

    156951-trackbar-color.jpg

        public class TrackBarCustom : TrackBar  
        {  
            public TrackBarCustom()  
            {  
            }  
      
            // custom draw item specs  
            private const int TBCD_TICS = 0x1;  
            private const int TBCD_THUMB = 0x2;  
            private const int TBCD_CHANNEL = 0x3;  
      
            [StructLayout(LayoutKind.Sequential)]  
            public struct NMHDR  
            {  
                public IntPtr hwndFrom;  
                public IntPtr idFrom;  
                public int code;  
            }  
      
            [StructLayout(LayoutKind.Sequential)]  
            public struct RECT  
            {  
                public int left;  
                public int top;  
                public int right;  
                public int bottom;  
            }  
      
            [StructLayout(LayoutKind.Sequential)]  
            public struct NMCUSTOMDRAW  
            {  
                public NMHDR hdr;  
                public int dwDrawStage;  
                public IntPtr hdc;  
                public RECT rc;  
                public IntPtr dwItemSpec;  
                public uint uItemState;  
                public IntPtr lItemlParam;  
            }  
      
            [Flags]  
            public enum CDRF  
            {  
                CDRF_DODEFAULT = 0x0,  
                CDRF_NEWFONT = 0x2,  
                CDRF_SKIPDEFAULT = 0x4,  
                CDRF_DOERASE = 0x8,  
                CDRF_SKIPPOSTPAINT = 0x100,  
                CDRF_NOTIFYPOSTPAINT = 0x10,  
                CDRF_NOTIFYITEMDRAW = 0x20,  
                CDRF_NOTIFYSUBITEMDRAW = 0x20,  
                CDRF_NOTIFYPOSTERASE = 0x40  
            }  
      
            [Flags]  
            public enum CDDS  
            {  
                CDDS_PREPAINT = 0x1,  
                CDDS_POSTPAINT = 0x2,  
                CDDS_PREERASE = 0x3,  
                CDDS_POSTERASE = 0x4,  
                CDDS_ITEM = 0x10000,  
                CDDS_ITEMPREPAINT = (CDDS.CDDS_ITEM | CDDS.CDDS_PREPAINT),  
                CDDS_ITEMPOSTPAINT = (CDDS.CDDS_ITEM | CDDS.CDDS_POSTPAINT),  
                CDDS_ITEMPREERASE = (CDDS.CDDS_ITEM | CDDS.CDDS_PREERASE),  
                CDDS_ITEMPOSTERASE = (CDDS.CDDS_ITEM | CDDS.CDDS_POSTERASE),  
                CDDS_SUBITEM = 0x20000  
            }  
      
            [DllImport("User32.dll", SetLastError = true)]  
            public static extern int FillRect(IntPtr hDC, ref RECT lpRect, IntPtr hBR);  
      
            [DllImport("Gdi32.dll", SetLastError = true)]  
            public static extern IntPtr CreateSolidBrush(int crColor);  
      
            [DllImport("Gdi32.dll", SetLastError = true)]  
            public static extern bool DeleteObject(IntPtr hObject);  
      
            protected override void WndProc(ref Message m)  
            {  
                if (m.Msg == WM_REFLECT + WM_NOFITY)  
                {  
                    var pnmhdr = (NMHDR)m.GetLParam(typeof(NMHDR));  
                    if (pnmhdr.code == NM_CUSTOMDRAW)  
                    {  
                        var pnmlv = (NMCUSTOMDRAW)m.GetLParam(typeof(NMCUSTOMDRAW));  
                        switch (pnmlv.dwDrawStage)  
                        {  
                            case (int)CDDS.CDDS_PREPAINT:  
                                {  
                                    m.Result = new IntPtr((int)CDRF.CDRF_NOTIFYITEMDRAW);  
                                    break;  
                                }  
      
                            case (int)CDDS.CDDS_ITEMPREPAINT:  
                                {  
                                    if (((int)pnmlv.dwItemSpec == TBCD_THUMB))  
                                    {   
                                        IntPtr hBrush = CreateSolidBrush(ColorTranslator.ToWin32(Color.Orange));  
                                        FillRect(pnmlv.hdc, ref pnmlv.rc, hBrush);  
                                        DeleteObject(hBrush);  
      
                                        m.Result = new IntPtr((int)CDRF.CDRF_SKIPDEFAULT);  
                                    }  
                                    else  
                                        m.Result = new IntPtr((int)CDRF.CDRF_NOTIFYPOSTPAINT);  
                                    break;  
                                }  
      
                            case (int)CDDS.CDDS_ITEMPOSTPAINT:  
                                {  
                                    m.Result = new IntPtr((int)CDRF.CDRF_DODEFAULT);  
                                    break;  
                                }  
                        }  
                    }  
                    return;  
                }  
                else  
                    base.WndProc(ref m);  
            }  
      
            private const int NM_FIRST = 0;  
            private const int NM_CLICK = NM_FIRST - 2;  
            private const int NM_CUSTOMDRAW = NM_FIRST - 12;  
            private const int WM_REFLECT = 0x2000;  
            private const int WM_NOFITY = 0x4E;  
        }  
      
    

0 additional answers

Sort by: Most helpful