参考文章:
-
https://blog.csdn.net/zhangyulin54321/article/details/8758934?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-4.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-4.control
-
https://blog.csdn.net/segen_jaa/article/details/7019766?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control
实现代码
* 实现:添加mySlider类,继承与CSliderCtrl,响应WM_LBUTTONDOWN消息
**功能:实现鼠标点击滑动条定位到鼠标位置,且精确定位
**************************************************/
//方法一:不能精确定位,效果混乱
//void MySlider::OnLButtonDown(UINT nFlags, CPoint point)
//{
// // TODO: 在此添加消息处理程序代码和/或调用默认值
//
// CSliderCtrl::OnLButtonDown(nFlags, point);
//}
//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);
//
//}
//方法一:精确定位,效果理想
void MySlider::OnLButtonDown(UINT nFlags, CPoint point)
{
// 按键在滑动块区域内,不做操作
CRect tRect;
GetThumbRect(&tRect);
if (tRect.PtInRect(point))
{
CSliderCtrl::OnLButtonDown(nFlags, point);
return;
}
// 实现滑动块精确定位
CRect rcSlider;
GetChannelRect(&rcSlider);
int nMax = GetRangeMax();
float fInterval = float(rcSlider.Width()) / nMax;
float fPos = (point.x - rcSlider.left + fInterval - 1) / fInterval;
CSliderCtrl::SetPos((int)fPos);
}