如何在MFC中使用滑块控件 [英] How to use Slider Control in MFC
本文介绍了如何在MFC中使用滑块控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hello Everyone,
我在MFC工具的对话框中放置了一个Slider控件。我想要的是当用户移动滑块时,我想要用户将滑块放在轨迹栏上的位置/值(在运行时)并将该值发送到USB端口以及在编辑框中显示该值。
我想在用户移动并放置滑块时立即获取值。如何在运行时获取值?
请给我一些建议。
Hello Everyone,
I have placed a Slider control on the dialog box from the MFC Tools. What I want is when the user moves the slider, I want the position / value where user has placed the slider on track bar (at runtime) and Send that value to USB Port as well as display that value in an edit box.
I want the value as soon as user moves and places the slider. How to get the value at runtime?
Please suggest me something.
推荐答案
只需处理OnHScroll()
或OnVScroll()
消息,如滑块通知消息 [ ^ ]。当用户移动滑块并释放鼠标按钮时,会发送TB_THUMBPOSITION
通知(nSBCode
滚动处理程序参数)。该位置在nPos
参数中传递,并在pScrollBar
参数中指向滑块控件的指针(必须进行转换)到CSliderCtrl *
)。
[更新:示例代码]
如果没有到目前为止,将WM_HSCROLL
处理程序添加到对话框中,并为滑块添加一个成员变量:
Just handle theOnHScroll()
orOnVScroll()
message as described in Slider notification messages[^]. When the user moves the slider and releases the mouse button, aTB_THUMBPOSITION
notification is send (nSBCode
parameter of the scroll handler). The position is passed in thenPos
parameter and a pointer to the slider control in thepScrollBar
parameter (this must be casted toCSliderCtrl*
).
[UPDATE: Example code]
If not done so far, add theWM_HSCROLL
handler to your dialog and optional a member variable for the slider:
void CMyDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
if (IDC_SLIDER == pScrollBar->GetDlgCtrlID())
{
// When there is no member variable and we need to access the control
// CSliderCtrl* pSlider = reinterpret_cast<CSliderCtrl*>(pScrollBar);
// Handle event here
switch (nSBCode)
{
case TB_LINEUP:
case TB_LINEDOWN:
case TB_PAGEUP:
case TB_PAGEDOWN:
case TB_THUMBPOSITION:
case TB_TOP:
case TB_BOTTOM:
case TB_THUMBTRACK:
case TB_ENDTRACK:
default:
break;
}
return;
}
// Default handling if not a slider control.
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
这篇关于如何在MFC中使用滑块控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文