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
For Vertical Slider Bar
- Open the Toolbox
- Drag a Static Text to the dialog window, rename
- to "Vertical Bar"
- Create a Text Label with
- to "IDC_V_SLIDER_ECHO"
- to "0"
- to
- Add an output Control Variable to
the Text Label, with
- Access to
- Category to
- Variable Name to "m_VSliderEcho"
- Variable Type to
- Drag a Slider Control to the dialog window,
inside the Properties window, change
- to "IDC_V_SLIDER_BAR"
- to "Vertical"
- Add an output Control Variable
to the vertical slider, with
- Category to
- Variable Name to "m_VSliderBar"
- Variable Type to
- Select the main application window and bring up theProperties window,
- Select the button
- Scroll down to "WM_VSCROLL"
- Left-Mouse-Button click in the drop-down menu and click "<Add> OnVScroll"
Figure 3.1 - Message Properties
- 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(L"%d", 0);
}
- 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(L"%d", value);
UpdateData(FALSE);
}else{
CDialog::OnVScroll(nSBCode,
nPos, pScrollBar);
}
Add Text Fields
For Horizontal Slider Bar
- Drag a Static Text to the dialog window, rename
- to "Horizontal Bar"
- Create a
Text Label with
- to "IDC_H_SLIDER_ECHO"
- to "0"
- to
- Add an output
Control Variable to
the Text Label, with
- Access to
- Category to
- Variable Name to "m_HSliderEcho"
- Variable Type to
Add Horizontal Slider Bar
- Repeat the steps for Adding Vertical Slider Bar with the following changes
- Inside the Slider Control Properties window, change
- to "IDC_H_SLIDER_BAR"
- to "Horizontal"
- Add an output Control Variable
to the horizontal slider, with
- Category to
- Variable Name to "m_HSliderBar"
- Variable Type to
- Create a Service Routine as "WM_HSCROLL"
and click "<Add> OnHScroll"
- 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(L"%d", 0);
}
- 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(L"%d", value);
UpdateData(FALSE);
}else{
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
- In the Visual Studio project window, click
- >>
- >> to run
Figure 3.2 - Application Window
» 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.