Let's take a CDataSet class. See the code below, there is a detailed note: // dataset.h: Header File
/
// CDataSet Dao Recordset
// This is the header file of the CDataSet. Wizard has done a lot of work for us.
// We need to look at the RFX mechanism
Class CDataSet: Public CDAORECORDSET
{
PUBLIC:
Virtual ~ cdataaset ();
CDataSet (CDAODATABASE * PDATABASE = NULL);
Declare_dynamic (CDataSet)
// Field / param data
// {{AFX_FIELD (CDataSet, CDaorecordset)
Long m_id;
CString M_Name;
CString M_SEX;
COLEDATETIME M_BIRTHDAY;
CSTRING M_REGION;
CSTRING M_ADDRESS;
CSTRING M_MAIL;
CString M_HomePage;
CSTRING M_RESUME;
CSTRING M_SORT;
//}} AFX_FIELD
/ / The above is a field mapped by wizard.
// Overrides
// ClassWizard Generated Virtual Function Overrides
// {{AFX_VIRTUAL (CDataSet)
PUBLIC:
Virtual cstring getDefaultdbname (); // default database name
Virtual cstring getDefaultsql (); // default SQL for Recordset
Virtual void DofieldExchange (CDAOFieldExchange * PFX);
/ / Overload the DofieldExchange function above to get RFX support
//}} AFX_VIRTUAL
// Implementation
#ifdef _Debug
Virtual void assertvalid () const;
Virtual Void Dump (CDumpContext & DC) Const;
#ENDIF
}
// The following is the implementation of the DataSet class
// dataset.cpp: importation file
#include "stdafx.h"
#include "addressbook.h"
#include "dataset.h"
#ifdef _Debug
#define new debug_new
#undef this_file
Static char this_file [] = __file__;
#ENDIF
/
// cdataaset
Implement_dynamic (CDataSet, CDaorecordset)
CDataSet :: CDataSet (CDAODATABASE * PDB)
: CDAORECORDSET (PDB) // Handmade Add
{
// {{AFX_FIELD_INIT (CDataSet)
m_id = 0;
m_name = _t ("");
m_sex = _t ("");
m_birtHDay = (date) 0;
m_region = _t ("");
m_address = _t ("");
m_mail = _t ("");
m_homepage = _t ("");
M_Resume = _t ("");
m_sort = _t (""); m_nfields = 10; // represents the number of fields.
//}} AFX_FIELD_INIT
m_ndefaulttype = dbopendynaset;
}
/// Get the name of the default database
/// When the default database is not in the specified path, the universal file dialog is called.
CString CDataSet :: getDefaultdbname ()
{
CfileDialog Fldlg (True, _T ("MDB"), NULL, OFN_HIDEREADOONLY | OFN_OVERWRITEPROMPT,
_T ("Microsoft Access Database (*. MDB) | * .mdb |"));
CString startle;
start.loadstring (ids_filedialog_title); // This is the dialog title setting in String Table resource setting
Fldlg.m_ofn.lpstrtitle = startle;
IF (fldlg.domodal () == idok)
{
Return_T (fldlg.getpathname ());
}
Else
Return_T ("c: //addressbook.mdb");
}
/// Get the default record set
CSTRING CDataSet :: getDefaultsql ()
{
Return_T ("[FriendInfo]");
}
// This is the central DOFIELDEXCHANGE function of DFX, which is overloaded.
// It implements two-way data exchange between data sources "----" record set
/// Everyone should take a closer look at MSDN's description
Void CDataSet :: DofieldExchange * PFX)
{
// {{AFX_FIELD_MAP (CDataSet)
PFX-> setfieldType (CDAOFIELDEXCHANGE :: OutputColumn);
DFX_LONG (PFX, _T ("[Number]", m_id);
DFX_Text (PFX, _T ("[Name"), M_Name);
DFX_Text (PFX, _T ("[Sex]"), M_SEX);
DFX_Datetime (PFX, _T ("[birthday]"), m_birdhday);
DFX_Text (PFX, _T ("[Region]"), M_REGION;
DFX_Text (PFX, _T ("[Address]", M_Address;
DFX_Text (PFX, _T ("[Mail]", M_Mail);
DFX_Text (PFX, _T ("[Home]"), m_homepage);
DFX_Text (PFX, _T ("[Introduction]"), M_RESUME);
DFX_Text (PFX, _T ("[Abbreviation]"), M_SORT);
//}} AFX_FIELD_MAP
}
/ / The above is a DFX mechanism, and its role is a mapping between VC data types and SQL data categories.
/ / You can see a table in the document.
/
// CDataSet Diagnostics
#ifdef _Debug
Void CDataSet :: assertvalid () const
{
CDAORECORDSET :: assertvalid ();
}
Void CDataSet :: Dump (CDumpContext & DC) Const
{
CDAORECORDSET :: DUMP (DC);
}
#ENDIF / / _ Debug
CDataSet :: ~ cdataaset ()
{
}
11, ok, the above is all CDataSet classes. One of the following weight is the code of the view. Let me also put it out of its head files, let's take a look. #include "dataset.h" // address by classview This is Wizard dry
#iF _MSC_VER> 1000
#pragma overce
#ENDIF / / 100 m _ _ _
Class Cabview: Public ClistView
{
protected: // Create from Serialization Only
CabView ();
Declare_Dyncreate (CabVIEW)
// attributes
PUBLIC:
Cabdoc * getDocument ();
// Operations
PUBLIC:
// Overrides
// ClassWizard Generated Virtual Function Overrides
// {{AFX_VIRTUAL (CABVIEW)
PUBLIC:
Virtual void OnDraw (CDC * PDC); // Overridden to Draw this View
Virtual Bool PrecreateWindow (CreateStruct & Cs);
protected:
Virtual void OnInitialUpdate (); // Called First Time After Construct
Virtual Bool Onprepareprinting (CPrintInfo * Pinfo);
Virtual Void OnBeginPrinting (CDC * PDC, CPrintInfo * Pinfo);
Virtual void OneendPrinting (CDC * PDC, CPrintInfo * Pinfo);
//}} AFX_VIRTUAL
// Implementation
PUBLIC:
Void Sort (Bool Isasc); // Sort Function
Void showtableData (); // Plip and display record set functions
Virtual ~ CabView ();
#ifdef _Debug
Virtual void assertvalid () const;
Virtual Void Dump (CDumpContext & DC) Const;
#ENDIF
protected:
// generated message map functions
protected:
// {{AFX_MSG (CabVIEW)
AFX_MSG VOID ONCOLUMNCLICK (NMHDR * PNMHDR, LRESULT * PRESULT); // Select the list
AFX_MSG Void OnDBLCLK (NmHDR * PNMHDR, LRESULT * PRESULT); / / Double click on a record
AFX_MSG void onButtonAdd (); // Add new record
AFX_MSG void onbuttonall (); // Display all
AFX_MSG VOID ONBUTTONDEL (); // Delete the current record
AFX_MSG void onbuttonedit (); // Edit the current record
AFX_MSG void onbuttonfaind (); // Query record
AFX_MSG void onbuttonhelp (); // Help
//}} AFX_MSG
Declare_message_map ()
Private:
CDataSet * m_pset;
BOOL M_BISSHOWALL; / / Whether to display all records
BOOL M_BISASC; // Is it sorted?
}
#ifndef _debug // debug version in abview.cpp
Inline Cabdoc * Cabview :: getDocument () {return (cabdoc *) m_pdocument;
#ENDIF