浅谈MFC单文档(SDI)程序

来源:互联网 发布:程序员数学 编辑:程序博客网 时间:2024/04/30 21:45

1.新建MFC单文档(SDI)项目,命名为TestSDI,项目类型选择MFC标准,其他的选择默认配置。

向导自动会帮我们生成五个类,CAboutDlg、CMainFrame、CTestSDIApp、CTestSDIDoc、CTestSDIView。

分别对应“关于”对话框、视窗、主程序、文档、视图。其中四个类派生关系如下图所示:


2.为了了解SDI程序的运行流程,在各个类的构造函数、析构函数以、初始化函数等函数设置断点,通过单步调试来完成,程序运行的流程如下。

------->targetver.h ------->stdafx.h ------->resource.h------->TestSDI.h ------->MainFrm.h------->TestSDIDoc.h------->TestSDIView.h------------------------------------------------------------------------------------------------------------------------------------------------------->TestSDI.cppCTestSDIApp  theApp;    CTestSDIApp::CTestSDIApp();  //断点2CTestSDIApp::InitInstance();  //断点3------->TestSDIDoc.cppCTestSDIDoc::CTestSDIDoc();------->MainFrame.cppCMainFrame::CMainFrame();CMainFrame::PreCreateWindow(CREATESTRUCT& cs);CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct);------->TestSDIView.cppCTestSDIView::CTestSDIView();CTestSDIView::PreCreateWindow(CREATESTRUCT& cs);------->TestSDIDoc.cppCTestSDIDoc::OnNewDocument();------->TestSDIView.cppCTestSDIView::OnDraw(CDC* pDC);------------------------------------------------------------------------------------------------------------------------------------------------------->TestSDIView.cppCTestSDIView::~CTestSDIView();------->MainFrame.cppCMainFrame::~CMainFrame();------->TestSDIDoc.cppCTestSDIDoc::~CTestSDIDoc();------->TestSDI.cpp                CTestSDIApp::~CTestSDIApp();

断点2:跳至MFC内部,调用AfxWinMain(); 跳至断点3,这里就是程序winmain函数的接口。

int AfxAPI AfxWinMain(){CWinApp* pApp=AfxGetApp();AfxWinInit();pApp->InitApplication();pApp->InitInstance();nReturnCode=pApp->Run();//消息循环}



4.Frame/Document/View之间的连接

// 注册应用程序的文档模板。文档模板CSingleDocTemplate* pDocTemplate;pDocTemplate = new CSingleDocTemplate(IDR_MAINFRAME,RUNTIME_CLASS(CTestSDIDoc),RUNTIME_CLASS(CMainFrame),       // 主 SDI 框架窗口RUNTIME_CLASS(CTestSDIView));if (!pDocTemplate)return FALSE;AddDocTemplate(pDocTemplate);

这三份对象由一个Document Template对象来管理。我们可以将其想象为一个大的容器,将三者装起来。


5.Document/View 文档视图结构

1.Document是数据的体,View是数据的面。

2.view是Document的第一线,负责与使用者接触。





0 0
原创粉丝点击