The Slider Control
来源:互联网 发布:广东碣石翻新衣服淘宝 编辑:程序博客网 时间:2024/04/30 19:03
A slider is a Windows control equipped with a small bar, also called a thumb, that slides along a visible line. There are two types of sliders: horizontal and vertical: To use the slider control, the user can drag the thumb in one of two directions. If the slider is horizontal, the user can drag the thumb left or right. The thumb of a vertical slider can be dragged up or down. This changes the position of the thumb. The user can also click the desired position along the line to place the thumb at the desired location. Alternatively, when the slider has focus, the user can also use the arrow keys of the keyboard to move the thumb. The ticks can visually guide the user for the available positions of the thumb mark. A slider can be used to let the user specify a value that conforms to a range. When equipped with ticks, a slider can be used to control exact values that the user can select in a range, preventing the user from setting just any desired value.
To provide a slider to an application, from the Controls toolbox, click the Slider button and click the desired area on the dialog box or the form. BOOL CDlgSlide::OnInitDialog() {CDialog::OnInitDialog();// TODO: Add extra initialization hereCSliderCtrl *TrackBar = new CSliderCtrl;TrackBar->Create(WS_CHILD | WS_VISIBLE,CRect(15, 20, 222, 50), this, 0x14);return TRUE; // return TRUE unless you set the focus to a control// EXCEPTION: OCX Property Pages should return FALSE}
After placing a slider control on a form or other host, by default, its assumes a horizontal direction. If you want a vertical slider, change the value of the Orientation property. If you are 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 hereCSliderCtrl *TrackBar = new CSliderCtrl;TrackBar->Create(WS_CHILD | WS_VISIBLE | TBS_VERT,CRect(20, 20, 50, 250), this, 0x14);return TRUE; // return TRUE unless you set the focus to a control// EXCEPTION: OCX Property Pages should return FALSE} 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 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. To make the thumb of a dynamically created horizontal slider point up, add the TBS_TOP style. If the slider is vertical, to point its thumb to the left, add the TBS_LEFT style to it. 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.
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. 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 don't 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. 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 herem_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. 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 herem_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 herem_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: BOOL CControlsDlg::OnInitDialog() {CDialog::OnInitDialog();// TODO: Add extra initialization herem_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 SetTicFreq(int nFreq);
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.
|
- The Slider Control
- slider control
- VC++ Slider Control
- Slider Control控件
- MFC-控件大全-Slider Control
- MFC-控件大全-Slider Control
- MFC-控件大全-Slider Control
- MFC的Slider control使用
- MFC-控件大全-Slider Control
- MFC-控件大全-Slider Control
- Spin Control 和 Slider Control 的用法
- 滑动条控制(Slider Control)
- Ajax Control Toolkit--Slider:有朝一日倒过来
- VC 透明滑动控件Slider Control
- MFC-控件大全-Slider Control(转)
- Color Slider Control的颜色计算
- MFC-控件大全-Slider Control(转)
- 滑块控件 (Slider Control) 样式
- The Color Changer
- CSliderCtrl类的成员函数
- 放浪
- CProgressCtrl类提供了少量的成员函数用来设置进度条
- 鸡年的工作第一天
- The Slider Control
- 兼顾浏览器兼容性标准
- 哇!有黑客! (振兴水版贴) 【安焦elly】
- aaa
- ftp[转]
- 在碎玻璃上慢慢爬的C++程序员
- CVS for linux安装!
- 为什么不喜欢写总结
- 大家好,我是小宝贝!