首先自己写一个MySlider类。。该类继承 CSliderCtrl类。。
MySlider类响应LButtonDown消息来实现鼠标单击定位。。。
- void MySlider::OnLButtonDown(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- CSliderCtrl::OnLButtonDown(nFlags, point);
- CRect rectClient,rectChannel;
- GetClientRect(rectClient);
- GetChannelRect(rectChannel);
- int nMax = 0;
- int nMin = 0;
- GetRange(nMin,nMax);
- int nPos =
- (nMax - nMin)*(point.x - rectClient.left - rectChannel.left)/(rectChannel.right - rectChannel.left);
- SetPos(nPos);
- }
在主界面中:(***Dlg.h)
添加:#include "MySlider.h"
MySlider m_MySlider;
(***Dlg.cpp) 关联变量。。。
- void CCSliderPosDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CCSliderPosDlg)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- //}}AFX_DATA_MAP
- DDX_Control(pDX,IDC_SLIDER1,m_MySlider);
- }
OnInitDialog()
{
m_MySlider.SetRange(0,100); //设置Slider的范围。。。
}
自定义滑块控件
本文介绍如何通过创建自定义的MySlider类实现鼠标单击定位功能。该类继承自CSliderCtrl,并重写了OnLButtonDown方法以实现特定功能。文章还提供了在主界面中使用该自定义滑块的具体步骤。
4334

被折叠的 条评论
为什么被折叠?



