| Tenouk C & C++ | MFC Home | MDI Serialization 1 | MDI Serialization 3 | Download | Site Index |


 

 

 

Module 12a:

Serialization: Reading and Writing Documents—MDI Applications 2

 

 

 

This is a continuation from 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. You can compare the standard C file I/O, standard C++ file I/O and Win32 directory, file and access controls with the MFC serialization. So many things lor! Similar but not same. Those links also given at the end of this tutorial.

  1. The MYMFC18 Example

  2. CMymfc18App Class

 

 

The MYMFC18 Project Example

 

This example is the MDI version of the MYMFC17 example from the previous module. It uses the same document and view class code and the same resources (except the program name). The application code and main frame class code are different, however. All the new code is listed here, including the code that AppWizard generates. A list of the files and classes in the MYMFC18 example are shown in the table below.

 

Header File

Source Code File

Class

Description

mymfc18.h

mymfc18.cpp

CMymfc18App

Application class (from AppWizard)

 

 

CAboutDlg

About dialog

MainFrm.h

MainFrm.cpp

CMainFrame

MDI main frame

ChildFrm.h

ChildFrm.cpp

CChildFrame

MDI child frame

Mymfc18Doc.h

Mymfc18Doc.cpp

CMymfc18Doc

Student document (from MYMFC17)

Mymfc18View.h

Mymfc18View.cpp

CMymfc18View

Student form view (from MYMFC18)

Student.h

Student.cpp

CStudent

Student record (from MYMFC17)

StdAfx.h

StdAfx.h

 

Precompiled headers (with afxtempl.h included)

 

Table 3.

 

The steps for this program example are as follows:

 

AppWizard step 1 of 6 for MYMFC18 project, selecting Multiple documents.

 

Figure 7: AppWizard step 1 of 6 for MYMFC18 project, selecting Multiple documents.

 

AppWizard step 4 of 6 for MYMFC18 project, setting the Advanced options.

 

Figure 8: AppWizard step 4 of 6 for MYMFC18 project, setting the Advanced options.

 

Entering the File extension, you can change other options as required but accept the shown name for this exercise.

 

Figure 9: Entering the File extension, you can change other options as required but accept the shown name for this exercise.

 

Notice the maximum number of the Doc type name and for the file extension, it is recommended for this example to use the 3 characters.

 

AppWizard step 6 of 6 for MYMFC18 project, selecting CFormView for the view base class.

 

Figure 10: AppWizard step 6 of 6 for MYMFC18 project, selecting CFormView for the view base class.

 

MYMFC18 project summary.

 

Figure 11: MYMFC18 project summary.

 

Control

ID

The dialog template

IDD_MYMFC18_FORM

Name edit control

IDC_NAME

Grade edit control

IDC_GRADE

Clear pushbutton

IDC_CLEAR

 

Table 4: Object IDs for MYMFC18.

 

Adding and modifying dialog and its controls properties.

 

Figure 12: Adding and modifying dialog and its controls properties.

 

ClassWizard was used to map the CMymfc18View Clear pushbutton notification message as follows.

 

Object ID

Message

Member Function

IDC_CLEAR

BN_CLICKED

OnClear()

 

Table 5.

 

 

 

 

 

 

Mapping the CMymfc18View Clear pushbutton notification message.

 

Figure 13: Mapping the CMymfc18View Clear pushbutton notification message.

 

And the member variables.

 

Control ID

Member Variable

Category

Variable Type

Other

IDC_GRADE

m_nGrade

Value

int

Min = 0, max = 100

IDC_NAME

m_strName

Value

CString

Max= 20

 

Table 6.

 

Using ClassWizard to add member variables.

 

Figure 14: Using ClassWizard to add member variables.

 

Adding and modifying Clear All menu properties.

 

Figure 15: Adding and modifying Clear All menu properties.

 

The Edit Clear All command is handled in the document class. The following message handlers were added through ClassWizard.

 

Object ID

Message

Member Function

ID_EDIT_CLEAR_ALL

COMMAND

OnEditClearAll()

ID_EDIT_CLEAR_ALL

ON_UPDATE_COMMAND_UI

OnUpdateEditClearAll()

 

Table 7.

 

Adding message handlers for ID_EDIT_CLEAR_ALL.

 

Figure 16: Adding message handlers for ID_EDIT_CLEAR_ALL.

 

Creating and modifying toolbar buttons properties.

 

Figure 17: Creating and modifying toolbar buttons properties.

 

Button

Function

Retrieves the first student record

Retrieves the last student record

Retrieves the previous student record

Retrieves the next student record

Deletes the current student record

Inserts a new student record

 

Table 8.

 

Back to the CMymfc18View class.

 

 

Object ID

Message

Member Function

ID_STUDENT_HOME

COMMAND

OnStudentHome()

ID_STUDENT_END

COMMAND

OnStudentEnd()

ID_STUDENT_PREV

COMMAND

OnStudentPrev()

ID_STUDENT_NEXT

COMMAND

OnStudentNext()

ID_STUDENT_INS

COMMAND

OnStudentIns()

ID_STUDENT_DEL

COMMAND

OnStudentDel()

 

Table 9.

 

Object ID

Message

Member Function

ID_STUDENT_HOME

UPDATE_COMMAND_UI

OnUpdateStudentHome()

ID_STUDENT_END

UPDATE_COMMAND_UI

OnUpdateStudentEnd()

ID_STUDENT_PREV

UPDATE_COMMAND_UI

OnUpdateStudentHome()

ID_STUDENT_NEXT

UPDATE_COMMAND_UI

OnUpdateStudentEnd()

ID_STUDENT_DEL

UPDATE_COMMAND_UI

OnUpdateCommandDel()

 

Table 10.

 

Adding message handlers for toolbar buttons.

 

Figure 18: Adding message handlers for toolbar buttons.

 

Now we are ready for the coding part.

 

CMymfc18App Class

 

In the CMymfc18App source code listing, the OpenDocumentFile() member function is overridden only for the purpose of inserting a TRACE statement. Also, a few lines have been added before the ProcessShellCommand() call in InitInstance(). They check the argument to ProcessShellCommand() and change it if necessary to prevent the creation of any empty document window on startup. Listing 5 shows the source code.

 

MYMFC18.H

// mymfc18.h : main header file for the MYMFC18 application

//

 

#if !defined(AFX_MYMFC18_H__7B5FE267_85E9_11D0_8FE3_00C04FC2A0C2__INCLUDED_)

#define AFX_MYMFC18_H__7B5FE267_85E9_11D0_8FE3_00C04FC2A0C2__INCLUDED_

 

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

 

#ifndef __AFXWIN_H__

    #error include 'stdafx.h' before including this file for PCH

#endif

#include "resource.h"       // main symbols

 

//////////////////////////////////////////////////////////////////////

// CMymfc18App:

// See mymfc18.cpp for the implementation of this class

//

 

class CMymfc18App : public CWinApp

{

public:

    CMymfc18App();

 

// Overrides

    // ClassWizard generated virtual function overrides

    //{{AFX_VIRTUAL(CMymfc18App)

    public:

    virtual BOOL InitInstance();

    virtual CDocument* OpenDocumentFile(LPCTSTR lpszFileName);

    //}}AFX_VIRTUAL

 

// Implementation

 

    //{{AFX_MSG(CMymfc18App)

    afx_msg void OnAppAbout();

        // NOTE - the ClassWizard will add and remove member functions here.

        //    DO NOT EDIT what you see in these blocks of generated code !

    //}}AFX_MSG

 

    DECLARE_MESSAGE_MAP()

};

 

//////////////////////////////////////////////////////////////////////

 

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations

// immediately before the previous line.

 

#endif // !defined(AFX_MYMFC18_H__7B5FE267_85E9_11D0_8FE3_00C04FC2A0C2__INCLUDED_)

 

 

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

 

 

 

 

 

 

MYMFC18.CPP

// mymfc18.cpp : Defines the class behaviors for the application.

//

 

#include "stdafx.h"

#include "mymfc18.h"

 

#include "MainFrm.h"

#include "ChildFrm.h"

#include "Mymfc18Doc.h"

#include "Mymfc18View.h"

 

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[ ] = __FILE__;

#endif

 

//////////////////////////////////////////////////////////////////////

// CMymfc18App

 

BEGIN_MESSAGE_MAP(CMymfc18App, CWinApp)

    //{{AFX_MSG_MAP(CMymfc18App)

    ON_COMMAND(ID_APP_ABOUT, OnAppAbout)

        // NOTE - the ClassWizard will add and remove mapping macros here.

        //    DO NOT EDIT what you see in these blocks of generated code!

    //}}AFX_MSG_MAP

    // Standard file based document commands

    ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)

    ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)

END_MESSAGE_MAP()

 

//////////////////////////////////////////////////////////////////////

// CMymfc18App construction

 

CMymfc18App::CMymfc18App()

{

    // TODO: add construction code here,

    // Place all significant initialization in InitInstance

}

 

//////////////////////////////////////////////////////////////////////

// The one and only CMymfc18App object

 

CMymfc18App theApp;

 

//////////////////////////////////////////////////////////////////////

// CMymfc18App initialization

 

BOOL CMymfc18App::InitInstance()

{

    AfxEnableControlContainer();

 

    // Standard initialization

    // If you are not using these features and wish to reduce the size

    //  of your final executable, you should remove from the following

    // the specific initialization routines you do not need.

 

#ifdef _AFXDLL

    Enable3dControls();    // Call this when using MFC in a shared DLL

#else

    Enable3dControlsStatic();       // Call this when linking to MFC statically

#endif

 

    // Change the registry key under which our settings are stored.

    // TODO: You should modify this string to be something appropriate

    // such as the name of your company or organization.

    SetRegistryKey(_T("Local AppWizard-Generated Applications"));

    LoadStdProfileSettings();  // Load standard INI file options (including MRU)

    // Register the application's document templates.  Document

    //  templates serve as the connection between documents, frame windows and views.

 

    CMultiDocTemplate* pDocTemplate;

    pDocTemplate = new CMultiDocTemplate(

        IDR_MYMFC1TYPE,

        RUNTIME_CLASS(CStudentDoc),

        RUNTIME_CLASS(CChildFrame), // custom MDI child frame

        RUNTIME_CLASS(CStudentView));

    AddDocTemplate(pDocTemplate);

 

    // create main MDI Frame window

    CMainFrame* pMainFrame = new CMainFrame;

    if (!pMainFrame->LoadFrame(IDR_MAINFRAME))

        return FALSE;

    m_pMainWnd = pMainFrame;

 

    // Enable drag/drop open

    m_pMainWnd->DragAcceptFiles();

 

    // Enable DDE Execute open

    EnableShellOpen();

    RegisterShellFileTypes(TRUE);

 

    // Parse command line for standard shell commands, DDE, file open

    CCommandLineInfo cmdInfo;

    ParseCommandLine(cmdInfo);

 

    // no empty document window on startup

    if (cmdInfo.m_nShellCommand == CCommandLineInfo::FileNew) {

        cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;

    }

 

    // Dispatch commands specified on the command line

    if (!ProcessShellCommand(cmdInfo))

        return FALSE;

 

    // The main window has been initialized, so show and update it.

    pMainFrame->ShowWindow(m_nCmdShow);

    pMainFrame->UpdateWindow();

 

    return TRUE;

}

//////////////////////////////////////////////////////////////////////

// CAboutDlg dialog used for App About

 

class CAboutDlg : public CDialog

{

public:

    CAboutDlg();

 

// Dialog Data

    //{{AFX_DATA(CAboutDlg)

    enum { IDD = IDD_ABOUTBOX };

    //}}AFX_DATA

    // ClassWizard generated virtual function overrides

    //{{AFX_VIRTUAL(CAboutDlg)

    protected:

    virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV

                                                     // support

    //}}AFX_VIRTUAL

 

// Implementation

protected:

    //{{AFX_MSG(CAboutDlg)

        // No message handlers

    //}}AFX_MSG

    DECLARE_MESSAGE_MAP()

};

 

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)

{

    //{{AFX_DATA_INIT(CAboutDlg)

    //}}AFX_DATA_INIT

}

 

void CAboutDlg::DoDataExchange(CDataExchange* pDX)

{

    CDialog::DoDataExchange(pDX);

    //{{AFX_DATA_MAP(CAboutDlg)

    //}}AFX_DATA_MAP

}

 

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)

    //{{AFX_MSG_MAP(CAboutDlg)

        // No message handlers

    //}}AFX_MSG_MAP

END_MESSAGE_MAP()

 

// App command to run the dialog

void CMymfc18App::OnAppAbout()

{

    CAboutDlg aboutDlg;

    aboutDlg.DoModal();

}

//////////////////////////////////////////////////////////////////////

// CMymfc18App message handlers

 

CDocument* CMymfc18App::OpenDocumentFile(LPCTSTR lpszFileName)

{

    TRACE("CMymfc18App::OpenDocumentFile\n");

    return CWinApp::OpenDocumentFile(lpszFileName);

}

 

 

Listing 5: The CMymfc18App source code listing.

 

Continue on next module...part 3

 

 

 

 

Further reading and digging:

  1. Standard C File Input/Output.

  2. Standard C++ File Input/Output.

  3. Win32 File Input/Output: Module C, Module D and Module E.

  4. MSDN MFC 7.0 class library online documentation.

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

  6. Porting & Migrating your older programs.

  7. MSDN Library

  8. DCOM at MSDN.

  9. COM+ at MSDN.

  10. COM at MSDN.

  11. Windows data type.

  12. Win32 programming Tutorial.

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

  14. Unicode and Multi-byte character set: Story and program examples.

 

 

 

 


| Tenouk C & C++ | MFC Home | MDI Serialization 1 | MDI Serialization 3 | Download | Site Index |