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 OnLoad();
    //}}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, OnLoad)
 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
时间: 2024-08-11 13:01:14

MFC多文档感悟1的相关文章

mfc学习---文档视图架构

MFC的AppWizard可以生成三种类型的应用程序:基于对话框的应用.单文档应用(SDI)和多文档应用(MDI). 一般情况下,采用文档/视结构的应用程序至少应由以下对象组成:       1.应用程序是一个CwinApp派生对象,它充当全部应用程序的容器.应用程序沿消息映射网络分配消息给它的所有子程序.       2.框架窗口是一CfrmeWnd派生对象.       3.文档是一个CDocument派生对象,它存储应用程序的数据,并把这些信息提供给应用程序的其余部分.       4.视

C++MFC编程笔记day07 MFC单文档绘图保存、多文档绘图保存

完善绘图例子(day06中的),加上保存功能 1 设计和编写图形类 CShape 1.1 成员变量 CPoint m_ptBegin; CPoint m_ptEnd; UINT m_nType; 1.2 支持序列化 1.2.1 继承自CObject 1.2.2 添加序列化的声明宏和实现宏 1.2.3 重写虚函数Serilize(),在函数中,完成成员变量的序列化 2 由于保存多个图形,引入MFC的集合类CObArray,保存的是CObject 对象的地址. 3 图形数据需要保存到文档类中,在该类

openGL三维网格坐标,旋转,缩放,灯光设置,纹理读取,模型读取(MFC单文档)

最近学习计算机图形学写的基于opengGL的作业 源码下载链接:链接:http://pan.baidu.com/s/1slANShZ 密码:hbwj 1.三维网格坐标建立 2.基本3维图形创建 3.鼠标相应旋转缩放 4.键盘相应旋转缩放 5.灯光设置 6.纹理载入映射 7.读取模型 关于MFC配置编写openGL网上有很多教程 需要的函数创建一般是: OnCreat() OnDestroy() Onsize() PreCreateWindow() OnDraw() 在我的MFC单文档项目中ena

MFC多文档的创建和打开过程

我们在学习MFC的时候,时常是很迷糊的,往往微软很多很好的机制都都深埋到代码里面了,让你无法看清她的神秘面纱.常常我们是糊里糊涂的将代码写出来了,往往是知其然,不知其所以然.这样的时候,有时候让我感觉很惶恐.叫我去看msdn,有发现自己的英文水平有限,再者,实在没这个耐心将其看完.看到一本很经典的书<深入浅出MFC这本书对MFC的机制讲的很好.仔细看了会对你受益匪浅,让你认识到,VC MFC编程就是拖拖控件这么简单的事情. MFC最经典的就是她的文档视图结构了 ,本文就愿作抛砖引玉之石.有不对之

MFC单文档的视图分割

最近做的工作里包括了MFC单文档的分割,过程很简单,但还是想记录下来. 如下进行了单文档分割为三个视图的操作: 1 利用类向导建立额外的两个view  基类是CView 2 在mainFirm.cpp中包含两个新加的view的头文件 3 在mainFirm.h中添加分割窗口的成员变量CSplitterWnd m_wndSplitter; 4 在mainFirm.cpp中重写oncreateclient BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT

[转载]MFC一个文档不同视图

MDI(MultipleDocument Interface)是Windows界面的一种规范,它建立多个窗口来浏览文档数据,如Windows中的ProgramManager等都是按MDI规范实现的.在实际工程软件开发中,许多程序员将其作为一种实现多窗口的标准方法.微软基础类库(MicrosoftFoundationClassLibrary,简称MFC库),是微软公司为方便Windows程序开发所提供的一个功能强大的通用类库.MFC的核心是以类的形式封装了大量WindowsAPI.在可视化编程语言

VTk与MFC单文档程序联合编程

兴趣需要,想做下VTK与MFC想结合的程序,MFC快要在桌面程序上面失去市场份额了,现在大多使用QT来做,但是本科的时候学的就是MFC,也相对来说比较熟悉,所以就想使用MFC来写一个简单的单文档程序.首先我们需要在编译的时候将USEGUISUPPORT->USEMFC勾选上,才能在MFC平台上使用VTK.网络上现在大多流行两种VTK和MFC的方法,其实两者结合的关键就是将VTK的绘制窗口vtkrenderwindow与MFC中的view窗口相一致,让VTK上的绘制图形能够在MFC上的VIEW类上

关于MFC视图文档框架的理解-1

一.单个文档和多个文档的区别: MFC分为对话框程序,单文档程序和多文档程序. 单个文档程序:一个主框架内仅允许打开一个视图,若要打开另一个,则当前的文档必须得关闭.单文档程序可以打开不同格式的文档. 对于单文档程序,主框架(CFrameWnd).视图(CView).文档(CDocument)被文档模板CSingleDocTemplate粘合在一起. 多个文档程序:一个主框架内可以打开多个视图. 对于多文档程序,主框架(CMDIFrameWnd)是单独存在的,子框架(CMDIChildWnd).

8.MFC多文档视图构架程序

一.多文档视图构架程序 1.相关问题 可以同时管理多个文档(可以有多个文档类对象) 2.相关类 CWinApp / CDocument / CView / CMIDChildWnd / CMIDFrameWnd CDocTemplate CMultiDocTemplate - 多文档模板类 CDocManager - 文档管理类