| Tenouk C & C++ | MFC Home | Separating the Document from Its View 1 | Separating the Document from Its View 3 | Download | Site Index |


 

 

 

 

 

 

Module 10a:

Separating the Document from Its View 2

 

 

 

 

 

 

This is a continuation from the previous module... Program examples compiled using Visual C++ 6.0 (MFC 6.0) compiler on Windows XP Pro machine with Service Pack 2. Topics and sub topics for this Tutorial are listed below:

 

Follow these steps to build the MYMFC15 example:

 

Run AppWizard to generate \mfcproject\mymfc15. In the Step 6 page, change the view's base class to CFormView, as shown here.

 

Step 6 of 6 AppWizard, changing the view base class to CFormView class.

 

Figure 2: Step 6 of 6 AppWizard, changing the view base class to CFormView class.

 

The options and the default class names are shown here.

 

MYMFC15 project summary.

 

Figure 3: MYMFC15 project summary.

 

Use the menu editor to replace the Edit menu options. Delete the current Edit menu items and replace them with a Clear All option, as shown here.

 

Adding and modifying the Edit menu properties.

 

Figure 4: Adding and modifying the Edit menu properties.

 

Use the default constant ID_EDIT_CLEAR_ALL, which is assigned by the application framework. A menu prompt automatically appears.

 

Use the dialog editor to modify the IDD_MYMFC15_FORM dialog. Open the AppWizard-generated dialog IDD_MYMFC15_FORM, and add controls as shown below. Be sure that the Styles properties are set exactly as shown in the Dialog Properties dialog (Style = Child; Border = None) and that Visible is unchecked.

 

Modifying the IDD_MYMFC15_FORM dialog and its properties.

 

Figure 5: Modifying the IDD_MYMFC15_FORM dialog and its properties.

 

Use the following IDs for the controls.

 

Control

ID

Name edit control

IDC_NAME

Grade edit control

IDC_GRADE

Enter pushbutton

IDC_ENTER

 

Table 2.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Modifying the push button properties.

 

Figure 6: Modifying the push button properties.

 

Use ClassWizard to add message handlers for CMymfc15View. Select the CMymfc15View class, and then add handlers for the following messages. Accept the default function names.

 

Object ID

Message

Member Function

IDC_ENTER

BN_CLICKED

OnEnter()

ID_EDIT_CLEAR_ALL

COMMAND

OnEditClearAll()

ID_EDIT_CLEAR_ALL

UPDATE_COMMAND_UI

OnUpdateEditClearAll()

 

Table 3.

 

Using ClassWizard to add message handlers for CMymfc15View.

 

Figure 7: Using ClassWizard to add message handlers for CMymfc15View.

 

Use ClassWizard to add variables for CMymfc15View. Click on the Member Variables tab in the MFC ClassWizard dialog, and then add the following variables.

 

Control ID

Member Variable

Category

Variable Type

IDC_GRADE

m_nGrade

Value

int

IDC_NAME

m_strName

Value

CString

 

Table 4.

 

Using ClassWizard to add variables for CMymfc15View..

 

Figure 8: Using ClassWizard to add variables for CMymfc15View..

 

For m_nGrade, enter a minimum value of 0 and a maximum value of 100.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

----------------------------------------------------------------------

Setting the minimum and maximum value for m_nGrade.

 

Figure 9: Setting the minimum and maximum value for m_nGrade.

 

Notice that ClassWizard generates the code necessary to validate data entered by the user.

 

 

Listing 2.

 

Add a prototype for the helper function UpdateControlsFromDoc(). In the ClassView window, right-click on CMymfc15View and choose Add Member Function. Fill out the dialog box to add the following function:

 

private:

    void UpdateControlsFromDoc();

 

Using ClassView to add a prototype for the helper function UpdateControlsFromDoc().

 

Figure 10: Using ClassView to add a prototype for the helper function UpdateControlsFromDoc().

 

Edit the file Mymfc15View.cpp. AppWizard generated the skeleton OnInitialUpdate() function and ClassView generated the skeleton UpdateControlsFromDoc() function. UpdateControlsFromDoc() is a private helper member function that transfers data from the document to the CMymfc15View data members and then to the dialog edit controls. Edit the code as shown here:

 

void CMymfc15View::OnInitialUpdate()

{   // called on startup

    UpdateControlsFromDoc();

}

 

 

Listing 3.

 

void CMymfc15View::UpdateControlsFromDoc()

{   // called from OnInitialUpdate and OnEditClearAll

    CMymfc15Doc* pDoc = GetDocument();

    m_nGrade = pDoc->m_student.m_nGrade;

    m_strName = pDoc->m_student.m_strName;

 

    UpdateData(FALSE); // calls DDX

}

 

 

Listing 4.

 

The OnEnter() function replaces the OnOK() function you'd expect to see in a dialog class. The function transfers data from the edit controls to the view's data members and then to the document. Add the code shown here:

 

void CMymfc15View::OnEnter()

{

    CMymfc15Doc* pDoc = GetDocument();

    UpdateData(TRUE);

    pDoc->m_student.m_nGrade = m_nGrade;

    pDoc->m_student.m_strName = m_strName;

}

 

 

Listing 5.

 

In a complex multi-view application, the Edit Clear All command would be routed directly to the document. In this simple example, it's routed to the view. The update command UI handler disables the menu item if the document's student object is already blank. Add the following code:

 

void CMymfc15View::OnEditClearAll()

{

    // "blank" student object

    GetDocument()->m_student = CStudent();

    UpdateControlsFromDoc();

}

 

void CMymfc15View::OnUpdateEditClearAll(CCmdUI* pCmdUI)

{

// blank?

pCmdUI->Enable(GetDocument()->m_student != CStudent());

}

 

 

Listing 6.

 

Edit the MYMFC15 project to add the files for CStudent. Choose Add To Project from the Project menu, choose New from the submenu, and select the C/C++ Header File and type the Student as the file name.

 

Adding new header file to the project for the Student class.

 

Figure 11: Adding new header file to the project for the Student class.

 

Then copy the previous Student.h code into the newly created Student.h file. Repeat the similar steps for Student.cpp file. Select the C++ Source File in this case.

 

Adding new source file to the project for the Student class.

 

Figure 12: Adding new source file to the project for the Student class.

 

Visual C++ will add the files' names to the project's DSP file so that they will be compiled when you build the project.

Add a CStudent data member to the CMymfc15Doc class. Use ClassView to add the following data member, and the #include will be added automatically.

 

public:

    CStudent m_student;

 

Adding a CStudent data member to the CMymfc15Doc class.

 

Figure 13: Adding a CStudent data member to the CMymfc15Doc class.

 

 

The CStudent constructor is called when the document object is constructed, and the CStudent destructor is called when the document object is destroyed. Edit the Mymfc15Doc.cpp file. Use the CMymfc15Doc constructor to initialize the student object, as shown here:

 

CMymfc15Doc::CMymfc15Doc() : m_student("The default value", 0)

{

    TRACE("Document object constructed\n");

}

 

 

Listing 7.

 

We can't tell whether the MYMFC15 program works properly unless we dump the document when the program exits. We'll use the destructor to call the document's Dump() function, which calls the CStudent::Dump function shown here:

 

CMymfc15Doc::~CMymfc15Doc()

{

#ifdef _DEBUG

    Dump(afxDump);

#endif // _DEBUG

}

 

 

Listing 8.

 

void CMymfc15Doc::Dump(CDumpContext& dc) const

{

    CDocument::Dump(dc);

    dc << "\n" << m_student << "\n";

}

 

 

Listing 9.

 

Build and test the MYMFC15 application. Type a name and a grade, and then click Enter. Now exit the application.

 

Building MYMFC15 in debug mode.

 

Figure 14: Building MYMFC15 in debug mode.

 

MYMFC15 program output, testing the functionalities.

 

Figure 15: MYMFC15 program output, testing the functionalities.

 

Does the Debug window show messages similar to those shown here?

 

...

a CMymfc15Doc at $421920

m_strTitle = Untitled

m_strPathName =

m_bModified = 0

m_pDocTemplate = $421B20

 

a CStudent at $421974

m_strName = Mr. John Lennon

m_nGrade = 99

...

 

 

Listing 10.

 

To see these messages, you must compile the application with the Win32 Debug target selected and you must run the program from the debugger.

 

 

Continue on next module...part 3.

 

 

 

 

Further reading and digging:

  1. MSDN MFC 7.0 class library online documentation.

  2. MSDN MFC 9.0 class library online documentation - latest version.

  3. Porting & Migrating your older programs.

  4. MSDN Library

  5. DCOM at MSDN.

  6. COM+ at MSDN.

  7. COM at MSDN.

  8. Windows data type.

  9. Win32 programming Tutorial.

  10. The best of C/C++, MFC, Windows and other related books.

  11. Unicode and Multibyte character set: Story and program examples.

 

 

 

 


 

| Tenouk C & C++ | MFC Home | Separating the Document from Its View 1 | Separating the Document from Its View 3 | Download | Site Index |