1. Creating the Dialog
Complete the following steps to create the dialog and add controls to it:
- Open Visual C++, click File»New. Click the Projects tab and click NI Measurement Studio AppWizard.
- Type Project in Project name.
- Choose an appropriate Location, such as \Temp.
- Click OK.
- Select Measurement MFC Application and click Next.
- Deselect all components except for the Analysis, Common, and User Interface components, then click Next.
Note The Analysis component name is either Advanced Analysis or Base Analysis, depending on the Measurement Studio installation you have.
- Click Next to navigate through the wizard, accepting all defaults, except on the Step 4 dialog box. On the Step 4 dialog box, disable the About Box option. Be sure to continue clicking Next until the Next button is no longer available, then click Finish and OK to complete the setup.
- In the dialog resource, remove the TODO: Place dialog controls here label by selecting it and pressing <Delete>. Remove the OK and Cancel buttons as well.
- Drag and drop a Measurement Studio Slide and a Measurement Studio Graph control onto the dialog resource, placing the controls as shown in Figure 1. To resize a control in the dialog resource, select the control and click and drag a corner of it.
Note If the Controls toolbar is not visible, right click anywhere in the frame of the Visual C++ environment and select Controls.
Figure 1. Initial Project Dialog
2. Modifying the Controls
You can set properties of the controls such as the labels, value ranges of numeric controls, types of cursors, and axis settings. Complete the following steps to modify the controls:
- Right click a control and choose Properties to display the control property pages.
- For each control, click the General tab. Change the Caption for the slide to Dash Dot and delete the caption for the graph.
- For the graph, click the Plots tab and change the Line style to Dash Dot.
- To set the scale of the Y axis on the graph, click the Axes tab and select YAxis-1 in Axes. Deselect Auto scale and type -10 in Minimum.
- For the slide, click the Numeric tab and in the Scale section, set the Minimum to 0 and the Maximum to 5. While you have the slide properties displayed, choose the Style tab and select the 3D Vertical Pointer Slide style.
3. Adding Variables for the Controls
You add variables for the controls so that you can set their properties and call their functions from within your program. Complete the following steps to add variable names for the controls:
- Click View»ClassWizard and click the Member Variables tab.
- Select IDC_CWGRAPH1 in Control IDs and click Add Variable.
- Notice that the member variable name field already contains m_. Type graph to complete the Member variable name and click OK.
- Repeat steps 2 and 3, highlighting the slide name in Control IDs and assigning the variable name m_slide1.
- Click OK to exit the ClassWizard.
4. Adding Member Functions for the Controls
You add member functions to the controls to respond to events that the controls generate. You can use the MFC ClassWizard or double click a control to generate a member function for it.
- Click View»ClassWizard.
- Click the Message Maps tab and select IDC_CWSLIDE1 in Object IDs, then select PointerValueChanged from Messages.
- Click Add Function, and click OK to accept the default name in Member function name.
- Click OK to exit the ClassWizard.
- In the workspace window of the environment, select FileView.
- Expand the project files and source files.
- Double click ProjectDlg.cpp.
- Scroll to the bottom of the ProjectDlg.cpp file to see the member function that the ClassWizard generated.
5. Adding Code to the Project
Complete the following steps to add code to your project:
1. In the ClassView tab in the workspace, find CProjectDlg, right click it, and select Add Member Variable. Type CNiReal64Vector in Variable Type and m_wave in Variable Name. Select Private in Access. You will use this variable to hold sine wave data. CNiReal64Vector is a Measurement Studio data type that holds an array of double values.
2. Click OK.
3. To add code to the member function skeleton code, expand the CProjectDlg item and double click OnInitDialog.
4. In the OnInitDialog() function, add the code shown in bold-faced font below.
- /////////////////////////////////////////////////////// ////
// CProjectDlg message handlers
// Set the icon for this dialog. The framework does
// this automatically when the application's main
// window is not a dialog.
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// Generate the internal wave vector.
CNiMath::SineWave (m_wave, 100, 1);
// Plot the first sine wave.
// Initialize the slide value for the appropriate
m_slide1.Value = 1;
// return TRUE unless you set the focus to a control
6. Add the code shown in bold-faced font below . This code changes the sine wave amplitude when the corresponding slide value changes.
- void CProjectDlg::OnPointerValueChangedCwslide1(long Pointer, VARIANT FAR* Value)
// Create a local copy of the sine wave.
// Scale the wave according to the slide value
// Update the first plot.
6. Adding Advanced Features to the Project
The following steps demonstrate how to programmatically change the color and style of plot lines. The CNiPlot property, LineStyle, is an enumerated data type. You can find the set of enumerated constants in the following places:
- Click Tools»Measurement Studio Reference and use the index to navigate to the CNiPlot topic.
- Click the FileView tab in the workspace and select Measurement Studio»Include»NiPlot.h.
1. Add a Measurement Studio Slide, a Measurement Studio Button, a Measurement Studio Numedit control, and a Visual C++ Group Box control to the dialog resource. Rearrange the controls as shown in Figure 2.
Figure 2. Additional Controls
- Note The Update button shown above is based on the Measurement Studio 3D button style, but you also can use the Visual C++ button style. If you use a Visual C++ button style, you must create an OnClick event handler for the button Click event. Refer to the code samples in the following steps for more information.
2. Add captions, as applicable, for the additional controls.
3. Using the Plots tab in the graph property page, click Add in the Plots section to add an additional plot to the graph. Change its Line style color to red.
4. Add member variables for the numedit and slide controls. Refer to the Adding Variables for the Controls section for more information. Use m_numedit for the numedit control and m_slide2 for the second slide control.
5. Add a member function to handle the button Click message. Refer to the steps in the Adding Member Functions for the Controls section for more information.
6. Add the following code to the bottom of the OnInitDialog function before the final return statement.
// Create a temporary vector initializing it with the
// m_wave member variable. Scale the temporary sine wave
// to have an amplitude of 2. Plot the second sine
7. Add the following bold-faced code to the OnClickCwboolean1 function.
// Update the second plot and amplitude numeric
m_numedit.Value = m_slide2.Value;
8. To change the color of the plots, replace the code in the OnPointerValueChangedCwslide1 function with the bold-faced code below .
void CProjectDlg::OnPointerValueChangedCwslide1(long Pointer, VARIANT FAR* Value)
double dValue = CNiVariant(Value);
// Get a reference to the plot to reduce amount
// of typing.
CNiPlot plot = m_graph.Plots.Item(1);
// Create a local copy of the sine wave.
// Scale the wave by the slide value.
if (dValue < 2)
plot.LineColor = CNiColor(0, 0, 255); // blue
plot.LineStyle = CNiPlot::LineSolid;
else if (dValue < 4)
plot.LineColor = CNiColor(0, 255, 0); // green
plot.LineStyle = CNiPlot::LineDash;
plot.LineColor = CNiColor(255, 0, 0); // red
plot.LineStyle = CNiPlot::LineDot;
// Plot the new sine wave.
9. Click Build»Build Project.exe, then click Build»Execute Project.exe to make sure the project compiles and runs as expected.
Verify that changing the Dash Dot slide updates the correct plot on the graph. Also, verify that changing the Solid Line slide and clicking the Update button displays the expected value in the Solid Line Plot Value control and updates the correct plot on the graph.
Below is a sample of the dialog as it appears after you have successfully built and run it.