我正在写一个基于C++ MFC对话框的应用程序,我的程序有很多滑块。我希望程序根据用户正在更改哪个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()

...

最佳答案

滑块控件在水平或垂直滚动​​时发送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;
    }

//...
}
`

10-06 06:09