您的位置:首页 > 其它

初步理解“单文档程序框架”(一)

2002-10-23 09:31 459 查看
初步理解“单文档程序框架”(一)

初学VC的人都会觉得头痛,尤其对MFC AppWizard 生成的程序框架不理解。确实,那些古怪的代码够你看很长时间的。其实要说简单,可以不必理会那些代码,只要在需要的地方加入自己的合法代码,一样可以编出自己的应用小程序。但是,如果不理解向导生成的代码,就很难做出较大的程序。因为你并没有真正理解Windows编程机制,更不能掌握MFC编程的实质。所以,你如果打算继续用MFC开发Windows应用程序,还是认真阅读一下向导生成的代码,随着以后的实践便会慢慢掌握。
其实我学VC也不久,理解的也不够深刻——只能作到“初步理解”(既做一些注释而已)。这次我把它贴出来,希望和初学者共同探讨,也希望得到高人的指点。总之,共同进步是我们学习的目的。
另外,代码看起来奇怪的原因之一是用了“匈牙利命名规则”,看多了也就习惯了。如果对“匈牙利命名规则”不了解,请参看有关书籍。
向导生成的工程中主要包含了:应用类、主框架类、文档类、视图类等类。每个类的声明和定义都位于.h.cpp文件中。这一次我们先来看看应用类。
打开VC,通过“新建”,用MFC AppWizard(.exe)生成默认的基于“单文档“的应用程序。我在建立时,工程取名为wl ,其余各步骤默认。
完成后打开wl.h文件,会看到类似以下内容。中文注释是我加上去的,希望对大家理解代码有所帮助。
// wl.h : main header file for the WL application
//这是应用类的头文件,主要做声明变量和函数时用。
#if !defined(AFX_WL_H__5DDF8EDB_C6DA_11D5_B729_00E04C69899E__INCLUDED_)
#define AFX_WL_H__5DDF8EDB_C6DA_11D5_B729_00E04C69899E__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#ifndef __AFXWIN_H__
#error include 'stdafx.h' before including this file for PCH
#endif
#include "resource.h" // main symbols
/////////////////////////////////////////////////////////////////////////////
// CWlApp:
// See wl.cpp for the implementation of this class
//刚才键入的工程名被演化成这个工程的应用类名,既CWlApp
class CWlApp : public CWinApp //声明本程序的应用类,派生自CWinApp
{
public:
CWlApp();
//以下一段是虚函数重载声明,由系统自动管理,一般不用修改
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CWlApp)
public:
virtual BOOL InitInstance();
//}}AFX_VIRTUAL
//以下是向导做的消息映射声明。想了解作用,可参看Windows消息映射机制。
// Implementation
//{{AFX_MSG(CWlApp)
afx_msg void OnAppAbout();
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_WL_H__5DDF8EDB_C6DA_11D5_B729_00E04C69899E__INCLUDED_)

让我们再看看wl.cpp文件,它位于Source File中:

// wl.cpp : Defines the class behaviors for the application.
//以下包含了运行应用类所须的头文件
#include "stdafx.h"
#include "wl.h"
#include "MainFrm.h"
#include "wlDoc.h"
#include "wlView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CWlApp
//开始消息映射
BEGIN_MESSAGE_MAP(CWlApp, CWinApp)
//{{AFX_MSG_MAP(CWlApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)//“帮助”菜单中“关于”的消息映射
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Standard file based document commands
ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)//“文件”菜单中“新建”的消息映射
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)//“文件”菜单中“打开”的消息映射
// Standard print setup command
ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)//“文件”菜单中“打印设置”的消息映射
END_MESSAGE_MAP()
//结束消息映射
/////////////////////////////////////////////////////////////////////////////
// CWlApp construction
CWlApp::CWlApp() //定义构造函数,可以加入自己的代码,初始化时使用
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
////////////////////////////////////////////////////////////////////////////
// The one and only CWlApp object
CWlApp theApp;//定义应用类的对象。注意这是一个全局的对象,程序运行时首先自动查找它。
/////////////////////////////////////////////////////////////////////////////
// CWlApp initialization
BOOL CWlApp::InitInstance()//虚函数重载,运行程序时系统调用它完成初始化工作。
{
AfxEnableControlContainer();

// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else //允许使用动态3D控件
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif //允许使用静态3D控件

// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
//默认的注册表相关操作。
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(); // Load standard INI file options (including MRU)
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.

CSingleDocTemplate* pDocTemplate; //单文档模板指针的定义
pDocTemplate = new CSingleDocTemplate( //新建单文档应用程序模板
IDR_MAINFRAME,
RUNTIME_CLASS(CWlDoc),
RUNTIME_CLASS(CMainFrame), //运行类的宏定义,在程序运行过程中监视该类对象与其基类的相关信息
RUNTIME_CLASS(CWlView));
AddDocTemplate(pDocTemplate); //将新建的模板添加到应用程序

// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo); //保存命令行参数到对象cmdInfo 中。

// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;

// The one and only window has been initialized, so show and update it.
//主窗口的初始化
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
//当单击“关于”菜单项时将弹出对话框,它是派生自基类CDialog
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };//“关于”菜单项默认的ID标识
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
// No message handlers
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)//消息映射
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// App command to run the dialog
void CWlApp::OnAppAbout()//用户单击“关于”时调用此函数
{
CAboutDlg aboutDlg;
aboutDlg.DoModal(); //DoModal()是“模式对话框”最常用的函数,它负责对话框的显示和终止。
}
/////////////////////////////////////////////////////////////////////////////
// CWlApp message handlers

代码先简单注释到这里,关于用到的函数和某些机制可以参阅MSDN(非常好的东西)。还有一个常见的问题,在上述代码中并看不见Windows程序运行时所需的WinMain()函数,那时因为它被封装到了框加内部,对用户来说是透明的,你可以不必理会。如果想了解WinMain()函数的运行机制,可以参考其他Windows编程书籍。我可以推荐两本:《Windows程序设计》和《Windows API 参考手册》,都可以更深的了解地层机制。
一个工程的“应用类”,是最基本的一个类,它完成了应用程序的初始化和其他准备工作,是程序正常运行的前提。所以,仅靠简单的理解是不够的,还须和其他几个类综合理解才能真正领悟其中奥秘,这一切都需要我们今后在实践中不断努力。我们一定要记住这个真理:实践是提高编程水平的唯一有效途径。

欢迎交流、指导: ml457@163.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: