Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 17: Track-Based Controls Visual C++ and MFC Fundamentals


17.3.4..Slider Methods.....................................................................................


As seen earlier, the slider control is based on the CSliderCtrl class. Therefore, we saw
that we can dynamically create a slider by declaring pointer to CSliderCtrl and call its
Create() method to initialize it. Once a slider has been designed and received the desired
style, you can programmatically use it and provide the necessary feedback to the user.

A slider is a control that provides a range of values between which the user can navigate
using the control’s thumb or by clicking on the slider’s line. Usually, the first aspect you
may need to configure on your control is to specify its limit values. To specify the
minimum value of a slider control, you can call the CSliderCtrl::SetRangeMin()
method. Its syntax is:

void SetRangeMin(int nMin, BOOL bRedraw = FALSE);

The nMin value is the new minimum value that the slider can assume. The control is
typically redrawn once the new value has been set. If you do not want the control to be
redrawn, pass a second argument with the FALSE value. If the lowest value of the
control has already been set and you want to find out what that value is, you can call the
CSliderCtrl::GetRangeMin() method. Its syntax is:

int GetRangeMin() const;

This method simply returns the lowest value that the slider can assume when the user has
dragged the thumb to the extreme left or bottom.

To set the highest value of a slider control, you can call the SliderCtrl::SetRangeMax()
method. Its syntax is:

void SetRangeMax(int nMax, BOOL bRedraw = FALSE);

The nMax argument holds the new maximum value for the control. Here is an example:

BOOL CControlsDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// TODO: Add extra initialization here
m_Slider.SetRangeMin(0);
m_Slider.SetRangeMax(50);

return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}

If the maximum value of the control had previously been set, you can find it out by
calling the SliderCtrl::GetRangeMax() method. Its syntax is:

int GetRangeMax() const;

This method returns the highest value that the slider control can have.

To set both the minimum and the maximum values of the slider with one line of code,
you can call the CSliderCtrl::SetRange() method. Its syntax is:
Free download pdf