MFC - Slider Controls

A Slider Control (also known as a trackbar) is a window containing a slider and optional tick marks. When the user moves the slider, using either the mouse or the direction keys, the control sends notification messages to indicate the change. There are two types of sliders — horizontal and vertical. It is represented by CSliderCtrl class.
ClearSel
Clears the current selection in a slider control.
ClearTics
Removes the current tick marks from a slider control.
Create
Creates a slider control and attaches it to a CSliderCtrl object.
CreateEx
Creates a slider control with the specified Windows extended styles and attaches it to a CSliderCtrl object.
GetBuddy
Retrieves the handle to a slider control buddy window at a given location.
GetChannelRect
Retrieves the size of the slider control's channel.
GetLineSize
Retrieves the line size of a slider control.
GetNumTics
Retrieves the number of tick marks in a slider control.
GetPageSize
Retrieves the page size of a slider control.
GetPos
Retrieves the current position of the slider.
GetRange
Retrieves the minimum and maximum positions for a slider.
GetRangeMax
Retrieves the maximum position for a slider.
GetRangeMin
Retrieves the minimum position for a slider.
GetSelection
Retrieves the range of the current selection.
GetThumbLength
Retrieves the length of the slider in the current trackbar control.
GetThumbRect
Retrieves the size of the slider control's thumb.
GetTic
Retrieves the position of the specified tick mark.
GetTicArray
Retrieves the array of tick mark positions for a slider control.
GetTicPos
Retrieves the position of the specified tick mark, in client coordinates.
GetToolTips
Retrieves the handle to the tooltip control assigned to the slider control, if any.
SetBuddy
Assigns a window as the buddy window for a slider control.
SetLineSize
Sets the line size of a slider control.
SetPageSize
Sets the page size of a slider control.
SetPos
Sets the current position of the slider.
SetRange
Sets the minimum and maximum positions for a slider.
SetRangeMax
Sets the maximum position for a slider.
SetRangeMin
Sets the minimum position for a slider.
SetSelection
Sets the range of the current selection.
SetThumbLength
Sets the length of the slider in the current trackbar control.
SetTic
Sets the position of the specified tick mark.
SetTicFreq
Sets the frequency of tick marks per slider control increment.
SetTipSide
Positions a tooltip control used by a trackbar control.
SetToolTips
Assigns a tooltip control to a slider control.
Let us look into a simple example by creating a new MFC dialog based project.
Step 1 − Once the project is created you will see the TODO line which is the Caption of Text Control. Remove the Caption and set its ID to IDC_STATIC_TXT.
Step 2 − Add a value variable m_strSliderVal for the Static Text control.
Step 3 − Drag the slider control from the Toolbox.
Step 4 − Add a control variable m_sliderCtrl for slider.
Step 5 − Go to the class view in solution.
Step 6 − Select the CMFCSliderControlDlg class.
Step 7 − In the Properties window, click Messages.
Step 8 − Scroll down to "WM_HSCROLL" and click on the drop-down menu. Click "<Add> OnHScroll".
Step 9 − Initialize the Slider and Static Text control inside the OnInitDialog() function.
BOOL CMFCSliderControlDlg::OnInitDialog() {
   CDialogEx::OnInitDialog();
  
   // Set the icon for this dialog. The framework does this automatically
   // when the application's main window is not a dialog
   SetIcon(m_hIcon, TRUE);            // Set big icon
   SetIcon(m_hIcon, FALSE);            // Set small icon

   // TODO: Add extra initialization here
   m_sliderCtrl.SetRange(0, 100, TRUE);
   m_sliderCtrl.SetPos(0);
   m_strSliderVal.Format(_T("%d"), 0);

   return TRUE; // return TRUE unless you set the focus to a control
}
Step 10 − Add the following code inside the function code block for OnVScroll()
void CMFCSliderControlDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) {
   // TODO: Add your message handler code here and/or call default
   if (pScrollBar == (CScrollBar *)&m_sliderCtrl) {
      int value = m_sliderCtrl.GetPos();
      m_strSliderVal.Format(_T("%d"), value);
      UpdateData(FALSE);
   }else {
      CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
   }
}
Step 11 − When the above code is compiled and executed, you will see the following output.
 Print
 Add Notes
 Bookmark this page