细看了下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);
}