让MFC滑块响应鼠标左键消息

网上找了一大堆的资料,都说MFC滑块不能直接自己响应鼠标左键消息,而且滑块拖动的时候,由控件发消息给其主窗口,由主窗口来相应鼠标拖动。所以必须在主窗口添加滚动条相应函数(滑块属于滚动条的一种)。

细看了下msdn关于滑块关于消息的描述分析,有些收获:

A slider control notifies its parent window of user actions by sending the parentWM_HSCROLL orWM_VSCROLL messages, depending on the orientation of the slider control. To handle these messages, add handlers for theWM_HSCROLL andWM_VSCROLL messages to the parent window. TheOnHScroll and OnVScroll member functions will be passed a notification code, the position of the slider, and a pointer to theCSliderCtrl object. Note that the pointer is of type CScrollBar * even though it points to aCSliderCtrl object. You may need to typecast this pointer if you need to manipulate the slider control.

Rather than using the scroll bar notification codes, slider controls send a different set of notification codes. A slider control sends theTB_BOTTOM,TB_LINEDOWN,TB_LINEUP, andTB_TOP notification codes only when the user interacts with a slider control by using the keyboard. TheTB_THUMBPOSITION andTB_THUMBTRACK notification messages are only sent when the user is using the mouse. TheTB_ENDTRACK,TB_PAGEDOWN, andTB_PAGEUP notification codes are sent in both cases.

对于鼠标滑块操作有两个最重要的消息,TB_THUMBPOSITION and TB_THUMBTRACK ,关于这两个消息,msdn有说明:

Notification message

Event causing notification to be sent

TB_BOTTOM

VK_END

TB_ENDTRACK

WM_KEYUP (the user released a key that sent a relevant virtual key code)

TB_LINEDOWN

VK_RIGHT or VK_DOWN

TB_LINEUP

VK_LEFT or VK_UP

TB_PAGEDOWN

VK_NEXT (the user clicked the channel below or to the right of the slider)

TB_PAGEUP

VK_PRIOR (the user clicked the channel above or to the left of the slider)

TB_THUMBPOSITION

WM_LBUTTONUP following a TB_THUMBTRACK notification message

TB_THUMBTRACK

Slider movement (the user dragged the slider)

TB_TOP

VK_HOME

先说后一个,当用户拖动滑块头 的时候,这个消息马上就会响应,如果你一直拖,就会引发很多次这个消息。直到你放开左键,这个时候滑块就会发送前一个消息给主窗口。所以如果你只想想响应鼠标左键松开这个消息,就可以对相应的消息进行过滤即可(只响应TB_THUMBPOSITION)。

参考代码如下:

void C***Dlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	switch (nSBCode)
	{
	case TB_THUMBTRACK: 
		break;
	case TB_THUMBPOSITION:
	case TB_PAGEDOWN:
	case TB_PAGEUP:
		if (pScrollBar->GetSafeHwnd() == m_mySlider.GetSafeHwnd())//如果拖动的是滑块
		{
		}			
	
		break;
   	case SB_ENDSCROLL:
		return;
	}
	CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar);
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值