Programming with Microsoft Visual C++.NET 6ed [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Programming with Microsoft Visual C++.NET 6ed [Electronic resources] - نسخه متنی

George Shepherd, David Kruglinski

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
توضیحات
افزودن یادداشت جدید








The Ex15a Example: A Simple Document-View Interaction


The first of this chapter's two examples shows a very simple document-view interaction. The CEx15aDoc document class, which is derived from CDocument, allows for a single embedded CStudent object. The CStudent class represents a student record composed of a CString name and an integer grade. The CEx15aView view class is derived from CFormView. It is a visual representation of a student record that has edit controls for the name and grade. The default Enter pushbutton updates the document with data from the edit controls. Figure 15-1 shows the Ex15a program window.


Figure 15-1: The Ex15a program in action.

The code for the CStudent class is shown below. Most of the class's features serve Ex15a, but a few items carry forward to Ex15b and the programs discussed in Chapter 16. For now, take note of the two data members, the default constructor, the operators, and the Dump function declaration. The DECLARE_DYNAMIC and IMPLEMENT_DYNAMIC macros ensure that the class name is available for the diagnostic dump.

Student.h






// student.h
#ifndef _INSIDE_VISUAL_CPP_STUDENT
#define _INSIDE_VISUAL_CPP_STUDENT
class CStudent : public CObject
{
DECLARE_DYNAMIC(CStudent)
public:
CString m_strName;
int m_nGrade;
CStudent()
{
m_nGrade = 0;
}
CStudent(const char* szName, int nGrade) : m_strName(szName)
{
m_nGrade = nGrade;
}
CStudent(const CStudent& s) : m_strName(s.m_strName)
{
// copy constructor
m_nGrade = s.m_nGrade;
}
const CStudent& operator =(const CStudent& s)
{
m_strName = s.m_strName;
m_nGrade = s.m_nGrade;
return *this;
}
BOOL operator ==(const CStudent& s) const
{
if ((m_strName == s.m_strName) && (m_nGrade == s.m_nGrade)) {
return TRUE;
}
else {
return FALSE;
}
}
BOOL operator !=(const CStudent& s) const
{
// Let's make use of the operator we just defined!
return !(*this == s);
}
#ifdef _DEBUG
void Dump(CDumpContext& dc) const;
#endif // _DEBUG
};
#endif // _INSIDE_VISUAL_CPP_STUDENT












Student.cpp






#include "stdafx.h"
#include "student.h"
IMPLEMENT_DYNAMIC(CStudent, CObject)
#ifdef _DEBUG
void CStudent::Dump(CDumpContext& dc) const
{
CObject::Dump(dc);
dc << "m_strName = " << m_strName << "\nm_nGrade = " << m_nGrade;
}
#endif // _DEBUG











Follow these steps to build the Ex15a example:



    Run the MFC Application Wizard to generate the Ex15a project. Make it an SDI application. On the Generated Classes page, change the view's base class to CFormView, as shown here.




    Use the menu editor to replace the Edit menu commands. Delete the current Edit menu commands and replace them with a Clear All command. Use the default constant ID_EDIT_CLEARALL, which is assigned by the application framework.



    Use the dialog editor to modify the IDD_EX15A_FORM dialog box.Open the MFC Application Wizard–generated dialog box IDD_EX15A_FORM, and then add controls as shown here.


    Be sure that you set the following properties in the dialog editor's Properties window: Style = Child, Border = None, Visible = False. Use the following IDs for the controls.



















    Control


    ID


    Name edit control


    IDC_NAME


    Grade edit control


    IDC_GRADE


    Enter button


    IDC_ENTER




    Use Class View's Properties window to add message handlers for CEx15aView. Select the CEx15aView class, and then add handlers for the following messages. Accept the default function names.



















    Object ID


    Message


    Member Function


    IDC_ENTER


    BN_CLICKED


    OnBnClickedEnter


    ID_EDIT_CLEARALL


    COMMAND


    OnEditClearall


    ID_EDIT_CLEARALL


    UPDATE_COMMAND_UI


    OnUpdateEditClearall




    Use the Add Member Variable Wizard to add variables for CEx15aView. In Class View, right-click on CEx15aView and choose Add Variable. Add the following variables:
















    Control ID


    Member Variable


    Category


    Variable Type


    IDC_GRADE


    m_nGrade


    Value


    int


    IDC_NAME


    m_strName


    Value


    CString


    For m_nGrade, enter a minimum value of 0 and a maximum value of 100. Notice that the Add Member Variable Wizard generates the code necessary to validate data entered by the user.



    Add a prototype for the helper function UpdateControlsFromDoc. In Class View, right-click on CEx15aView and choose Add Function. Fill out the dialog box to add the following function:

    private:
    void UpdateControlsFromDoc(void);



    Edit the file

    Ex15aView.cpp . The MFC Application Wizard generated the skeleton OnInitialUpdate function, and the Add Member Function Wizard available from Class View generated the skeleton UpdateControlsFromDoc function. UpdateControlsFromDoc is a private helper member function that transfers data from the document to the CEx15aView data members and then to the dialog edit controls. Edit the code as shown here:

    void CEx15aView::OnInitialUpdate()
    { // called on startup
    CFormView::OnInitialUpdate();
    UpdateControlsFromDoc();
    }
    void CEx15aView::UpdateControlsFromDoc(void)
    { // called from OnInitialUpdate and OnEditClearall
    CEx15aDoc* pDoc = GetDocument();
    m_nGrade = pDoc->m_student.m_nGrade;
    m_strName = pDoc->m_student.m_strName;
    UpdateData(FALSE); // calls DDX
    }

    The OnBnClickedEnter 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 boldface code shown here:

    void CEx15aView::OnBnClickedEnter()
    {
    CEx15aDoc* pDoc = GetDocument();
    UpdateData(TRUE);
    pDoc->m_student.m_nGrade = m_nGrade;
    pDoc->m_student.m_strName = m_strName;
    }

    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 user interface handler disables the menu command if the document's student object is already blank. Add the following boldface code:

    void CEx15aView::OnEditClearall()
    {
    GetDocument()->m_student = CStudent(); // "blank" student object
    UpdateControlsFromDoc();
    }
    void CEx15aView::OnUpdateEditClearall(CCmdUI* pCmdUI)
    {
    pCmdUI->Enable(GetDocument()->m_student != CStudent()); // blank?
    }



    Edit the Ex15a project to add the files for CStudent. Be sure that

    Student.h and

    Student.cpp are in your project directory. Choose Add Existing Item from the Project menu and select the

    Student.h header and the

    Student.cpp source code files. Visual C++ .NET will add the files' names to the project's project file so that they will be compiled when you build the project.



    Add a CStudent data member to the CEx15aDoc class. Edit the code in

    Ex15aDoc.h and remember to include

    Student.h in the C

    Ex15aDoc.h file.

    public:
    CStudent m_student;

    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

    Ex15aDoc.cpp file. Use the CEx15aDoc constructor to initialize the student object, as shown here:

    CEx15aDoc::CEx15aDoc() : m_student("default value", 0)
    {
    TRACE("Document object constructed\n");
    }

    We can't tell whether the Ex15a 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:

    CEx15aDoc::~CEx15aDoc()
    {
    #ifdef _DEBUG
    Dump(afxDump);
    #endif // _DEBUG
    }
    void CEx15aDoc::Dump(CDumpContext& dc) const
    {
    CDocument::Dump(dc);
    dc << "\n" << m_student << "\n";
    }



    Build and test the Ex15a application. Type a name and a grade, and then click Enter. Now exit the application. Does the Debug window show messages similar to those shown here?

    a CEx15aDoc at $411580 
    m_strTitle = Untitled
    m_strPathName =
    m_bModified = 0
    m_pDocTemplate = $4113A0
    a CStudent at $4115D4
    m_strName = Sullivan, Walter
    m_nGrade = 78







Note

To see these messages, you must compile the application with the DEBUG symbol defined or with the Debug configuration selected.



/ 319