12

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

...

Neophile
  • 5,660
  • 14
  • 61
  • 107

4 Answers4

22

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

//...  
}  
`
MikMik
  • 3,426
  • 2
  • 23
  • 41
4
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);
}
SChalice
  • 502
  • 4
  • 7
3

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

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Yes, you are absolutely right. I added the WM_VSCROLL function and edited it according the other answer that was provided. But really thanks a lot for your input. Works like a charm now :) – Neophile Sep 01 '11 at 15:01
2

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

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Hello, I tried doing it this way but did not really help much. I just added the ON_CONTROL bit in the beginning. Slider Control has CUstomDraw, OutofMemory, releasedcapture and themechanged when trying to add an event handler. Not sure if anything can be used from these. – Neophile Sep 01 '11 at 13:18
  • 1
    ON_CONTROL _bit_ ? I don't know that one, I'm talking about [the macro](http://msdn.microsoft.com/en-us/library/7hkb8hew.aspx) – MSalters Sep 01 '11 at 13:36
  • Yes, I meant the same not the bit. Unfortunately, it did not work. – Neophile Sep 01 '11 at 13:51
  • 1
    Then please show us your message map (with sufficient context). – MSalters Sep 01 '11 at 13:53