MFCExample include afxwin h afxwin class My App

  • Slides: 40
Download presentation

MFC的Example #include <afxwin. h> //載入afxwin標頭檔 class My. App : public CWin. App //繼承CWin. App

MFC的Example #include <afxwin. h> //載入afxwin標頭檔 class My. App : public CWin. App //繼承CWin. App { public: BOOL Init. Instance() //程式進入點 { CFrame. Wnd *Frame = new CFrame. Wnd(); //建立CFrame. Wnd物件(產生) m_p. Main. Wnd = Frame; //將m_p. Main. Wnd設定為Frame->Create(NULL, “Hello MFC”); Frame->Show. Window(SW_SHOW); return true; } }; My. App a_app; //建立應用程式物件 //建立視窗(建立)

程式說明 BOOL Init. Instance() //程式進入點 { CFrame. Wnd *Frame = new CFrame. Wnd(); //建立CFrame.

程式說明 BOOL Init. Instance() //程式進入點 { CFrame. Wnd *Frame = new CFrame. Wnd(); //建立CFrame. Wnd物 件 m_p. Main. Wnd = Frame; //將m_p. Main. Wnd設定為Frame->Create(NULL, "Hello MFC"); //建立視窗 Frame->Show. Window(SW_SHOW); return true; } 建立應用程式物件 My. App a_app; //建立應用程式物件

My. Frame程式範例 #include <afxwin. h> #include "My. Frame. h" //由資源編輯器所產生的標頭檔 class My. Frame :

My. Frame程式範例 #include <afxwin. h> #include "My. Frame. h" //由資源編輯器所產生的標頭檔 class My. Frame : public CFrame. Wnd //繼承CFrame. Wnd類別 { private: CMenu *FMenu; public: My. Frame() { Create(NULL, "Hello MFC"); //建立視窗 FMenu = new CMenu; //產生選單 FMenu->Load. Menu(IDR_MENU 1); //載入選單 Set. Menu(FMenu); //設定視窗所使用的選單 } };

My. Frame程式範例 class My. App : public CWin. App { public: BOOL Init. Instance()

My. Frame程式範例 class My. App : public CWin. App { public: BOOL Init. Instance() { CFrame. Wnd *Frame = new My. Frame; //產生視窗 m_p. Main. Wnd = Frame; //將視窗物件設定給應用程式 Frame->Show. Window(SW_SHOW); return true; } }; My. App a_app; //建立應用程式物件 //顯示視窗

程式說明 // Used by My. Frame. rc #define IDR_MENU 1 #define ID_Exit 101 40001

程式說明 // Used by My. Frame. rc #define IDR_MENU 1 #define ID_Exit 101 40001 // Next default values for new objects // #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NEXT_RESOURCE_VALUE 102 #define _APS_NEXT_COMMAND_VALUE 40002 #define _APS_NEXT_CONTROL_VALUE 1000 #define _APS_NEXT_SYMED_VALUE 101 #endif

訊息映射表的建立 class My. Frame : public CFrame. Wnd { … DECLARE_MESSAGE_MAP()//宣告訊息映表 }; BEGIN_MESSAGE_MAP(My. Frame,

訊息映射表的建立 class My. Frame : public CFrame. Wnd { … DECLARE_MESSAGE_MAP()//宣告訊息映表 }; BEGIN_MESSAGE_MAP(My. Frame, CFrame. Wnd) //建立My. Frame類別的訊息映射表 函數 ON_COMMAND(ID_Exit 1, On. Exit) 名稱 ON_WM_LBUTTONDOWN() ON_WM_MOUSEMOVE() 訊息 ON_WM_LBUTTONUP() 代號 END_MESSAGE_MAP()

Message程式範例 #include <afxwin. h> #include "Message. h" //載入資 源檔所使用之標頭檔 class My. Frame : public

Message程式範例 #include <afxwin. h> #include "Message. h" //載入資 源檔所使用之標頭檔 class My. Frame : public CFrame. Wnd { private: CMenu *FMenu; public: My. Frame() //建構子 { Create(NULL, "Hello MFC"); FMenu = new CMenu; FMenu>Load. Menu(IDR_MENU 1) ; Set. Menu(FMenu); } ~My. Frame(){ delete FMenu; } //解構子 afx_msg void On. Exit() //ID_EXIT 1的回應函數 { Message. Box("Exit 1"); Destroy. Window(); } afx_msg void On. LButton. Down(UINT n. Flags, CPoint point) { Set. Capture(); } //當滑鼠 左鍵按下後的回應函數,取得滑 鼠訊息接收權 afx_msg void On. LButton. Up(UINT n. Flags, CPoint point) { Release. Capture(); } //當滑鼠 左鍵放開後的回應函數,釋放滑 鼠訊息接收權

Message程式範例 afx_msg void On. Mouse. Move(UINT n. Flags, CPoint point) { //當滑鼠移動時的回應函數 if (this

Message程式範例 afx_msg void On. Mouse. Move(UINT n. Flags, CPoint point) { //當滑鼠移動時的回應函數 if (this == Get. Capture())//判 斷滑鼠游標是否在視窗之上 {CClient. DC a. DC(this); //建立一個畫布 a. DC. Set. Pixel(point, RGB(255, 0 , 0)); //利用Set. Pixel在 畫布上點出紅點} } afx_msg void On. LButton. Up(UINT n. Flags, CPoint point) { Release. Capture(); } //當滑鼠 左鍵放開後的回應函數,釋放滑 鼠訊息接收權 DECLARE_MESSAGE_MAP() //宣告訊息映射表}; class My. App : public CWin. App //應用程式類別 { public: BOOL Init. Instance() //程式進 入點 { CFrame. Wnd *Frame = new My. Frame; m_p. Main. Wnd = Frame; Frame>Show. Window(SW_SHOW ); //顯示視窗 return true; } } a_app; //宣告應用程式物件

Document/View程式範例 #include <afxwin. h> #include "Doc_View. h" class My. Document : public CDocument {DECLARE_DYNCREATE(My.

Document/View程式範例 #include <afxwin. h> #include "Doc_View. h" class My. Document : public CDocument {DECLARE_DYNCREATE(My. Docu ment) //宣告run-time類別}; IMPLEMENT_DYNCREATE(My. Doc ument, CDocument) //宣告My. Document為run-time類別 class My. View : public CView {public: void On. Draw(CDC * a. DC) //必須過載的虛擬函數 {} DECLARE_DYNCREATE(My. View)/ /宣告run-time類別}; IMPLEMENT_DYNCREATE(My. Vie w, CView) //宣告My. View為run-time類 別 class My. Frame : public CFrame. Wnd { DECLARE_DYNCREATE(My. Fra me) //宣告run-time類別 }; IMPLEMENT_DYNCREATE(My. Fra me, CFrame. Wnd) //宣告My. Frame為run-time 類別

Document/View程式範例 class My. App : public CWin. App {public: BOOL Init. Instance() {CDocument *doc;

Document/View程式範例 class My. App : public CWin. App {public: BOOL Init. Instance() {CDocument *doc; //宣告指 向文件的指標 CSingle. Doc. Template* Doc. Template; //宣告指向單文件 樣版物件的指標 Doc. Template = new CSingle. Doc. Template( //建立具 有單文件樣版物件 IDR_MENU 1, //用於單文件框架 的資源代號 RUNTIME_CLASS(My. Document), //單文件視窗的Document RUNTIME_CLASS(My. Frame), // 單文件視窗的視窗框架 RUNTIME_CLASS(My. Vie w)); //單文件視窗的View Add. Doc. Template(Doc. Template); // 將單文件樣版物件設定給My. App doc = Doc. Template>Create. New. Document(); //建立新 的文件 m_p. Main. Wnd = Doc. Template>Create. New. Frame( doc, NULL ); //建立一個視窗框架 Doc. Template->Initial. Update. Frame ( (CFrame. Wnd*)m_p. Main. Wnd, doc ); //起始化視窗框架中的View m_p. Main. Wnd->Show. Window ( SW_SHOW); //顯示視窗 return true; }} a_app; //建立應用程 式物件

Document/View架構的應用 視窗的重繪 #include <afxwin. h> #include "repaint. h" #include <afxtempl. h> //定義樣 版類別的標頭檔 class

Document/View架構的應用 視窗的重繪 #include <afxwin. h> #include "repaint. h" #include <afxtempl. h> //定義樣 版類別的標頭檔 class My. Document : public CDocument { public: CArray<CPoint, CPoint &> p. Array; //容納滑鼠軌跡點 的Array容器 void Add. Point(CPoint p) //將軌跡 點加到容器內 { p. Array. Add(p); } CPoint Get. Point(int i) //將軌跡 點從容器中取出 { return p. Array[i]; } int Get. Size() { return p. Array. Get. Size(); } //取得容器的大小 DECLARE_DYNCREATE(My. Do cument) //宣告為run-time 類別 DECLARE_MESSAGE_MAP() //宣告訊息映射表 }; IMPLEMENT_DYNCREATE(My. Doc ument, CDocument) //建立run-time類別 BEGIN_MESSAGE_MAP(My. Docum ent, CDocument) END_MESSAGE_MAP() //建立訊息映射表

Document/View架構的應用 視窗的重繪 class My. View : public CView {public: void On. Draw(CDC * a.

Document/View架構的應用 視窗的重繪 class My. View : public CView {public: void On. Draw(CDC * a. DC) //過載 On. Draw虛擬函數 {My. Document *doc = (My. Document *)Get. Document(); //取得目前Document物件的指標 int num = doc->Get. Size(); //取得目前儲存的軌跡點點數 int i; for(i = 0; i < num; ++i) //將 Document中儲存的軌跡點重繪到 視窗上 {CPoint point = doc->Get. Point(i); a. DC->Set. Pixel(point, RGB(255, 0 , 0)); }} afx_msg void On. LButton. Down(UINT, CPoint point) { Set. Capture(); } //取得滑 鼠訊息的接收權 afx_msg void On. Mouse. Move(UINT, CPoint point) {if (this == Get. Capture()) {CClient. DC a. DC(this); //建立畫布 a. DC. Set. Pixel(point, RGB(255, 0 , 0)); //將點畫在畫布上 My. Document *doc = (My. Document *)Get. Document(); // 取得目前Document物件的指標 doc->Add. Point(point); //將軌跡 點加入Document物件中

Document/View架構的應用 視窗的重繪 afx_msg void On. LButton. Up(UINT, CPoint point){ Release. Capture(); } //釋放滑鼠訊息的接收權 DECLARE_DYNCREATE(My.

Document/View架構的應用 視窗的重繪 afx_msg void On. LButton. Up(UINT, CPoint point){ Release. Capture(); } //釋放滑鼠訊息的接收權 DECLARE_DYNCREATE(My. Vie w) //宣告為run-time類別 DECLARE_MESSAGE_MAP() //宣告訊息映射表}; IMPLEMENT_DYNCREATE(My. Vie w, CView)//建立run-time類別 BEGIN_MESSAGE_MAP(My. View, CView) ON_WM_LBUTTONDOWN() ON_WM_MOUSEMOVE() ON_WM_LBUTTONUP() END_MESSAGE_MAP() //建立訊息映射表 class My. Frame : public CFrame. Wnd { public: DECLARE_DYNCREATE(My. Fra me) //宣告為run-time類別 DECLARE_MESSAGE_MAP() //宣告訊息映射表 }; IMPLEMENT_DYNCREATE(My. Fra me, CFrame. Wnd) //建立run-time類別 BEGIN_MESSAGE_MAP(My. Frame, CFrame. Wnd) END_MESSAGE_MAP() //建立訊息映射表

Document/View架構的應用 視窗的重繪 class My. App : public CWin. App {public: BOOL Init. Instance() {CDocument

Document/View架構的應用 視窗的重繪 class My. App : public CWin. App {public: BOOL Init. Instance() {CDocument *doc; CSingle. Doc. Template* Doc. Template; Doc. Template = new CSingle. Doc. Template( //單文件 樣版類別 IDR_MENU 1, RUNTIME_CLASS(My. Document ), RUNTIME_CLASS(My. Frame), RUNTIME_CLASS(My. View)); Add. Doc. Template(Doc. Template); // 將文件樣版物件加入應用程式 doc = Doc. Template>Create. New. Document(); //建立新 文件 m_p. Main. Wnd = Doc. Template>Create. New. Frame( doc, NULL ); //建立新的視窗框架 Doc. Template->Initial. Update. Frame ( (CFrame. Wnd*)m_p. Main. Wnd, doc ); //起始化View物件 m_p. Main. Wnd->Show. Window ( SW_SHOW ); //顯示視窗 return true; } } a_app;