MFC文档感悟代码

 1 // sd.h : main header file for the SD application
 2 //
 3 
 4 #if !defined(AFX_SD_H__DB621B01_7480_4661_A977_D2FC564CCA3C__INCLUDED_)
 5 #define AFX_SD_H__DB621B01_7480_4661_A977_D2FC564CCA3C__INCLUDED_
 6 
 7 #if _MSC_VER > 1000
 8 #pragma once
 9 #endif // _MSC_VER > 1000
10 
11 #ifndef __AFXWIN_H__
12     #error include 'stdafx.h' before including this file for PCH
13 #endif
14 
15 #include "resource.h"       // main symbols
16 
17 /////
18 // CSdApp:
19 // See sd.cpp for the implementation of this class
20 //
21 
22 class CSdApp : public CWinApp
23 {
24 public:
25     CSdApp();
26 
27 // Overrides
28     // ClassWizard generated virtual function overrides
29     //{{AFX_VIRTUAL(CSdApp)
30     public:
31     virtual BOOL InitInstance();
32     //}}AFX_VIRTUAL
33 
34 // Implementation
35 
36 public:
37     //{{AFX_MSG(CSdApp)
38     afx_msg void OnAppAbout();
39         // NOTE - the ClassWizard will add and remove member functions here.
40         //    DO NOT EDIT what you see in these blocks of generated code !
41     //}}AFX_MSG
42     DECLARE_MESSAGE_MAP()
43 };
44 
45 
46 /////
47 
48 //{{AFX_INSERT_LOCATION}}
49 // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
50 
51 #endif // !defined(AFX_SD_H__DB621B01_7480_4661_A977_D2FC564CCA3C__INCLUDED_)
  1 // sd.cpp : Defines the class behaviors for the application.
  2 //
  3 
  4 #include "stdafx.h"
  5 #include "sd.h"
  6 
  7 #include "MainFrm.h"
  8 
  9 #ifdef _DEBUG
 10 #define new DEBUG_NEW
 11 #undef THIS_FILE
 12 static char THIS_FILE[] = __FILE__;
 13 #endif
 14 
 15 /////
 16 // CSdApp
 17 
 18 BEGIN_MESSAGE_MAP(CSdApp, CWinApp)
 19     //{{AFX_MSG_MAP(CSdApp)
 20     ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
 21         // NOTE - the ClassWizard will add and remove mapping macros here.
 22         //    DO NOT EDIT what you see in these blocks of generated code!
 23     //}}AFX_MSG_MAP
 24 END_MESSAGE_MAP()
 25 
 26 /////
 27 // CSdApp construction
 28 
 29 CSdApp::CSdApp()
 30 {
 31     // TODO: add construction code here,
 32     // Place all significant initialization in InitInstance
 33 }
 34 
 35 /////
 36 // The one and only CSdApp object
 37 
 38 CSdApp theApp;
 39 
 40 /////
 41 // CSdApp initialization
 42 
 43 BOOL CSdApp::InitInstance()
 44 {
 45     CMainFrame* pFrame = new CMainFrame;
 46     m_pMainWnd = pFrame;//主窗口地址必须记录
 47 //CWnd::CreateEx(...)
 48     pFrame->LoadFrame(IDR_MAINFRAME,
 49         WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL,
 50         NULL);
 51 
 52     pFrame->ShowWindow(SW_MAXIMIZE);
 53     pFrame->UpdateWindow();
 54 
 55     return TRUE;//必须返回TRUE;
 56 }
 57 
 58 /////
 59 // CSdApp message handlers
 60 
 61 
 62 
 63 
 64 
 65 /////
 66 // CAboutDlg dialog used for App About
 67 
 68 class CAboutDlg : public CDialog
 69 {
 70 public:
 71     CAboutDlg();
 72 
 73 // Dialog Data
 74     //{{AFX_DATA(CAboutDlg)
 75     enum { IDD = IDD_ABOUTBOX };
 76     //}}AFX_DATA
 77 
 78     // ClassWizard generated virtual function overrides
 79     //{{AFX_VIRTUAL(CAboutDlg)
 80     protected:
 81     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
 82     //}}AFX_VIRTUAL
 83 
 84 // Implementation
 85 protected:
 86     //{{AFX_MSG(CAboutDlg)
 87         // No message handlers
 88     //}}AFX_MSG
 89     DECLARE_MESSAGE_MAP()
 90 };
 91 
 92 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
 93 {
 94     //{{AFX_DATA_INIT(CAboutDlg)
 95     //}}AFX_DATA_INIT
 96 }
 97 
 98 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
 99 {
100     CDialog::DoDataExchange(pDX);
101     //{{AFX_DATA_MAP(CAboutDlg)
102     //}}AFX_DATA_MAP
103 }
104 
105 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
106     //{{AFX_MSG_MAP(CAboutDlg)
107         // No message handlers
108     //}}AFX_MSG_MAP
109 END_MESSAGE_MAP()
110 
111 // App command to run the dialog
112 void CSdApp::OnAppAbout()
113 {
114     CAboutDlg aboutDlg;
115     aboutDlg.DoModal();
116 }
117 
118 /////
119 // CSdApp message handlers
 1 #if !defined(AFX_MAINVIEW_H__A232082F_2230_4E08_97EA_73538F012DBE__INCLUDED_)
 2 #define AFX_MAINVIEW_H__A232082F_2230_4E08_97EA_73538F012DBE__INCLUDED_
 3 
 4 #if _MSC_VER > 1000
 5 #pragma once
 6 #endif // _MSC_VER > 1000
 7 // MainView.h : header file
 8 //
 9 
10 /////
11 // CMainView view
12 #include <afxcview.h>
13 class CMainView : public CListView
14 {
15 protected:
16     CMainView();           // protected constructor used by dynamic creation
17     DECLARE_DYNCREATE(CMainView)
18 
19 // Attributes
20 public:
21 
22 // Operations
23 public:
24 
25 // Overrides
26     // ClassWizard generated virtual function overrides
27     //{{AFX_VIRTUAL(CMainView)
28     public:
29     virtual void OnInitialUpdate();
30     protected:
31     virtual void OnDraw(CDC* pDC);      // overridden to draw this view
32     //}}AFX_VIRTUAL
33 
34 // Implementation
35 protected:
36     virtual ~CMainView();
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(CMainView)
45     afx_msg void OnEditAdd();
46     afx_msg void OnEditDel();
47     afx_msg void OnEditMod();
48     //}}AFX_MSG
49     DECLARE_MESSAGE_MAP()
50 };
51 
52 /////
53 
54 //{{AFX_INSERT_LOCATION}}
55 // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
56 
57 #endif // !defined(AFX_MAINVIEW_H__A232082F_2230_4E08_97EA_73538F012DBE__INCLUDED_)
  1 // MainView.cpp : implementation file
  2 //
  3 
  4 #include "stdafx.h"
  5 #include "sd.h"
  6 #include "MainView.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 // CMainView
 16 
 17 IMPLEMENT_DYNCREATE(CMainView, CListView)
 18 
 19 CMainView::CMainView()
 20 {
 21 }
 22 
 23 CMainView::~CMainView()
 24 {
 25 }
 26 
 27 
 28 BEGIN_MESSAGE_MAP(CMainView, CListView)
 29     //{{AFX_MSG_MAP(CMainView)
 30     ON_COMMAND(ID_EDIT_ADD, OnEditAdd)
 31     ON_COMMAND(ID_EDIT_DEL, OnEditDel)
 32     ON_COMMAND(ID_EDIT_MOD, OnEditMod)
 33     //}}AFX_MSG_MAP
 34 END_MESSAGE_MAP()
 35 
 36 /////
 37 // CMainView drawing
 38 
 39 void CMainView::OnDraw(CDC* pDC)
 40 {
 41     CDocument* pDoc = GetDocument();
 42     // TODO: add draw code here
 43 }
 44 
 45 /////
 46 // CMainView diagnostics
 47 
 48 #ifdef _DEBUG
 49 void CMainView::AssertValid() const
 50 {
 51     CListView::AssertValid();
 52 }
 53 
 54 void CMainView::Dump(CDumpContext& dc) const
 55 {
 56     CListView::Dump(dc);
 57 }
 58 #endif //_DEBUG
 59 
 60 /////
 61 // CMainView message handlers
 62 
 63 void CMainView::OnInitialUpdate() 
 64 {//类似于对话框的OnInitDialog,在没有显示之前
 65     CListView::OnInitialUpdate();
 66 
 67     CListCtrl &list = this->GetListCtrl();
 68     list.ModifyStyle(0,LVS_REPORT);
 69     list.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
 70     list.InsertColumn(0,"工号",0,180);
 71     list.InsertColumn(1,"姓名",0,180);
 72     list.InsertColumn(2,"部门",0,180);
 73     
 74 }
 75 #include "AddDlg.h"
 76 void CMainView::OnEditAdd() 
 77 {
 78     // TODO: Add your command handler code here
 79     CAddDlg dlg;
 80     if(dlg.DoModal() == IDCANCEL)
 81         return;
 82     CListCtrl &list = this->GetListCtrl();
 83     int i = list.GetItemCount();
 84     list.InsertItem(i,dlg.m_szNumb);
 85     list.SetItemText(i,1,dlg.m_szName);
 86     list.SetItemText(i,2,dlg.m_szDept);
 87     
 88 
 89 }
 90 
 91 void CMainView::OnEditDel() 
 92 {
 93     // TODO: Add your command handler code here
 94     
 95 }
 96 
 97 void CMainView::OnEditMod() 
 98 {
 99     // TODO: Add your command handler code here
100     
101 }
 1 // MainFrm.h : interface of the CMainFrame class
 2 //
 3 /////
 4 
 5 #if !defined(AFX_MAINFRM_H__9ECFA6B6_6F97_4C85_9DD5_396FF8608530__INCLUDED_)
 6 #define AFX_MAINFRM_H__9ECFA6B6_6F97_4C85_9DD5_396FF8608530__INCLUDED_
 7 
 8 #if _MSC_VER > 1000
 9 #pragma once
10 #endif // _MSC_VER > 1000
11 
12 #include "ChildView.h"
13 #include "MainView.h"
14 class CMainFrame : public CFrameWnd//SDI外框架
15 {
16     
17 public:
18     CMainFrame();
19 protected: 
20     DECLARE_DYNAMIC(CMainFrame)
21 
22 // Attributes
23 public:
24 
25 // Operations
26 public:
27 
28 // Overrides
29     // ClassWizard generated virtual function overrides
30     //{{AFX_VIRTUAL(CMainFrame)
31     virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
32     virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo);
33     //}}AFX_VIRTUAL
34 
35 // Implementation
36 public:
37     virtual ~CMainFrame();
38 #ifdef _DEBUG
39     virtual void AssertValid() const;
40     virtual void Dump(CDumpContext& dc) const;
41 #endif
42 
43 protected:  // control bar embedded members
44     CStatusBar  m_wndStatusBar;
45     CToolBar    m_wndToolBar;
46     //CChildView    m_wndView;
47     CMainView *m_pMainView;
48 
49 // Generated message map functions
50 protected:
51     //{{AFX_MSG(CMainFrame)
52     afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
53     afx_msg void OnSetFocus(CWnd *pOldWnd);
54     afx_msg BOOL OnEraseBkgnd(CDC* pDC);
55     //}}AFX_MSG
56     DECLARE_MESSAGE_MAP()
57 };
58 
59 /////
60 
61 //{{AFX_INSERT_LOCATION}}
62 // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
63 
64 #endif // !defined(AFX_MAINFRM_H__9ECFA6B6_6F97_4C85_9DD5_396FF8608530__INCLUDED_)
  1 // MainFrm.cpp : implementation of the CMainFrame class
  2 //
  3 
  4 #include "stdafx.h"
  5 #include "sd.h"
  6 
  7 #include "MainFrm.h"
  8 
  9 #ifdef _DEBUG
 10 #define new DEBUG_NEW
 11 #undef THIS_FILE
 12 static char THIS_FILE[] = __FILE__;
 13 #endif
 14 
 15 /////
 16 // CMainFrame
 17 
 18 IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)
 19 
 20 BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
 21     //{{AFX_MSG_MAP(CMainFrame)
 22     ON_WM_CREATE()
 23     ON_WM_SETFOCUS()
 24     ON_WM_ERASEBKGND()
 25     //}}AFX_MSG_MAP
 26 END_MESSAGE_MAP()
 27 
 28 static UINT indicators[] =
 29 {
 30     ID_SEPARATOR,           // status line indicator
 31     ID_INDICATOR_CAPS,
 32     ID_INDICATOR_NUM,
 33     ID_INDICATOR_SCRL,
 34 };
 35 
 36 /////
 37 // CMainFrame construction/destruction
 38 
 39 CMainFrame::CMainFrame()
 40 {
 41     // TODO: add member initialization code here
 42     m_pMainView = NULL;
 43     
 44 }
 45 
 46 CMainFrame::~CMainFrame()
 47 {
 48 }
 49 #include "MainView.h"
 50 int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
 51 {
 52     if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
 53         return -1;
 54     // create a view to occupy the client area of the frame
 55     /*
 56     if (!m_wndView.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
 57         CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL))
 58     {
 59         TRACE0("Failed to create view window\n");
 60         return -1;
 61     }//内部视图
 62     */
 63 
 64     //将VIEW和FRAME紧密联系在一起
 65     CCreateContext cc;
 66     cc.m_pNewViewClass =RUNTIME_CLASS(CMainView);
 67 
 68     m_pMainView  = ( CMainView* )CreateView(&cc);
 69     
 70 
 71 
 72     if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
 73         | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
 74         !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
 75     {
 76         TRACE0("Failed to create toolbar\n");
 77         return -1;      // fail to create
 78     }//工具栏创建
 79 
 80     if (!m_wndStatusBar.Create(this) ||
 81         !m_wndStatusBar.SetIndicators(indicators,
 82           sizeof(indicators)/sizeof(UINT)))
 83     {
 84         TRACE0("Failed to create status bar\n");
 85         return -1;      // fail to create
 86     }//状态栏支持
 87     m_wndToolBar.SetWindowText("标准工具栏");//在浮动之前设置标题
 88     // TODO: Delete these three lines if you don't want the toolbar to
 89     //  be dockable
 90     m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
 91     EnableDocking(CBRS_ALIGN_ANY);
 92     DockControlBar(&m_wndToolBar);//工具栏停靠
 93     //FloatControlBar(&m_wndToolBar,CPoint(200,200));//工具栏飘起来
 94     
 95     return 0;
 96 }
 97 
 98 BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
 99 {
100     if( !CFrameWnd::PreCreateWindow(cs) )
101         return FALSE;
102     // TODO: Modify the Window class or styles here by modifying
103     //  the CREATESTRUCT cs
104 
105     cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
106     cs.lpszClass = AfxRegisterWndClass(0);
107     return TRUE;
108 }
109 
110 /////
111 // CMainFrame diagnostics
112 
113 #ifdef _DEBUG
114 void CMainFrame::AssertValid() const
115 {
116     CFrameWnd::AssertValid();
117 }
118 
119 void CMainFrame::Dump(CDumpContext& dc) const
120 {
121     CFrameWnd::Dump(dc);
122 }
123 
124 #endif //_DEBUG
125 
126 /////
127 // CMainFrame message handlers
128 void CMainFrame::OnSetFocus(CWnd* pOldWnd)
129 {
130     // forward focus to the view window
131 //    m_wndView.SetFocus();//当框架得到焦点的时候,立即把焦点给视图
132 }
133 
134 BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
135 {
136     // let the view have first crack at the command
137 //    if (m_wndView.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))//命令转发
138 //        return TRUE;
139     if (m_pMainView->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))//消息分发机制
140         return TRUE;
141     // otherwise, do default handling
142     return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
143 }
144 
145 
146 BOOL CMainFrame::OnEraseBkgnd(CDC* pDC) 
147 {
148     // TODO: Add your message handler code here and/or call default
149     
150     //return CFrameWnd::OnEraseBkgnd(pDC);
151     //CRect rect;
152     //GetClientRect(rect);//获取客户区域
153 
154     //pDC ->FillSolidRect(rect,GetSysColor(COLOR_BTNSHADOW));
155     return TRUE;
156 }
 1 #if !defined(AFX_ADDDLG_H__CDC10284_A9B4_4155_904F_F0C87E1A0A9D__INCLUDED_)
 2 #define AFX_ADDDLG_H__CDC10284_A9B4_4155_904F_F0C87E1A0A9D__INCLUDED_
 3 
 4 #if _MSC_VER > 1000
 5 #pragma once
 6 #endif // _MSC_VER > 1000
 7 // AddDlg.h : header file
 8 //
 9 
10 /////
11 // CAddDlg dialog
12 
13 class CAddDlg : public CDialog
14 {
15 // Construction
16 public:
17     CAddDlg(CWnd* pParent = NULL);   // standard constructor
18 
19 // Dialog Data
20     //{{AFX_DATA(CAddDlg)
21     enum { IDD = IDD_ADD_DLG };
22     CString    m_szDept;
23     CString    m_szName;
24     CString    m_szNumb;
25     //}}AFX_DATA
26 
27 
28 // Overrides
29     // ClassWizard generated virtual function overrides
30     //{{AFX_VIRTUAL(CAddDlg)
31     protected:
32     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
33     //}}AFX_VIRTUAL
34 
35 // Implementation
36 protected:
37 
38     // Generated message map functions
39     //{{AFX_MSG(CAddDlg)
40         // NOTE: the ClassWizard will add member functions here
41     //}}AFX_MSG
42     DECLARE_MESSAGE_MAP()
43 };
44 
45 //{{AFX_INSERT_LOCATION}}
46 // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
47 
48 #endif // !defined(AFX_ADDDLG_H__CDC10284_A9B4_4155_904F_F0C87E1A0A9D__INCLUDED_)
 1 // AddDlg.cpp : implementation file
 2 //
 3 
 4 #include "stdafx.h"
 5 #include "sd.h"
 6 #include "AddDlg.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 // CAddDlg dialog
16 
17 
18 CAddDlg::CAddDlg(CWnd* pParent /*=NULL*/)
19     : CDialog(CAddDlg::IDD, pParent)
20 {
21     //{{AFX_DATA_INIT(CAddDlg)
22     m_szDept = _T("");
23     m_szName = _T("");
24     m_szNumb = _T("");
25     //}}AFX_DATA_INIT
26 }
27 
28 
29 void CAddDlg::DoDataExchange(CDataExchange* pDX)
30 {
31     CDialog::DoDataExchange(pDX);
32     //{{AFX_DATA_MAP(CAddDlg)
33     DDX_Text(pDX, IDC_DEPT, m_szDept);
34     DDX_Text(pDX, IDC_NAME, m_szName);
35     DDX_Text(pDX, IDC_NUMB, m_szNumb);
36     //}}AFX_DATA_MAP
37 }
38 
39 
40 BEGIN_MESSAGE_MAP(CAddDlg, CDialog)
41     //{{AFX_MSG_MAP(CAddDlg)
42         // NOTE: the ClassWizard will add message map macros here
43     //}}AFX_MSG_MAP
44 END_MESSAGE_MAP()
45 
46 /////
47 // CAddDlg message handlers

 

转载于:https://www.cnblogs.com/sangyangyang/p/6551487.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MFC (Microsoft Foundation Class)是一种用于Windows平台的应用程序开发框架,它提供了一套面向对象的类库,可以简化Windows应用程序的开发过程。多文档多视图是MFC框架中的一种常见应用模式,它允许用户同时打开多个文档,并在多个视图中显示这些文档的不同部分。 在MFC中,实现多文档多视图应用最关键的是建立一个文档类(CDocument)和至少一个视图类(CView)。文档类用于管理文档的数据和状态,而视图类则用于显示文档的内容,可以有不同类型的视图类对应不同的展示方式。通常情况下,一个应用程序可以有多个文档对象,每个文档对象可以对应多个视图对象。 在源代码中,可以通过继承MFC框架提供的基础类来实现文档类和视图类。文档类必须继承自CDocument类,并且需要重写其中的一些虚函数来处理文档的打开、保存、关闭等操作。视图类则需要继承自CView类,并且需要重写其中的一些虚函数来处理显示文档内容、用户交互等操作。在视图类中,通常需要处理绘图相关的操作,通过GDI(图形设备接口)来绘制文档内容。 在主应用程序中,还需要建立一个框架窗口类(CFrameWnd)来容纳多个视图对象。框架窗口类也需要继承自MFC框架提供的基础类,并在其中创建和管理文档对象和视图对象。用户可以通过菜单、工具栏等交互方式来打开、关闭、保存文档,以及在视图中进行操作。 总的来说,实现MFC文档多视图应用的源代码需要建立文档类、视图类和框架窗口类,并通过继承MFC框架提供的基础类来实现文档管理和视图显示的功能。通过合理的设计和编码,可以实现一个灵活、可扩展的多文档多视图应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值