After placing a slider control on a dialog box, by default, it assumes a horizontal position. If you want a vertical slider, change the value of the Orientation property. If you were dynamically creating the control, its default orientation would be horizontal whose style is TBS_HORZ. If you want a vertical slider, apply the TBS_VERT style: BOOL CDlgSlide::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
CSliderCtrl *Taskbar = new CSliderCtrl;
Taskbar->Create(WS_CHILD | WS_VISIBLE | TBS_VERT,
CRect(20, 20, 60, 280), this, IDC_SLIDER_CONTROL);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
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() member function. 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() member function. Its syntax is: int GetRangeMin() const; This member function 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 CSliderCtrl::SetRangeMax() member function. 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() member function. Its syntax is: int GetRangeMax() const; This member function 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() member function. Its syntax is: 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() member function whose syntax is: void GetRange(int& nMin, int& nMax) const; This member function 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() member function. 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() member function should be in the range nMax - nMin of the SetRange() member function. If there is a possibility that this value is outside the valid range, you can call the CSliderCtrl::VerifyPos() member function 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() member function whose syntax is: int GetPos() const; 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() member function. Its syntax is: void SetSelection(int nMin, int nMax); When calling this member function, 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() member function whose syntax is: void GetSelection(int& nMin, int& nMax) const; This member function 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() member function. Its syntax is: void SetTicFreq(int nFreq);
The new slider appears as one line, horizontal or vertical, that guides the user with the area to slide the thumb. When sliding the thumb along the line, the user can set only the value where the thumb is positioned. Alternatively, if you want the user to be able to select a range of values instead of just a value, at design time, you can set the Enable Selection Range property to True. This is equivalent to adding the TBS_ENABLESELECTION style. A slider equipped with this style displays a 3-D "whole" in the body of the slider:
The selection area allows the user to select a range of values.
The thumb of a slider can assume one of three shapes. By default, it appears as a rectangular box. Alternatively, you can convert one of its shorter borders to appear as an arrow. The shape of the thumb is controlled at design time by the Point property. Its default value is Both, which gives it a rectangular shape. You can get this same shape by omitting or adding the TBS_BOTH value. For a horizontal slider, you can make the thumb's arrow point to the left by changing the Point property to Top/Left. If the slider were horizontal, this Point value would orient the thumb arrow to the top:
To make the thumb of a dynamically created horizontal slider point up, add the TBS_TOP. If the slider is vertical, to point its thumb to the left, add the TBS_LEFT style to it. If you want the thumb to point down for a horizontal slider, set the Point property to Bottom/Right. This same value would make the thumb of a vertical slider point n the right direction:
To point the thumb up for a horizontal slider you are programmatically creating, add the TBS_BOTTOM. For the thumb of a vertical slider to point right, add the TBS_RIGHT to it.
If you want to guide the user with some ticks on the control, at design time, set the Tick Marks property to True. If you are dynamically creating the slider and you want it to display ticks, simply adding the either the TBS_VERT or the TBS_HORZ style equips the slider with ticks. If you do not want to display the ticks at all, at design time, clear the Tick Marks property or set its value to False. The ticks are positioned on the side the thumb is pointing. If the slider is created with the Both value for the Point property or the TBS_BOTH style, the ticks would appear on both sides the thumb. The thumb of a slider is used to scroll from one minimum value to another. The range of these extreme values can be divided in regular increments that can further guide the user with value selection. To display where these increments are set, at design time, set the Auto Ticks property to True or add the TBS_AUTOTICKS style:
On its own, the slider controls can send three notification messages:
For its functionality, the slider highly relies on its
parent. When the user clicks the thumb or any part of the slider, which
causes it to slide, a scroll event is fired. If the slider is horizontal,
the CWnd::OnHScroll() event is sent. If the slider is
vertical, the CWnd::OnVScroll() event is sent.
|
|
|||||||||||||||||||||||||||||||||||||||
|