VC Slider控件,根据鼠标单击位置来显示滑块位置!

来源:互联网 发布:淘宝网信用评价体系 编辑:程序博客网 时间:2024/06/06 02:33

首先自己写一个MySlider类。。该类继承 CSliderCtrl类。。

MySlider类响应LButtonDown消息来实现鼠标单击定位。。。

 

void MySlider::OnLButtonDown(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call defaultCSliderCtrl::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_MAPDDX_Control(pDX,IDC_SLIDER1,m_MySlider); }

OnInitDialog()

m_MySlider.SetRange(0,100); //设置Slider的范围。。。