Lab 3: Displaying Application Data

In this lab, you will learn how to use the classes generated by the MFC AppWizard to implement the basic document/view architecture. Using the STUpload project that you created for Lab 2, you will learn how to:

  • Use the document class to store the application data.
  • Use the OnDraw() function of your view class to display the application data on screen.

You will recall from the discussion of the STUpload application in Lab 1 that your client, StockWatch Data Services, requires that the data be loaded into the application from an ASCII data file. Each of the data rows in the text file will contain three values: a fund name, a date, and a closing price. Your first task should be to establish an appropriate structure for the data that you are trying to model. You can then implement this structure as part of the application's document class.

A data file can be thought of as a collection of data rows. Therefore, an appropriate way to proceed would be to:

  1. Create a class that represents a data row.
  2. Create a collection of these data row classes.

To save you time and effort, we have implemented the data row class and a class that encapsulates a collection of these data row classes. The data row class, CStockData, is implemented with the following data members:

double m_dblPrice; COleDateTime m_date; CString m_strFund;

The COleDateTime class is an MFC class that encapsulates date and time values. The CString class is MFC's variable length string class. Information on both of these classes is available in the Visual C++ Help file.

We have supplied the definition and implementation of the CStockData class in the StockData.h and StockData.cpp files in the \Chapter 3\Code directory on the companion CD. The files also define constructors, accessor functions, and comparison operators for the class, as well as the GetAsString() function to display the object's data as a string.

We have supplied the CStockDataList class, declared in \Chapter 3\ Code\StockDataList.h as follows:

class CStockDataList : public CList<CStockData, CStockData &>

The CStockDataList class is derived from the MFC template-based collection class CList. The template takes two parameters: the first specifies the type of object to be contained in the list and the second specifies the type used to reference objects stored in the list.

MFC provides a number of template-based classes to implement data collections, such as CArray and CMap. We have chosen to use the CList class because it provides the facility to insert items at any location in the list. You can find out more about MFC collection classes by searching for "MFC collection classes" in the Visual C++ Help file.

You are going to import the source files into your project so that you can use the CStockData and the CStockDataList classes.

  • To import the source files into your project
    1. Using Windows Explorer, copy the files StockData.h, StockData.cpp, StockDataList.h, and StockDataList.cpp from the \Chapter 3\ Code directory to your STUpload working directory.
    2. Open the STUpload project that you created in Lab 2.
    3. In the Workspace window, switch to FileView.
    4. Right-click the STUpload Files icon. On the shortcut menu, click Add Files to Project.
    5. Browse to the STUpload directory (you are probably already there). Select the StockData.h, StockData.cpp, StockDataList.h, and StockDataList.cpp files and click OK. The files will be inserted into the project.
    6. In the Workspace window, switch to Class View. The STUpload classes tree now contains the StockData and StockDataList classes. Expand the class icons and look at the member data and functions that are provided.

    Now you have a class that can be used to represent the application data. The CStockDataList class represents all the records loaded from one of the ASCII data files used by StockWatch Data Services. The next task is to create an instance of your application data class inside your document class, so that your document class contains your application data. You can achieve this by creating a variable of the CStockDataList type as a member of the CSTUploadDoc class.

  • To add the m_DocList data member to the CSTUploadDoc class
    1. In ClassView, right-click the CSTUploadDoc class icon.
    2. On the shortcut menu, click Add Member Variable.
    3. In the Add Member Variable dialog box, type CStockDataList in the Variable Type box.
    4. Type m_DocList into the Variable Name box.
    5. Set the access specifier to Protected. Press OK to add the variable.
    6. Double-click the CSTUploadDoc class icon in ClassView to open the CSTUploadDoc.h file and jump to the top of the class declaration.
    7. Check that ClassView has added the line
    8. #include "StockDataList.h"

      to the top of the CSTUploadDoc.h file.

    Because you have added m_DocList as a protected member variable, you will need to add a function that will provide read-only access to the list.

  • To add an accessor function for the protected m_DocList member
  • Add the following line to the public section of the CSTUploadDoc class declaration:

    const CStockDataList & GetDocList() { return m_DocList; }

    You are not going to add functions to load records from a text file in this lab. However, you are going to add a few CStockData items to the CSTUploadDoc:: m_DocList member so that you can learn how to display the document data in the application's view. As a temporary measure, you will add a few fake CStockData records in the CSTUploadDoc constructor. We have provided an implementation of the constructor in the file \Chapter 3\Code\Ch3_01.cpp on the companion CD.

  • To add the fake CStockData records
    1. Using Windows Explorer, locate the \Chapter 3\Code directory on the companion CD.
    2. Double-click the Ch3_01.cpp file to open it in Visual C++. The new CSTUploadDoc constructor function displays as shown below.
    3. CSTUploadDoc::CSTUploadDoc() {      m_DocList.AddTail(CStockData(_T("ARSC"),            COleDateTime(1999, 4, 1, 0, 0, 0),            22.33));      m_DocList.AddTail(CStockData(_T("ARSC"),            COleDateTime(1999, 4, 2, 0, 0, 0),           23.44));      m_DocList.AddTail(CStockData(_T("ARSC"),            COleDateTime(1999, 4, 3, 0, 0, 0),           24.55));      m_DocList.AddTail(CStockData(_T("ARSC"),            COleDateTime(1999, 4, 4, 0, 0, 0),           25.66));      m_DocList.AddTail(CStockData(_T("ARSC"),            COleDateTime(1999, 4, 5, 0, 0, 0),           26.77)); }

      The code adds five CStockData objects to the CSTUploadDoc::m_DocList member. Select the entire function and press CTRL+C to copy the code to the clipboard.

    4. Close the Ch3_01.cpp file.
    5. In ClassView, double-click the CSTUploadDoc constructor icon to jump to the constructor implementation. The default blank constructor should display as follows:
    6. CSTUploadDoc::CSTUploadDoc() {      // TODO: add one-time construction code here }

    7. Select and delete the entire blank constructor. Press CTRL+V to paste in the replacement constructor.

    For the purposes of illustration, you will provide a simple implementation of the CSTUploadView::OnDraw() function. You will implement a much more sophisticated drawing function in Chapter 5, Implementing Application Behaviors.

  • To implement the CSTUploadView::OnDraw() function
    1. Using Windows Explorer, locate the \Chapter 3\Code directory on the companion CD.
    2. Double-click the Ch3_02.cpp file to edit it in Visual C++. The new OnDraw() function displays as follows:
    3. void CSTUploadView::OnDraw(CDC* pDC) {      CSTUploadDoc* pDoc = GetDocument();      ASSERT_VALID(pDoc);      // Save the current state of the device context      int nDC = pDC->SaveDC();            // Create font for axis labels      CFont aFont;      if(aFont.CreateFont(16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,            FF_MODERN, 0))              pDC->SelectObject(&aFont);      else      {           AfxMessageBox("Unable to create font");           return;      }      const CStockDataList & pData = pDoc->GetDocList();      int yPos = 10;      int nTextHeight = pDC->GetTextExtent("A").cy;      POSITION pos = pData.GetHeadPosition();      while(pos)      {           CStockData sd = pData.GetNext(pos);           pDC->TextOut(10, yPos, sd.GetAsString());           yPos += nTextHeight;      }      // Restore the original device context      pDC->RestoreDC(nDC); }

      The code uses functions of the CList class to iterate across the CSTUploadDoc:: m_DocList member, and uses the CStockData::GetAsString() function to provide a string version of the data that can be displayed using the CDC::TextOut() function. Note how the CDC functions SaveDC() and RestoreDC() are used to save and restore the state of the device context at the beginning and end of the function. This is recommended practice whenever you make calls to functions that modify the device context. This function modifies the device context by selecting a new font with the CDC::SelectObject() function.

    4. Select the entire function and press CTRL+C to copy the code to the clipboard.
    5. Close the Ch3_02.cpp file.
    6. In ClassView, double-click the CSTUploadView::OnDraw() icon to jump to the function implementation. The default function should display as follows:
    7. void CSTUploadView::OnDraw(CDC* pDC) {      CSTUploadDoc* pDoc = GetDocument();      ASSERT_VALID(pDoc);      // TODO: add draw code for native data here }

    8. Select and delete the entire OnDraw() function. Press CTRL+V to paste in the new version.

    You can now build and run your STUpload application. The five stock records should display as shown in Figure 3.9.

    click to view at full size.

    Figure 3.9 STUpload at the end of Lab 3



    Microsoft Press - Desktop Applications with Microsoft Visual C++ 6. 0. MCSD Training Kit
    Desktop Applications with Microsoft Visual C++ 6.0 MCSD Training Kit
    ISBN: 0735607958
    EAN: 2147483647
    Year: 1999
    Pages: 95

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net