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

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

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

 

  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. }  
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)  关联变量。。。

  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. }  
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的范围。。。

 

 

 

1楼 a117653909 2012-03-01 14:21发表 [回复] [引用] [举报]
博主那样写得有点小问题,应该这样写
void MySlider::OnLButtonDown(UINT nFlags, CPoint 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);
CSliderCtrl::OnLButtonDown(nFlags, point);
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值