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

来源:互联网 发布:三国杀卡牌制作软件 编辑:程序博客网 时间:2024/06/03 19:04

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

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

 

[cpp] view plaincopyprint?
  1. void MySlider::OnLButtonDown(UINT nFlags, CPoint point)   
  2. {  
  3.     // TODO: Add your message handler code here and/or call default  
  4.     CSliderCtrl::OnLButtonDown(nFlags, point);  
  5.     CRect   rectClient,rectChannel;   
  6.     GetClientRect(rectClient);   
  7.     GetChannelRect(rectChannel);   
  8.     int nMax = 0;  
  9.     int nMin = 0;  
  10.     GetRange(nMin,nMax);  
  11.     int nPos =     
  12.         (nMax - nMin)*(point.x - rectClient.left - rectChannel.left)/(rectChannel.right - rectChannel.left);   
  13.     SetPos(nPos);  
  14.       
  15. }  


在主界面中:(***Dlg.h)

添加:#include "MySlider.h"

MySlider m_MySlider;

 

(***Dlg.cpp)  关联变量。。。

[cpp] view plaincopyprint?
  1. void CCSliderPosDlg::DoDataExchange(CDataExchange* pDX)  
  2. {  
  3.     CDialog::DoDataExchange(pDX);  
  4.     //{{AFX_DATA_MAP(CCSliderPosDlg)   
  5.         // NOTE: the ClassWizard will add DDX and DDV calls here  
  6.     //}}AFX_DATA_MAP   
  7.     DDX_Control(pDX,IDC_SLIDER1,m_MySlider);   
  8. }  

OnInitDialog()

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