Creating a Trackbar

You can create a trackbar by using the CreateWindow or CreateWindowEx function and specifying the TRACKBAR_CLASS class name or by using the MFC Create member function on your CSliderCtrl object. You can then use trackbar messages or member functions to set the minimum and maximum positions for the slider, draw tick marks, and set a selection range. To use trackbars in your application, you must include the COMMCTRL.H header file, and you also need to have the COMCTL32.LIB file in your list of libraries. This C code creates the status bar and three trackbars in the SLIDER sample:

case WM_CREATE:

hWndStatus = CreateWindow (
STATUSCLASSNAME,
"",
WS_CHILD | WS_BORDER | WS_VISIBLE,
-100, -100, 10, 10,
hWnd,
(HMENU)100,
hInst,
NULL);

hWndSlider1 = CreateWindow (
TRACKBAR_CLASS,
"",
WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS,
10, 50, 200, 20,
hWnd,
(HMENU)10,
hInst,
NULL);

if (hWndSlider1 == NULL)
MessageBox (NULL, "Slider1 not created!", NULL, MB_OK);

hWndSlider2 = CreateWindow (
TRACKBAR_CLASS,
"",
WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_TOP |
TBS_ENABLESELRANGE,
10, 100, 200, 20,
hWnd,
(HMENU)11,
hInst,
NULL);

if (hWndSlider2 == NULL)
MessageBox (NULL, "Slider2 not created!", NULL, MB_OK);

hWndSlider3 = CreateWindow (
TRACKBAR_CLASS,
"",
WS_CHILD | WS_VISIBLE | TBS_VERT | TBS_LEFT | TBS_AUTOTICKS,
10, 150, 20, 100,
hWnd,
(HMENU)12,
hInst,
NULL);

if (hWndSlider3 == NULL)
MessageBox (NULL, "Slider3 not created!", NULL, MB_OK);

// Set the default range.
SendMessage (hWndSlider1, TBM_SETRANGE, TRUE, MAKELONG (1,10));
SendMessage (hWndSlider2, TBM_SETRANGE, TRUE, MAKELONG (1,10));
SendMessage (hWndSlider3, TBM_SETRANGE, TRUE, MAKELONG (1,10));

// Set the selection range.
SendMessage (hWndSlider2, TBM_SETSEL, TRUE, MAKELONG (3,5));
break;

In the MFC version, MFCTRACK, I created the trackbar in the view class. I also needed to include the AFXCMN.H file in my STDAFX.H file.

// The view class is defined as follows in MFCTRVW.H.
class CMfctrackView : public CView
{
protected: // create from serialization only
CMfctrackView ();
DECLARE_DYNCREATE (CMfctrackView);
CSliderCtrl m_Slider1;
CSliderCtrl m_Slider2;
CSliderCtrl m_Slider3;

// Attributes
public:
CMfctrackDoc *GetDocument ();

// Operations
public:
CSliderCtrl *GetSlider (int iSlider);

// Overrides
// ClassWizard generated virtual function overrides.
// {{AFX_VIRTUAL (CMfctrackView)
public:
virtual void OnDraw (CDC *pDC); // overridden to
// }}AFX_VIRTUAL // draw this view

// Implementation
public:
virtual ~CMfctrackView ();
#ifdef _DEBUG
virtual void AssertValid () const;
virtual void Dump (CDumpContext& dc) const;
#endif

protected:
VOID TrackScrolling (UINT nSBCode);

// Generated message map functions
protected:
// {{AFX_MSG (CMfctrackView)
afx_msg int OnCreate (LPCREATESTRUCT lpCreateStruct);
afx_msg void OnDestroy ();
afx_msg void OnHScroll (UINT nSBCode, UINT nPos,
CScrollBar *pScrollBar);
afx_msg void OnRange ();
afx_msg void OnFrequency ();
afx_msg void OnLinesize ();
afx_msg void OnPagesize ();
afx_msg void OnVScroll (UINT nSBCode, UINT nPos,
CScrollBar *pScrollBar);
// }}AFX_MSG
DECLARE_MESSAGE_MAP ()
};

// The trackbars are created in MFCTRVW.CPP.
int CMfctrackView::OnCreate (LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate (lpCreateStruct) == -1)
return -1;

// Create the trackbars.
m_Slider1.Create (WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS,
CRect (10, 50, 200, 70),
this,
ID_SLIDER1);

m_Slider2.Create (WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_TOP |
TBS_ENABLESELRANGE,
CRect (10, 100, 200, 130),
this,
ID_SLIDER2);

m_Slider3.Create (WS_CHILD | WS_VISIBLE | TBS_VERT | TBS_LEFT |
TBS_AUTOTICKS,
CRect (10, 150, 30, 350),
this,
ID_SLIDER3);

// Set the default range.
m_Slider1.SetRange (1, 10, TRUE);
m_Slider2.SetRange (1, 25, TRUE);
m_Slider3.SetRange (1, 30, TRUE);

// Set the selection range.
m_Slider2.SetSelection (10, 20);

return 0;
}