MFC Step-by-Step Guide

Tutorial 3

Slider Control Variables


GOAL: To understand that control variables can be a pre-defined class and demonstrate working with control variables
PREREQUISITES: Tutorial 2

Add Text Fields
  1. Open the Toolbox
  2. Drag a Static Text to the dialog window, rename
    1. Caption to "Vertical Bar"
  3. Create a Text Label with
    1. ID to "IDC_V_SLIDER_ECHO"
    2. Caption to "0"
    3. Client Edge to True
  4. Add an output Control Variable to the Text Label, with
    1. Access to private
    2. Category to Value
    3. Variable Name to "m_VSliderEcho"
    4. Variable Type to CString
Add Vertical Slider Bar
  1. Drag a Slider Control to the dialog window, inside the Properties window, change
    1. ID to "IDC_V_SLIDER_BAR"
    2. Orientation to "Vertical"
  2. Add an output Control Variable to the vertical slider, with
    1. Category to Control
    2. Variable Name to "m_VSliderBar"
    3. Variable Type to CSliderCtrl
Create Service Routine
  1. Select the main application window and bring up theProperties window,
    1. Select the Messages button
    2. Scroll down to "WM_VSCROLL"
    3. Left-Mouse-Button click in the drop-down menu and click "<Add> OnVScroll"
    Figure 3.1 - Message Properties
    Figure 3.1 - Message Properties
  2. In 'TutorialDlg.cpp', initialize the slider inside the OnInitDialog() function:
    BOOL CTutorialDlg::OnInitDialog(){
       ...
       m_VSliderBar.SetRange(0, 100, TRUE);
       m_VSliderBar.SetPos(0);
       m_VSliderEcho.Format("%d", 0);
    }
  3. Now inside the function code block for OnVScroll(...), add the following code:
    if (pScrollBar == (CScrollBar *) &m_VSliderBar)
    {
       int value = m_VSliderBar.GetPos();
       m_VSliderEcho.Format("%d", value);
       UpdateData(FALSE);
    }else{
      CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
    }
Add Horizontal Slider Bar
  1. Repeat the steps for Adding Vertical Slider Bar with the following changes
  2. Inside the Slider Control Properties window, change
    1. ID to "IDC_H_SLIDER_BAR"
    2. Orientation to "Horizontal"
  3. Add an output Control Variable to the horizontal slider, with
    1. Category to Control
    2. Variable Name to "m_HSliderBar"
    3. Variable Type to CSliderCtrl
  4. Create a Service Routine as "WM_HSCROLL" and click "<Add> OnHScroll"
  5. In 'TutorialDlg.cpp', initialize the slider inside the OnInitDialog() function:
    BOOL CTutorialDlg::OnInitDialog(){
       ...
       m_HSliderBar.SetRange(0, 100, TRUE);
       m_HSliderBar.SetPos(0);
       m_HSliderEcho.Format("%d", 0);
    }
  6. Now inside the function code block for OnHScroll(...), add the following code:
    if (pScrollBar == (CScrollBar *) &m_HSliderBar)
    {
       int value = m_HSliderBar.GetPos();
       m_HSliderEcho.Format("%d", value);
       UpdateData(FALSE);
    }else{
      CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
    }


» Written by William Frankhouser (wjf2@washington.edu)
» Advised by Kelvin Sung (ksung@washington.edu) as part of the project sponsored by the National Science Foundation under Grant No. 0442420. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.
» Produced in the "Essential Concepts for Building Interactive Computer Graphics Applications", A.K. Peters, Ltd.