更改特定滑块时的事件

发布于 2024-12-02 11:23:34 字数 1027 浏览 1 评论 0原文

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

...

I am writing a C++ MFC Dialog based Application and my program has lots of sliders. I want the program to call a function depending on which Slider is being changed by the user. I tried using GetPos() but not much success so far. Any easier way of doing this?

Message Map:

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()

...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

邮友 2024-12-09 11:23:34

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

//...  
}  
`

Slider controls send WM_HSCROLL or WM_VSCROLL notifications when they are scrolled, horizontally or vertically. Catch them in your dialog and there you can call your desired function, depending on who sent the notification.

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;  
    }

//...  
}  
`
凉世弥音 2024-12-09 11:23:34
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);
}
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);
}
提笔书几行 2024-12-09 11:23:34

我想,我想通了。您所说的滑块通常称为“滚动条”。您可能正在寻找 WM_VSCROLL信息。如此处所述,“lParam:如果消息是通过滚动条发送的,则此参数是滚动条控件的句柄。”

另请参阅CWnd::OnVScroll

I figured it out, I think. What you call a slider is commonly called a "Scrollbar". You're probably looking for the WM_VSCROLL message. As noted there, "lParam: If the message is sent by a scroll bar, this parameter is the handle to the scroll bar control."

See also CWnd::OnVScroll

夏末 2024-12-09 11:23:34

对于不同的控件,您确实有不同的 ON_CONTROL 宏吗?因为只需指定不同的方法作为 ON_CONTROL 的第三个参数即可

You do have different ON_CONTROL macro's for the different controls? Because it's then just a matter of specifying different methods as the third argument to ON_CONTROL

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文