Chapter 17: Track-Based Controls Visual C++ and MFC Fundamentals
If the slider control was specified to let the user select a range, you can define your own
selected range at any time by calling the CSliderCtrl::SetSelection() method. Its syntax
is:
void SetSelection(int nMin, int nMax);
When calling this method, make sure you specify the nMin and the nMax values so that
this nMin is greater than the minimum value of the slider and this nMax is less than the
highest possible value of the slider. Furthermore, the value of this nMin must be less than
that of nMax. This relationship can be illustrated as follows:
Minimum <= nMin < nMax <= Maximum
Here is an example:
BOOL CControlsDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
m_Slider.SetRange(0, 50);
m_Slider.SetPos(32);
m_Slider.SetSelection(22, 42);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
If a selected range has been performed on the slider, if you want to get the minimum and
the maximum values of the selection, you can call the CSliderCtrl::GetSelection()
method whose syntax is:
void GetSelection(int& nMin, int& nMax) const;
This method returns two values, the minimum as nMin and the maximum as nMax.
If the slider control is configured to display ticks, you can specify their frequency with a
call to the CSliderCtrl::SetTicFreq() method. Its syntax is:
void SetTicFreq(int nFreq);
Practical Learning: Using Track Bar Events
- In the OnInitDialog event of the dialog class, set the range of values of the slider to 0
to 9 and the frequency of its ticks to:
m_CarSlider.SetRange(1, 10);
m_CarSlider.SetTicFreq(1);