又探MFC(一)基于对话框的MFC项目架构
大学毕设时学的MFC,工作头一年还用的MFC,之后再也用不到了.当时买的MFC书籍早就束之高阁了.现在需要开发一个简单的桌面程序,于是我又把MFC捡起来.MFC早已过时,不过如果开发简单的Windows桌面应用,MFC不失为一个简单的选择.
通过Visual studio MFC wizard创建MFC对话框项目xxx,一开始即存在两个类:应用程序类CxxxApp,以及对话框类CxxxDlg.
可通过AfxGetApp()获得CxxxApp实例.
可通过AfxGetApp()->m_pMainWnd获得CxxxDlg实例.
对话框
CxxxDlg继承自CDialogEx.MSDN查看CDialogEx.
CDialogEx描述:The CDialogEx class specifies the background color and backgroundimage of a dialog box.
再查看CDialogEx的父类CDialog.
CDialog描述:The base class used for displaying dialog boxes on the screen.
看其Public Methods以及Remarks主要提供创建和销毁对话框,以及提供OnInitDialog初始化对话框消息.
可以通过DoModel创建模式对话框,通过Create创建非模式对话框.Remarks解释了两种对话框类型.Dialog boxes are of two types: modal andmodeless. A modal dialog box must be closed by the user before the applicationcontinues. A modeless dialog box allows the user to display the dialog box andreturn to another task without canceling or removing the dialog box.
如何关闭对话框?Remarks介绍如下.
A modal dialog boxcloses automatically when the user presses the OK or Cancel buttons or whenyour code calls the EndDialog member function.
When you implement amodeless dialog box, always override the OnCancel member function and callDestroyWindow from within it. Don't call the base class CDialog::OnCancel,because it calls EndDialog, which will make the dialog box invisible but willnot destroy it. You should also override PostNcDestroy for modeless dialogboxes in order to delete this, since modeless dialog boxes are usuallyallocated with new. Modal dialog boxes are usually constructed on the frame anddo not need PostNcDestroy cleanup.
继续查看CDialog的父类CWnd.
CWnd描述:Provides the base functionality of all window classes in theMicrosoft Foundation Class Library.
看其Public Methods以及Remarks主要实现了消息路由,以及提供各类onxxx消息.
应用程序
CxxxApp继承自CWinApp.MSDN查看CWinApp.
CWinApp描述:The base class from which you derive a Windows application object.
应用程序对象整个应用程序唯一,所有地方都能访问.所以是放可供所有地方访问的对象和方法的好地方.
看CWinApp:The Application class.MFC的入口函数WinMain置于MFC库中,WinMain会调用CWinApp::InitInstance().而应用程序退出之前会调用CWinApp::ExitInstance(),我们可以在CxxxApp中覆盖ExitInstance,这是一个退出之前销毁对象的好地方.