MFC Quick Start Guide (Part 4): Slider Bar Control

  1. Open the Toolbox tab, select Slider Control and drag it to your dialog

    (Fig. 4.1: Slider Control)
  2. Do it three times


    (Fig. 4.2: Three Sliders)
  3. Rename the IDs of them to be IDC_RED_SLIDER, IDC_GREEN_SLIDER and IDC_BLUE_SLIDER
  4. Right click on the edit box first slider bar and choose Add variable


    (Fig. 4.3: Add variable)
  5. Input the data according to the following setting:
    1. Access: private
    2. Check the box “control variable”
    3. Category: value
    4. Variable Type: CString
    5. Variable name: m_RedSliderEcho


      (Fig. 4.4: Setting)
  6. Repeat step 5 similarly for the other edit boxes
  7. Make sure the Orientation field in the Property panel is horizontal

    (Fig. 4.5: Horizontal)
  8. Right click on the first slider ctrl and choose Add variable
  9. Input the data according to the following setting:
    1. Access: private
    2. Check the box “control variable”
    3. Category: control
    4. Variable Type: CSliderCtrl
    5. Variable name: m_RedSliderCtrl


      (Fig. 4.6: Setting)
  10. Repeat step 9 similarly for the other 2 slider bars

Adding Event Handler

  1. Find the line “BEGIN_MESSAGE_MAP(CMFC101Dlg, CDialogEx)” in MFC101Dlg.cpp
  2. Add this following line of code:

    ON_WM_HSCROLL()
    


    (Fig. 4.7: Message Map)
  3. Add the event handler function by
    1. Add the member function inside MFC101Dlg.h

      afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
      


      (Fig. 4.8: Header)

    2. Back to MDC101Dlg.cpp, add the member function

      void CMFC101Dlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
      {
          // TODO: Add your message handler code here and/or call default
          if ((pScrollBar == (CScrollBar *)&m_RedSliderCtrl) ||
              (pScrollBar == (CScrollBar *)&m_GreenSliderCtrl) ||
              (pScrollBar == (CScrollBar *)&m_BlueSliderCtrl))
          {
              int nIndex;
              nIndex = m_RedSliderCtrl.GetPos();
              m_RedSliderEcho.Format(_T("%d"), nIndex);
              //m_sExposureValue.Format(_T("%d"), nIndex);
      
              nIndex = m_GreenSliderCtrl.GetPos();
              m_GreenSliderEcho.Format(_T("%d"), nIndex);
      
              nIndex = m_BlueSliderCtrl.GetPos();
              m_BlueSliderEcho.Format(_T("%d"), nIndex);
      
              UpdateData(FALSE);
          }
          else
          {
              CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
          }
      }
      
    3. Inside the OnInitDialog() function, add the following:

      m_RedSliderCtrl.SetRange(0, 255, TRUE);
      m_RedSliderCtrl.SetPos(0);
      
      m_RedSliderEcho.Format(_T("%d"), 0);
      
      m_GreenSliderCtrl.SetRange(0, 255, TRUE);
      m_GreenSliderCtrl.SetPos(0);
      
      m_GreenSliderEcho.Format(_T("%d"), 0);
      
      m_BlueSliderCtrl.SetRange(0, 255, TRUE);
      m_BlueSliderCtrl.SetPos(0);
      
      m_BlueSliderEcho.Format(_T("%d"), 0);
      
      



      (Fig. 4.9: OnInitDialog())

Build & Run

  1. Press Ctrl+shift+B to build the solution 
  2. Press Ctrl+F5 to run (without debugging).
  3. Try adjusting the positions of the slider bars, the value will show on the edit boxes
MFC Quick Start Guide (Part 4): Slider Bar Control MFC Quick Start Guide (Part 4): Slider Bar Control Reviewed by Kevin Lai on 9:48:00 PM Rating: 5

No comments:

Powered by Blogger.