首页 > 代码库 > MFC多文档感悟1
MFC多文档感悟1
1:在调用OnNewDocument之后会在内部调用Serialize
2:在调用OnPenNewDoucment之后会在内部调用Serialize
// fDlg.h : header file // #if !defined(AFX_FDLG_H__A45D9FA6_8710_41E3_9CDD_9AADE445DC3A__INCLUDED_) #define AFX_FDLG_H__A45D9FA6_8710_41E3_9CDD_9AADE445DC3A__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 ///////////////////////////////////////////////////////////////////////////// // CFDlg dialog #include "Doc.h" class CFDlg : public CDialog { CDoc m_doc; // Construction public: void Serialize(CArchive &ar); CFDlg(CWnd* pParent = NULL); // standard constructor // Dialog Data //{{AFX_DATA(CFDlg) enum { IDD = IDD_F_DIALOG }; CListCtrl m_list; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CFDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: HICON m_hIcon; // Generated message map functions //{{AFX_MSG(CFDlg) virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); afx_msg void OnAdd(); afx_msg void OnSave(); afx_msg void onl oad(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_FDLG_H__A45D9FA6_8710_41E3_9CDD_9AADE445DC3A__INCLUDED_)
1 // fDlg.cpp : implementation file 2 // 3 4 #include "stdafx.h" 5 #include "f.h" 6 #include "fDlg.h" 7 8 #ifdef _DEBUG 9 #define new DEBUG_NEW 10 #undef THIS_FILE 11 static char THIS_FILE[] = __FILE__; 12 #endif 13 14 ///////////////////////////////////////////////////////////////////////////// 15 // CAboutDlg dialog used for App About 16 17 class CAboutDlg : public CDialog 18 { 19 public: 20 CAboutDlg(); 21 22 // Dialog Data 23 //{{AFX_DATA(CAboutDlg) 24 enum { IDD = IDD_ABOUTBOX }; 25 //}}AFX_DATA 26 27 // ClassWizard generated virtual function overrides 28 //{{AFX_VIRTUAL(CAboutDlg) 29 protected: 30 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support 31 //}}AFX_VIRTUAL 32 33 // Implementation 34 protected: 35 //{{AFX_MSG(CAboutDlg) 36 //}}AFX_MSG 37 DECLARE_MESSAGE_MAP() 38 }; 39 40 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 41 { 42 //{{AFX_DATA_INIT(CAboutDlg) 43 //}}AFX_DATA_INIT 44 } 45 46 void CAboutDlg::DoDataExchange(CDataExchange* pDX) 47 { 48 CDialog::DoDataExchange(pDX); 49 //{{AFX_DATA_MAP(CAboutDlg) 50 //}}AFX_DATA_MAP 51 } 52 53 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 54 //{{AFX_MSG_MAP(CAboutDlg) 55 // No message handlers 56 //}}AFX_MSG_MAP 57 END_MESSAGE_MAP() 58 59 ///////////////////////////////////////////////////////////////////////////// 60 // CFDlg dialog 61 62 CFDlg::CFDlg(CWnd* pParent /*=NULL*/) 63 : CDialog(CFDlg::IDD, pParent) 64 { 65 //{{AFX_DATA_INIT(CFDlg) 66 // NOTE: the ClassWizard will add member initialization here 67 //}}AFX_DATA_INIT 68 // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 69 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 70 } 71 72 void CFDlg::DoDataExchange(CDataExchange* pDX) 73 { 74 CDialog::DoDataExchange(pDX); 75 //{{AFX_DATA_MAP(CFDlg) 76 DDX_Control(pDX, IDC_LIST, m_list); 77 //}}AFX_DATA_MAP 78 } 79 80 BEGIN_MESSAGE_MAP(CFDlg, CDialog) 81 //{{AFX_MSG_MAP(CFDlg) 82 ON_WM_SYSCOMMAND() 83 ON_WM_PAINT() 84 ON_WM_QUERYDRAGICON() 85 ON_BN_CLICKED(IDC_ADD, OnAdd) 86 ON_BN_CLICKED(IDC_SAVE, OnSave) 87 ON_BN_CLICKED(IDC_LOAD, onl oad) 88 //}}AFX_MSG_MAP 89 END_MESSAGE_MAP() 90 91 ///////////////////////////////////////////////////////////////////////////// 92 // CFDlg message handlers 93 #include "resource.h" 94 BOOL CFDlg::OnInitDialog() 95 { 96 CDialog::OnInitDialog(); 97 98 m_list.InsertColumn(0,"工号",0,100); 99 m_list.InsertColumn(1,"姓名",0,100); 100 // Add "About..." menu item to system menu. 101 102 // IDM_ABOUTBOX must be in the system command range. 103 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 104 ASSERT(IDM_ABOUTBOX < 0xF000); 105 106 CMenu* pSysMenu = GetSystemMenu(FALSE); 107 if (pSysMenu != NULL) 108 { 109 CString strAboutMenu; 110 strAboutMenu.LoadString(IDS_ABOUTBOX); 111 if (!strAboutMenu.IsEmpty()) 112 { 113 pSysMenu->AppendMenu(MF_SEPARATOR); 114 pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 115 } 116 } 117 118 // Set the icon for this dialog. The framework does this automatically 119 // when the application‘s main window is not a dialog 120 SetIcon(m_hIcon, TRUE); // Set big icon 121 SetIcon(m_hIcon, FALSE); // Set small icon 122 123 // TODO: Add extra initialization here 124 125 return TRUE; // return TRUE unless you set the focus to a control 126 } 127 128 void CFDlg::OnSysCommand(UINT nID, LPARAM lParam) 129 { 130 if ((nID & 0xFFF0) == IDM_ABOUTBOX) 131 { 132 CAboutDlg dlgAbout; 133 dlgAbout.DoModal(); 134 } 135 else 136 { 137 CDialog::OnSysCommand(nID, lParam); 138 } 139 } 140 141 // If you add a minimize button to your dialog, you will need the code below 142 // to draw the icon. For MFC applications using the document/view model, 143 // this is automatically done for you by the framework. 144 145 void CFDlg::OnPaint() 146 { 147 if (IsIconic()) 148 { 149 CPaintDC dc(this); // device context for painting 150 151 SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 152 153 // Center icon in client rectangle 154 int cxIcon = GetSystemMetrics(SM_CXICON); 155 int cyIcon = GetSystemMetrics(SM_CYICON); 156 CRect rect; 157 GetClientRect(&rect); 158 int x = (rect.Width() - cxIcon + 1) / 2; 159 int y = (rect.Height() - cyIcon + 1) / 2; 160 161 // Draw the icon 162 dc.DrawIcon(x, y, m_hIcon); 163 } 164 else 165 { 166 CDialog::OnPaint(); 167 } 168 } 169 170 // The system calls this to obtain the cursor to display while the user drags 171 // the minimized window. 172 HCURSOR CFDlg::OnQueryDragIcon() 173 { 174 return (HCURSOR) m_hIcon; 175 } 176 177 void CFDlg::OnAdd() 178 { 179 // TODO: Add your control notification handler code here 180 CString str; 181 GetDlgItemText(IDC_NUMB,str); 182 int i = m_list.GetItemCount(); 183 m_list.InsertItem(i,str); 184 GetDlgItemText(IDC_NAME,str); 185 m_list.SetItemText(i,1,str); 186 } 187 188 void CFDlg::OnSave() 189 { 190 // TODO: Add your control notification handler code here 191 CFileDialog fd(FALSE,"*.fff",NULL,OFN_OVERWRITEPROMPT, 192 "信息文件(*.fff)|*.fff|所有文件|*.*||",NULL); 193 if(IDCANCEL==fd.DoModal()) 194 { 195 return; 196 } 197 m_doc.OnSaveDocument(fd.GetPathName()); 198 199 } 200 201 void CFDlg::OnLoad() 202 { 203 // TODO: Add your control notification handler code here 204 CFileDialog fd(TRUE,"*.fff",NULL,OFN_OVERWRITEPROMPT, 205 "信息文件(*.fff)|*.fff|所有文件|*.*||",NULL); 206 if(IDCANCEL==fd.DoModal()) 207 { 208 return; 209 } 210 m_doc.OnOpenDocument(fd.GetPathName()); 211 } 212 213 void CFDlg::Serialize(CArchive &ar) 214 { 215 if(ar.IsStoring()) 216 { 217 int i = 0,nCount = m_list.GetItemCount(); 218 ar << nCount; 219 while(i < nCount) 220 { 221 int nNumb = atoi(m_list.GetItemText(i,0)); 222 CString szName = m_list.GetItemText(i,1); 223 ar << nNumb << szName; 224 ++i; 225 } 226 } 227 else 228 { 229 m_list.DeleteAllItems(); 230 int nNumb = 0; 231 CString szName; 232 CString str; 233 234 int i = 0,nCount = 0; 235 ar >> nCount; 236 while(i<nCount) 237 { 238 ar >> nNumb >> szName; 239 str.Format("%d",nNumb); 240 m_list.InsertItem(i,str); 241 m_list.SetItemText(i,1,szName); 242 ++i; 243 } 244 } 245 }
1 #if !defined(AFX_DOC_H__2373BCC2_D5B2_4E35_8C62_3D8353264404__INCLUDED_) 2 #define AFX_DOC_H__2373BCC2_D5B2_4E35_8C62_3D8353264404__INCLUDED_ 3 4 #if _MSC_VER > 1000 5 #pragma once 6 #endif // _MSC_VER > 1000 7 // Doc.h : header file 8 // 9 10 ///////////////////////////////////////////////////////////////////////////// 11 // CDoc document 12 13 class CDoc : public CDocument 14 { 15 public: 16 CDoc(); // protected constructor used by dynamic creation 17 DECLARE_DYNCREATE(CDoc) 18 19 // Attributes 20 public: 21 22 // Operations 23 public: 24 25 // Overrides 26 // ClassWizard generated virtual function overrides 27 //{{AFX_VIRTUAL(CDoc) 28 public: 29 virtual void Serialize(CArchive& ar); // overridden for document i/o 30 protected: 31 virtual BOOL OnNewDocument(); 32 //}}AFX_VIRTUAL 33 34 // Implementation 35 public: 36 virtual ~CDoc(); 37 #ifdef _DEBUG 38 virtual void AssertValid() const; 39 virtual void Dump(CDumpContext& dc) const; 40 #endif 41 42 // Generated message map functions 43 protected: 44 //{{AFX_MSG(CDoc) 45 // NOTE - the ClassWizard will add and remove member functions here. 46 //}}AFX_MSG 47 DECLARE_MESSAGE_MAP() 48 }; 49 50 //{{AFX_INSERT_LOCATION}} 51 // Microsoft Visual C++ will insert additional declarations immediately before the previous line. 52 53 #endif // !defined(AFX_DOC_H__2373BCC2_D5B2_4E35_8C62_3D8353264404__INCLUDED_)
1 // Doc.cpp : implementation file 2 // 3 4 #include "stdafx.h" 5 #include "f.h" 6 #include "Doc.h" 7 8 #ifdef _DEBUG 9 #define new DEBUG_NEW 10 #undef THIS_FILE 11 static char THIS_FILE[] = __FILE__; 12 #endif 13 14 ///////////////////////////////////////////////////////////////////////////// 15 // CDoc 16 17 IMPLEMENT_DYNCREATE(CDoc, CDocument) 18 19 CDoc::CDoc() 20 { 21 } 22 23 BOOL CDoc::OnNewDocument() 24 { 25 if (!CDocument::OnNewDocument()) 26 return FALSE; 27 return TRUE; 28 } 29 30 CDoc::~CDoc() 31 { 32 } 33 34 35 BEGIN_MESSAGE_MAP(CDoc, CDocument) 36 //{{AFX_MSG_MAP(CDoc) 37 // NOTE - the ClassWizard will add and remove mapping macros here. 38 //}}AFX_MSG_MAP 39 END_MESSAGE_MAP() 40 41 ///////////////////////////////////////////////////////////////////////////// 42 // CDoc diagnostics 43 44 #ifdef _DEBUG 45 void CDoc::AssertValid() const 46 { 47 CDocument::AssertValid(); 48 } 49 50 void CDoc::Dump(CDumpContext& dc) const 51 { 52 CDocument::Dump(dc); 53 } 54 #endif //_DEBUG 55 56 ///////////////////////////////////////////////////////////////////////////// 57 // CDoc serialization 58 59 void CDoc::Serialize(CArchive& ar) 60 { 61 if (ar.IsStoring()) 62 { 63 // TODO: add storing code here 64 AfxGetMainWnd()->Serialize(ar); 65 } 66 else 67 { 68 AfxGetMainWnd()->Serialize(ar); 69 // TODO: add loading code here 70 } 71 } 72 73 ///////////////////////////////////////////////////////////////////////////// 74 // CDoc commands
MFC多文档感悟1
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。