Visual C++ Windows Programming 第三章 MFC簡介
大綱 MFC 的基本架構 MFC 的使用方式 資源的設計與使用 訊息處理 Document / View 的觀念
MFC 的基本架構 CObject 應用程式類別 容器類別 繪圖類別 視窗類別 資料庫類別 網際網路服務類別
MFC 的使用方式 標頭檔 應用程式物件與視窗框架物件 #include <afxwin.h> CWinApp CFrameWnd
MFC 的使用方式 (續) #include <afxwin.h> class CFirstApp : public CWinApp { public: BOOL InitInstance() { CFrameWnd *Frame = new CFrameWnd(); m_pMainWnd = Frame; Frame->Create(NULL, "Hello, MFC!!"); Frame->ShowWindow(SW_SHOW); return true; } } ; CFirstApp a_app;
資源的設計與使用 Resource Script Accelerator Bitmap Cursor Dialog Icon Menu String Table Toolbar Version
資源的設計與使用 (續) Menu Item 屬性 ID: 按下 menu item 傳回的 command 代號 Caption: menu item 的文字 ("&" : 快速鍵) Seperator: 分隔線 Checked: menu item 是否被選取 Pop-up: menu "folder" or menu item Grayed: 是否為 enabled Inactive: 是否正為游標選取中
資源的設計與使用 (續) #include <afxwin.h> #include "resource.h" class CFirstFrame : public CFrameWnd { private: CMenu *FMenu; public: CFirstFrame() { Create(NULL, "Menu Resource Demo"); FMenu = new CMenu; FMenu->LoadMenu(IDR_MENU1); SetMenu(FMenu); } } ; class CFirstApp : public CWinApp { BOOL InitInstance() { CFrameWnd *Frame = new CFirstFrame; m_pMainWnd = Frame; Frame->ShowWindow(SW_SHOW); return true; CFirstApp a_app;
訊息處理 DECLARE_MESSAGE_MAP() BEGIN_MESSAGE_MAP(class, base) END_MESSAGE_MAP()
訊息處理 (續) class CFirstFrame : public CFreamWnd { ...; DECLARE_MESSAGE_MAP() } ; ... BEGIN_MESSAGE_MAP(CFirstFrame, CFrameWnd) ON_COMMAND(ID_EXIT, OnExit) ON_WM_LBUTTONDOWN() ON_WM_MOUSEMOVE() ON_WM_LBUTTONUP() END_MESSAGE_MAP()
訊息處理 (續) #include <afxwin.h> #include "resource.h" class CFirstFrame : public CFrameWnd { private: CMenu *FMenu; public: CFirstFrame() { Create(NULL, "Message Processing Demo"); FMenu = new CMenu; FMenu->LoadMenu(IDR_MENU1); SetMenu(FMenu); } ~CFirstFrame() { delete FMenu; } afx_msg void OnExit() { MessageBox("It's time to stop.", "Stop Window", MB_ICONSTOP); DestroyWindow(); afx_msg void OnLButtonDown(UINT nFlags, CPoint point) { SetCapture(); } afx_msg void OnMouseMove(UINT nFlags, CPoint point) { if(this == GetCapture()) { CClientDC aDC(this); aDC.Ellipse(point.x - 3, point.y - 3, point.x + 3, point.y + 3);
訊息處理 (續) afx_msg void OnLButtonUp(UINT nFlags, CPoint point) { ReleaseCapture(); } DECLARE_MESSAGE_MAP() } ; BEGIN_MESSAGE_MAP(CFirstFrame, CFrameWnd) ON_COMMAND(ID_Exit, OnExit) ON_WM_LBUTTONDOWN() ON_WM_MOUSEMOVE() ON_WM_LBUTTONUP() END_MESSAGE_MAP() class CFirstApp : public CWinApp { public: BOOL InitInstance() { CFrameWnd *Frame = new CFirstFrame; m_pMainWnd = Frame; Frame->ShowWindow(SW_SHOW); return true; } CFirstApp a_app;
Document / View #include <afxwin.h> #include "resource.h" #include <afxtempl.h> class MyDocument : public CDocument { public: CArray <CPoint, CPoint &> pArray; MyDocument() { SetTitle("Document / View Demo"); } void AddPoint(CPoint p) { pArray.Add(p); } CPoint GetPoint(int i) { return pArray[i]; } int GetSize() { return pArray.GetSize(); } DECLARE_DYNCREATE(MyDocument) DECLARE_MESSAGE_MAP() } ; IMPLEMENT_DYNCREATE(MyDocument, CDocument) BEGIN_MESSAGE_MAP(MyDocument, CDocument) END_MESSAGE_MAP()
class MyView : public CView { void OnDraw(CDC *aDC) { MyDocument *doc = (MyDocument *) GetDocument(); int num = doc->GetSize(); int i; for(i = 0; i < num; i++) { CPoint point = doc->GetPoint(i); aDC->Ellipse(point.x - 3, point.y - 3, point.x + 3, point.y + 3); } afx_msg void OnLButtonDown(UINT, CPoint point) { SetCapture(); } afx_msg void OnMouseMove(UINT, CPoint point) { if (this == GetCapture()) { CClientDC aDC(this); aDC.Ellipse(point.x - 3, point.y - 3, point.x + 3, point.y + 3); doc->AddPoint(point); afx_msg void OnLButtonUp(UINT, CPoint point) { ReleaseCapture(); } DECLARE_DYNCREATE(MyView) DECLARE_MESSAGE_MAP() } ;
IMPLEMENT_DYNCREATE(MyView, CView) BEGIN_MESSAGE_MAP(MyView, CView) ON_WM_LBUTTONDOWN() ON_WM_MOUSEMOVE() ON_WM_LBUTTONUP() END_MESSAGE_MAP() class MyFrame : public CFrameWnd { public: afx_msg void OnExit() { MessageBox("It's time to stop.", "Stop Window", MB_ICONSTOP); DestroyWindow(); } DECLARE_DYNCREATE(MyFrame) DECLARE_MESSAGE_MAP() } ; IMPLEMENT_DYNCREATE(MyFrame, CFrameWnd) BEGIN_MESSAGE_MAP(MyFrame, CFrameWnd) ON_COMMAND(ID_Exit, OnExit)
class MyApp : public CWinApp { BOOL InitInstance() { CDocument *doc; CSingleDocTemplate *DocTemplate; DocTemplate = new CSingleDocTemplate(IDR_MENU1, RUNTIME_CLASS(MyDocument), RUNTIME_CLASS(MyFrame), RUNTIME_CLASS(MyView)); AddDocTemplate(DocTemplate); doc = DocTemplate->CreateNewDocument(); m_pMainWnd = DocTemplate->CreateNewFrame(doc, NULL); DocTemplate->InitialUpdateFrame((CFrameWnd *)m_pMainWnd, doc); m_pMainWnd->ShowWindow(SW_SHOW); return true; } } ; MyApp a_app;