National Instruments does not actively maintain this document.
This content provides support for older products and technology, so you may notice outdated links or obsolete information about operating systems or other relevant products.
The MFC document/view architecture includes a combination of a document, in which data is stored, and a view, which has privileged access to the data. The document/view architecture separates the storage and maintenance of data from the display of data. This separation facilitates development of applications that display multiple views of a single set of data simultaneously. When the application user changes the data in one view, the architecture handles the task of notifying the other views that they must update to display the modified data.The following table compares the architectures.
You might choose to create a dialog-based application when the following scenarios are applicable:
When you use the document/view architecture to implement a test and measurement application, you must design the user interface differently than when you use the dialog-based architecture. For example, you must consider how changes to the document affect the view and how changes to the view affect the document. Additionally, you might need to provide different sets of menu items and toolbar buttons for each view.When you create a Visual C++ application, you can create the application with either a Single Document Interface (SDI) or a Multiple Document Interface (MDI). When you use an application in which you can work with only one document at a time, you are using an application that uses the SDI architecture. For example, Notepad is an SDI application. If you are working in one Notepad document, you must close it before you can work in another Notepad document. When you use an application in which you can work with more than one document at a time, you are using an application that uses the MDI architecture. For example, National Instruments TestStand is an MDI application. You can open several sequence files in one session of TestStand and move between the files, changing and saving information in each one separately, without closing one file to work in another file.
This application note leads you through the process of creating an SDI application called Waveform that uses the document/view architecture to plot sine waves, sawtooth waves, square waves, and white noise on a graph. Waveform offers a menu that includes choices associated with each type of waveform data you want to plot on the graph and additional toolbar buttons associated with the menu items. Waveform also includes a graph control on which to display the waveform data.Waveform uses a Frame class, a Document class, and a View class. Visual C++ provides these classes automatically when you select the option to create an application with document/view architecture in the MFC Application Wizard. These classes make up the functionality of the document/view architecture. The Frame class encompasses the toolbar, menus, and status bar. The Document class contains the data that the application uses. The View class displays the data that the application uses. In Waveform, the Frame class determines what type of waveform data to plot; the Document class determines if the buffer contains data and whether to clear the buffer for new documents; and the View class updates the graph with the appropriate plots.This application note guides you through the following processes to design the Waveform application:
Refer to Figure 1 for an example of how the finished application appears. Notice the custom menu and toolbar buttons.Figure 1. User Interface of Completed Project
Complete the following steps to create the Waveform application:
One difference between a dialog-based application and an application built with the document/view architecture is that the document/view application window contains a frame window that surrounds the views. In a dialog-based application, the user interface contains only the dialog. In the document/view architecture, the frame window is derived from the CMainFrame class. In Figure 2, the frame is the area that contains the menus and toolbar at the top and the status bar at the bottom.
When you create Waveform with the document/view architecture, you must decide if the event handlers for the items in the frame are going to interact with the view and, if so, how. For example, in Figure 2, the toolbar buttons specify the type of waveform to generate and display on the graph, which is in the view. So, the event handlers for these toolbar buttons are in the CFormView-derived class rather than in the CMainFrame-derived class.Note: Visual C++ creates some of the items in the frame automatically. For example, Visual C++ automatically creates the File, View, and Help menus and generates the event handlers for these items. After the event handlers are generated, you can fill in the code in the .cpp source file for the class in which you declared these functions. In this example, the event handlers are in the WaveformView.cpp file.
The following sections include steps to design the user interface, custom menu, and custom toolbars and to add event handlers and event handler code to Waveform.Add the Graph Control
Add the Custom Menu
Add Buttons to the ToolbarIn this section, you create toolbar buttons and associate each with its corresponding item in the Waveform menu.
Add the Member Variable and Event HandlersThis section includes steps for adding a variable for the graph control that you placed on the dialog resource and adding event handlers for the items that you added to the Waveform menu.
Add the Event Handler CodeThis section includes steps to add the event handler code for each Waveform menu item.1. Open the Document class .h file and declare a CNiReal64Vector variable named m_wave in the //Attributes section. You use m_wave in the functions that display waveforms on the graph.2. Open the View class .cpp file and scroll to the bottom of the file to view the skeleton event handlers for the menu items.3. In the sine wave event handler, declare a double variable named phase and initialize it to zero.Call the appropriate waveform generation function in the CNiMath class. Pass the appropriate parameters to the waveform function. Use the GetDocument function to get the m_wave variable from the Document class. You also typically pass phase, amplitude, and frequency in addition to m_wave.4. Use the GetDocument function and the SetModifiedFlag function to specify when the document has changed. Using the SetModifiedFlag ensures that the framework prompts the user to save data when appropriate.5. Add a statement to call the PlotY function on the graph, using the GetDocument function to get the m_wave value from the Document class.Refer to the following code sample for the code that you add for the SineWave function:
6. Repeat steps 3 through 5 for the square wave and sawtooth wave event handlers. For white noise, pass the seed parameter rather than the frequency parameter and do not declare the phase.Note: Each function takes different parameters. Review the online help information for each function to determine the parameters and appropriate values to pass.7. Build and run Waveform.Note: Because you have not yet added code to handle clearing and/or updating the view, you are unable to use the New item in the File menu and/or on the toolbar. Refer to the Add the Code to Update the View section for information about adding the File»New functionality.Add the Code to Update the ViewWhen you display a waveform on the graph, the information that is plotted is held in a CNiReal64Vector object. To ensure that accurate information is displayed on the graph, you must add code to Waveform to clear the graph and object of old waveform data before displaying new data. Complete the following steps to properly clear the graph and object.
Add Advanced SettingsYou can set the line color of each waveform to match the color of the icons you designed for each custom button on the toolbar.1. Open the Document class .h file and declare a public CNiColor variable called m_plotColor in the //Attributes section. You use this variable to specify the color of the waveforms you display on the graph.2. Open the View .cpp file. In the event handler code for a menu item, add a statement to set the value of the m_plotColor variable. Add a statement using the CNiPlot::LineColor property to set the color of the plot to the value of the m_plotColor variable.The following code excerpt shows how to get the waveform data from the document and plot it on the graph in a particular color. In this example, the code sets the sine wave plot color to red:
3. Repeat step 2 for each menu item in the Waveform menu.4. Modify the View class OnUpdate function to set the color of the plot to the value of the Document class m_plotColor variable before the call to PlotY.Add Code for Saving and Loading Data in the Graph Ideally, you want users to save waveform data and load it in the graph at a later time. Use serialization functions or streaming operators to handle the input and output functionality required to save and load data in the graph. Because you used the document/view architecture, the Save and Open functionality is built into the Waveform application. To enable this functionality, you must modify the Document Serialize function as shown in the following example:
The code sample above uses the CArchive object, which specifies if the serialization operation is an input operation or an output operation. If the operation is an output operation, this function writes the values in m_wave and the value of m_plotColor into the archive. If the operation is an input operation, this function extracts the values of m_wave and m_plotColor from the archive and writes the values into m_wave and m_plotColor.
You have created an SDI application that uses the document/view architecture to plot several different types of waveforms, in different colors, on a graph. Waveform uses the Frame, Document, and View classes to efficiently maintain and update the waveform data and display the data in the view.Build and run Waveform, then complete the following steps to test the application:1. Use each of the menu items and toolbar buttons to ensure that they generate and plot the correct waveform in the correct color.2. Save and load waveform files to ensure that the Save and Open items work correctly.3. Close the application without saving to ensure that Waveform prompts you to save the data.To demonstrate the benefits of using the document/view architecture for the Waveform application, the following table shows a side-by-side comparison of creating Waveform using dialog-based architecture and document/view architecture.
Collaborate with other users in our discussion forums
A valid service agreement may be required, and support options vary by country.