MFC的六大关键技术:
1)MFC程序的初始化过程
2)消息映射
3)运行时类型识别(RTTI)
4)动态创建
5)永久保存
6)消息传递
一、MFC的初始化过程:
MFC的架构组成:
1.要有CWinApp的派生类
2.必须在全局区定义一个派生类的对象
3.在CWinApp派生类中必须对InitInstance()函数进行重写
【在MFC软件工程中,以APP类中的InitInstance()函数作为主函数,连接MFC的平台使用static Library静态链接库】
#include "stdafx.h" class CMyApp:public CWinApp { virtual BOOL InitInstance() { AfxMessageBox("xx"); return TRUE; } }; CMyApp theApp; /* int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // TODO: Place code here. MessageBox(NULL,"Win32测试MFC程序","测试",0); return 0; } */
消息映射机制:
1.必须使用类向导建立一个窗口类(CWin)的派生类
2.必须建立派生类的对象,来接受客户界面返回的消息
//在窗口派生类中,每个消息都与一个成员函数相对应
3.消息映射函数,必须通过类向导(ClassWizard)建立
Win32的消息处理机制:
#include "stdafx.h" BOOL CALLBACK DialogProc( HWND hwndDlg, // handle to dialog box UINT uMsg, // message WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ) { if(uMsg==WM_COMMAND) { if(LOWORD(wParam)==IDCANCEL) { EndDialog(hwndDlg,IDCANCEL); return TRUE; } } if(uMsg==WM_LBUTTONDOWN) { int x=LOWORD(lParam); int y=HIWORD(lParam); char ch[22]; sprintf(ch,"(%3d,%3d)",x,y); //MessageBox(NULL,ch,"",MB_OK); SetWindowText(hwndDlg,ch); } if(uMsg==WM_MOUSEMOVE) { int x=LOWORD(lParam); int y=HIWORD(lParam); char ch[22]; sprintf(ch,"(%3d,%3d)",x,y); //MessageBox(NULL,ch,"",MB_OK); SetWindowText(hwndDlg,ch); } return FALSE; } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // TODO: Place code here. //MessageBox(NULL,"Win32测试MFC程序","测试",0); DialogBox(hInstance,(LPCSTR)IDD_DIALOG1,NULL,DialogProc); return 0; }
MFC的消息处理机制
//theApp: #include "stdafx.h" #include"MainDlg.h" class MyApp:public CWinApp { virtual BOOL InitInstance( ) { //AfxMessageBox("cc",NULL,MB_OK); //CDialog *dlg=new CDialog(IDD_DIALOG1); //dlg->DoModal(); MainDlg *dlg=new MainDlg(); dlg->DoModal(); return TRUE; } }; MyApp theApp;
//MainDlg派生类 #include "stdafx.h" #include "MainDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // MainDlg dialog MainDlg::MainDlg(CWnd* pParent /*=NULL*/) : CDialog(MainDlg::IDD, pParent) { //{{AFX_DATA_INIT(MainDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT } void MainDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(MainDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(MainDlg, CDialog) //{{AFX_MSG_MAP(MainDlg) ON_WM_LBUTTONDOWN() ON_WM_MOUSEMOVE() ON_WM_RBUTTONDOWN() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // MainDlg message handlers BOOL MainDlg::OnInitDialog() { CDialog::OnInitDialog(); //Title CDialog::SetWindowText("测试"); //Icon HICON hicon=LoadIcon(AfxGetInstanceHandle(),(LPCSTR)IDI_ICON1); CDialog::SetIcon(hicon,TRUE); return TRUE; } void MainDlg::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CDialog::OnLButtonDown(nFlags, point); CString str; str.Format("(%d,%d)",point.x,point.y); if(MK_SHIFT&nFlags) str+="按下了Shift键"; if(MK_CONTROL&nFlags) str+="按下了Ctrl键"; if(MK_LBUTTON&nFlags) str+="按下了鼠标左键"; if(MK_MBUTTON&nFlags) str+="按下了鼠标中键"; if(MK_RBUTTON&nFlags) str+="按下了鼠标右键"; AfxMessageBox(str); //CDialog::GetDlgItemText(IDC_TEXT_COORD,str); } void MainDlg::OnRButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CDialog::OnRButtonDown(nFlags, point); CDialog::OnLButtonDown(nFlags, point); CString str; str.Format("(%d,%d)",point.x,point.y); if(MK_SHIFT&nFlags) str+="按下了Shift键"; if(MK_CONTROL&nFlags) str+="按下了Ctrl键"; if(MK_LBUTTON&nFlags) str+="按下了鼠标左键"; if(MK_MBUTTON&nFlags) str+="按下了鼠标中键"; if(MK_RBUTTON&nFlags) str+="按下了鼠标右键"; AfxMessageBox(str); } void MainDlg::OnMouseMove(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CDialog::OnMouseMove(nFlags, point); CString str; str.Format("(%d,%d)",point.x,point.y); GetDlgItemText(IDC_TEXT_COORD,str); }