Visual C++ and MFC Fundamentals Chapter 17: Track-Based Controls
void SetRange(int nMin, int nMax, BOOL bRedraw = FALSE);
The nMin and the nMax arguments hold the lowest and the highest respective values of
the control. Here is an example:
BOOL CControlsDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
m_Slider.SetRange(0, 50);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
If the control is already functioning and you want to know its limit values, you can call
the CSliderCtrl::SetRange() method whose syntax is:
void GetRange(int& nMin, int& nMax) const;
This method returns two values, namely the lowest value, as nMin, and the highest value,
as nMax.
Once the minimum and maximum values have been set, the user can slide the thumb to
select a value or a range. This value is what mostly interests you. While sliding the
thumb, the value of the slider is called its position. At startup or at any time, you can set a
specific position for the thumb. This can be done by calling the CSliderCtrl::SetPos()
method. Its syntax is:
void SetPos(int nPos);
The nPos argument holds the new position of the slider. Here is an example:
BOOL CControlsDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
m_Slider.SetRange(0, 50);
m_Slider.SetPos(32);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
The value specified using the SetPos() method should be in the range nMax – nMin of the
SetRange() method. If there is a possibility that this value is outside the valid range, you
can call the CSliderCtrl::VerifyPos() method to check it. Its syntax is:
void VerifyPos( );
When the position of the thumb has change and you want to find out what it is, call the
CSliderCtrl::GetPos() method whose syntax is:
int GetPos() const;