Creating a Measurement Studio for Visual C++ 6.0 Project

Publish Date: Feb 19, 2007 | 34 Ratings | 3.91 out of 5 |  PDF

Overview

This tutorial includes step-by-step procedures for using Measurement Studio classes and controls to complete the following tasks:
  • graph two sine waves, the amplitudes of which you can set with slides
  • add an update button that displays the amplitude of the second slide in a numeric indicator and updates the graph with the plot of the second slide
  • programmatically change the color, line style, and line width of the graphs according to amplitude


Table of Contents

  1. Creating the Dialog
  2. Modifying the Controls
  3. Adding Variables for the Controls
  4. Adding Member Functions for the Controls
  5. Adding Code to the Project
  6. Adding Advanced Features to the Project

1. Creating the Dialog


Complete the following steps to create the dialog and add controls to it:
  1. Open Visual C++, click File»New. Click the Projects tab and click NI Measurement Studio AppWizard.
  2. Type Project in Project name.
  3. Choose an appropriate Location, such as \Temp.
  4. Click OK.
  5. Select Measurement MFC Application and click Next.
  6. 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.
  7. 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.
  8. 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.
  9. 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

Back to Top

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:
  1. Right click a control and choose Properties to display the control property pages.
  2. For each control, click the General tab. Change the Caption for the slide to Dash Dot and delete the caption for the graph.
  3. For the graph, click the Plots tab and change the Line style to Dash Dot.
  4. 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.
  5. 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.

Back to Top

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:
  1. Click View»ClassWizard and click the Member Variables tab.
  2. Select IDC_CWGRAPH1 in Control IDs and click Add Variable.
  3. Notice that the member variable name field already contains m_. Type graph to complete the Member variable name and click OK.
  4. Repeat steps 2 and 3, highlighting the slide name in Control IDs and assigning the variable name m_slide1.
  5. Click OK to exit the ClassWizard.

Back to Top

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.
  1. Click View»ClassWizard.
  2. Click the Message Maps tab and select IDC_CWSLIDE1 in Object IDs, then select PointerValueChanged from Messages.
  3. Click Add Function, and click OK to accept the default name in Member function name.
  4. Click OK to exit the ClassWizard.
  5. In the workspace window of the environment, select FileView.
  6. Expand the project files and source files.
  7. Double click ProjectDlg.cpp.
  8. Scroll to the bottom of the ProjectDlg.cpp file to see the member function that the ClassWizard generated.

    Back to Top

    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
    BOOL CProjectDlg::OnInitDialog()
    {
    CDialog::OnInitDialog();
    // 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.
    m_graph.Plots.Item(1).PlotY(m_wave);
    // Initialize the slide value for the appropriate
    // amplitude.
    m_slide1.Value = 1;
    return TRUE;
    // return TRUE unless you set the focus to a control
    }
5. In the dialog resource editor, double click the slide to display the OnPointerValueChangedCwslide1 function.

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.
    CNiReal64Vector temp(m_wave);
    // Scale the wave according to the slide value
    temp.Scale(CNiVariant(Value));
    // Update the first plot.
    m_graph.Plots.Item(1).PlotY(temp);
    }
7. 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 changes the plot on the graph.
 

Back to Top

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

      // wave.

      CNiReal64Vector temp(m_wave);

      temp.Scale(2);

      m_graph.Plots.Item(2).PlotY(temp);


    7. Add the following bold-faced code to the OnClickCwboolean1 function.

      void CProjectDlg::OnClickCwboolean1()

      {


        // Update the second plot and amplitude numeric

        // indicator.

        m_numedit.Value = m_slide2.Value;

        CNiReal64Vector temp(m_wave);

        temp.Scale(m_slide2.Value);

        m_graph.Plots.Item(2).PlotY(temp);


      }

    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.

        CNiReal64Vector temp(m_wave);

        // Scale the wave by the slide value.

        temp.Scale(dValue);

        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;


        }

        else

        {


          plot.LineColor = CNiColor(255, 0, 0); // red

          plot.LineStyle = CNiPlot::LineDot;


        }

        // Plot the new sine wave.

        plot.PlotY(temp);


      }

    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.


Back to Top

Bookmark & Share


Ratings

Rate this document

Answered Your Question?
Yes No

Submit