Adding Code to CScribbleDoc

In procedures that follow, you’ll add new member variables (m_nPenWidth and m_penCur) and functions (InitDocument and NewStroke) to CScribbleDoc.

Suggested Reading

Note   The source files for tutorials such as Scribble contain comments that clarify the purpose of the code. Including such comments is not always mentioned as part of a tutorial step, both for brevity’s sake, and because the purpose of code you add is explained in the procedures you’ll follow. You can modify or add to comments in your own files.

To add member variables to Scribble’s document class

  1. In ClassView, expand the project folder, if necessary, and right-click CScribbleDoc. In the menu, click Add Member Variable.

    The Add Member Variable dialog box appears.

  2. In the Variable Type drop-box, type UINT.

  3. In the Variable Declaration box, type m_nPenWidth.

  4. In the Access area, click Protected, and click OK.

    ClassWizard adds the variable declaration to file ScribbleDoc.h.

  5. Repeat steps 1 through 4 to declare the protected m_penCur variable of type CPen.

    ClassView displays the new member declarations and variables you’ve added with your code.

  6. Expand the icon for CScribbleDoc.

    Note that m_nPenWidth and m_penCur are now displayed as variables belonging to the class. Once member functions and variables are declared, ClassView displays them, even if their definition isn’t written yet.

  7. Double-click m_nPenWidth.

    ScribbleDoc.h places the cursor at the definition for m_nPenWidth.

  8. Add the following lines of code in the Public attributes section of ScribbleDoc.h:
    CTypedPtrList<CObList, CStroke*> m_strokeList;
    
    CPen*   GetCurrentPen( ) { return &m_penCur; }
    

    The first line is an instance of a template. The second is the definition of an inline function, so its definition appears here instead of in ScribbleDoc.cpp.

To add member functions to Scribble’s document class

  1. In ClassView, right-click CScribbleDoc.

  2. In the menu, click Add Member Function.

  3. Fill in the Add Member Function dialog box as follows:

    A declaration is added to ScribbleDoc.h, while a starter definition is added to ScribbleDoc.cpp. You’ll fill in the definition later in this lesson.

  4. Repeat steps 1 through 3 to declare the function InitDocument. In the Function Type box, type void, and in the Access area, click Protected.

  5. Save the file. (Optional.)

    Note   The development environment saves all your work for you automatically when you build your project, close the workspace, or exit the program. Any prompts within these procedures to save your files are provided only as extra safeguards of your work.

You can view the new functions using ClassView.