MFC Quick Start Guide (Part 4): Slider Bar Control
- Open the Toolbox tab, select Slider Control and drag it to your dialog
(Fig. 4.1: Slider Control) - Do it three times
- Rename the IDs of them to be IDC_RED_SLIDER, IDC_GREEN_SLIDER and IDC_BLUE_SLIDER
- Right click on the edit box first slider bar and choose Add variable
- Input the data according to the following setting:
- Access: private
- Check the box “control variable”
- Category: value
- Variable Type: CString
- Variable name: m_RedSliderEcho
- Repeat step 5 similarly for the other edit boxes
- Make sure the Orientation field in the Property panel is horizontal
(Fig. 4.5: Horizontal) - Right click on the first slider ctrl and choose Add variable
- Input the data according to the following setting:
- Access: private
- Check the box “control variable”
- Category: control
- Variable Type: CSliderCtrl
- Variable name: m_RedSliderCtrl
- Repeat step 9 similarly for the other 2 slider bars
Adding Event Handler
- Find the line “BEGIN_MESSAGE_MAP(CMFC101Dlg, CDialogEx)” in MFC101Dlg.cpp
- Add this following line of code:
ON_WM_HSCROLL()
- Add the event handler function by
- Add the member function inside MFC101Dlg.h
afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
- 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); } }
- 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);
Build & Run
- Press Ctrl+shift+B to build the solution
- Press Ctrl+F5 to run (without debugging).
- 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
Reviewed by Kevin Lai
on
9:48:00 PM
Rating:
No comments: