Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
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.
Sr.No.
Name & Description
1
ClearSel
Clears the current selection in a slider control.
2
ClearTics
Removes the current tick marks from a slider control.
3
Create
Creates a slider control and attaches it to a CSliderCtrl object.
4
CreateEx
Creates a slider control with the specified Windows extended styles and attaches it to a CSliderCtrl object.
5
GetBuddy
Retrieves the handle to a slider control buddy window at a given location.
6
GetChannelRect
Retrieves the size of the slider control's channel.
7
GetLineSize
Retrieves the line size of a slider control.
8
GetNumTics
Retrieves the number of tick marks in a slider control.
9
GetPageSize
Retrieves the page size of a slider control.
10
GetPos
Retrieves the current position of the slider.
11
GetRange
Retrieves the minimum and maximum positions for a slider.
12
GetRangeMax
Retrieves the maximum position for a slider.
13.
GetRangeMin
Retrieves the minimum position for a slider.
14
GetSelection
Retrieves the range of the current selection.
15
GetThumbLength
Retrieves the length of the slider in the current trackbar control.
16
GetThumbRect
Retrieves the size of the slider control's thumb.
17
GetTic
Retrieves the position of the specified tick mark.
18
GetTicArray
Retrieves the array of tick mark positions for a slider control.
19
GetTicPos
Retrieves the position of the specified tick mark, in client coordinates.
20
GetToolTips
Retrieves the handle to the tooltip control assigned to the slider control, if any.
21
SetBuddy
Assigns a window as the buddy window for a slider control.
22
SetLineSize
Sets the line size of a slider control.
23
SetPageSize
Sets the page size of a slider control.
24
SetPos
Sets the current position of the slider.
25
SetRange
Sets the minimum and maximum positions for a slider.
26
SetRangeMax
Sets the maximum position for a slider.
27
SetRangeMin
Sets the minimum position for a slider.
28
SetSelection
Sets the range of the current selection.
29
SetThumbLength
Sets the length of the slider in the current trackbar control.
30
SetTic
Sets the position of the specified tick mark.
31
SetTicFreq
Sets the frequency of tick marks per slider control increment.
32
SetTipSide
Positions a tooltip control used by a trackbar control.
33
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.