我正在编写基于C ++ MFC Dialog的应用程序,我的程序有很多滑块。我希望程序根据用户更改的Slider调用函数。我尝试使用GetPos()但到目前为止没有太大的成功。这样做更容易吗?
消息地图:
BEGIN_MESSAGE_MAP(CSerialPortDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
//ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
ON_BN_CLICKED(IDC_READ_COMM, OnBnClickedReadComm)
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_WRITE, OnBnClickedWrite)
//ON_CBN_SELCHANGE(IDC_SENSORS, OnCbnSelchangeSensors)
//ON_CBN_SELCHANGE(IDC_SENSOR_LIST, OnCbnSelchangeSensorList)
ON_BN_CLICKED(IDC_GO, OnGo)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_KILL_TIMER, OnBnClickedKillTimer)
ON_BN_CLICKED(IDC_READ_TIMER, OnBnClickedReadTimer)
ON_BN_CLICKED(IDC_WRITE_COMM, OnBnClickedWriteComm)
ON_BN_CLICKED(IDC_TERMINATE, OnBnClickedTerminate)
ON_BN_CLICKED(IDC_RUN, OnBnClickedRun)
ON_CONTROL(NM_CLICK,IDC_BOOM_SLIDER, Write_Boom)
ON_CONTROL(NM_CLICK,IDC_PITCH_SLIDER, Write_Pitch)
END_MESSAGE_MAP()
...
答案 0 :(得分:19)
滑块控件在水平或垂直滚动时发送WM_HSCROLL或WM_VSCROLL通知。在对话框中抓住它们,您可以根据发送通知的人来调用您想要的功能。
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
//...
ON_WM_HSCROLL()
//...
END_MESSAGE_MAP()
//////////////////////////
// nSBCode: The operation performed on the slider
// nPos: New position of the slider
// pScrollBar: The scrollbar (slider ctrl in this case) that sent the notification
void CMyDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
CSliderCtrl* pSlider = reinterpret_cast<CSliderCtrl*>(pScrollBar);
// Check which slider sent the notification
if (pSlider == &c_Slider1)
{
}
else if (pSlider == &c_Slider2)
{
}
// Check what happened
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;
}
//...
}
`
答案 1 :(得分:3)
我想,我想出来了。你称之为滑块的通常称为&#34;滚动条&#34;。您可能正在寻找WM_VSCROLL
消息。如上所述,&#34; lParam :如果消息是通过滚动条发送的,则此参数是滚动条控件的句柄。&#34;
答案 2 :(得分:2)
对于不同的控件,您确实有不同的ON_CONTROL
宏吗?因为这只是指定不同方法作为ON_CONTROL
答案 3 :(得分:2)
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
//...
ON_WM_HSCROLL()
//...
END_MESSAGE_MAP()
void CMyDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
CSliderCtrl *ACSliderCtrl = (CSliderCtrl *)pScrollBar;
int nID = ACSliderCtrl->GetDlgCtrlID();
int NewPos = ((CSliderCtrl *)pScrollBar)->GetPos();
CWnd *ACWnd = GetDlgItem(nID);
switch (nID)
{
default:
break;
case IDC_SLIDER1:
m_edit1.Format( "%d", NewPos );
UpdateData(FALSE);
break;
}
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}