Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual C++ Windows Programming

Similar presentations


Presentation on theme: "Visual C++ Windows Programming"— Presentation transcript:

1 Visual C++ Windows Programming
第四章 資源物件的使用

2

3

4

5

6

7

8

9 本章的準備 #include <afxwin.h> class CMyFrame : public CFrameWnd {
private: public: CMyFrame() { Create(NULL, Resource Usage Demo"); } ~CMyFrame() { } afx_msg void OnLButtonDown(UINT nFlags, CPoint point) { SetCapture(); } afx_msg void OnMouseMove(UINT nFlags, CPoint point) { if(this == GetCapture()) { CClientDC aDC(this); aDC.SetPixel(point, RGB(255, 0, 0));

10 afx_msg void OnLButtonUp(UINT nFlags, CPoint point) { ReleaseCapture(); }
DECLARE_MESSAGE_MAP() } ; BEGIN_MESSAGE_MAP(CMyFrame, CFrameWnd) ON_WM_LBUTTONDOWN() ON_WM_MOUSEMOVE() ON_WM_LBUTTONUP() END_MESSAGE_MAP() class CMyApp : public CWinApp { public: BOOL InitInstance() { CFrameWnd *Frame = new CMyFrame; m_pMainWnd = Frame; Frame->ShowWindow(SW_SHOW); return true; } CMyApp a_app;

11

12 大綱 資源編輯器的操作 選單 (menu) 資源 熱鍵 (acc. table) 資源 工具列 (toolbar) 資源
UPDATE_COMMAND_UI 的觀念 狀態列 (status bar; 不是資源!) 與 字串表 (string table) 資源 點陣圖 (bitmap) 資源、圖示 (icon) 資源與游標 (cursor) 資源

13 資源編輯器的操作 新增資源物件 刪除資源物件 更改資源物件識別子與屬性
當工作區開啟 "Resource View" 時,在資源檔的圖示上按滑鼠右鍵,選取 Insert 選項,在 Insert Resource 對話盒中選取欲新增的資源物件,然後按下 New 按鈕。 刪除資源物件 在 Resource View 工作區點選欲刪除的資源,然後按下鍵盤的 Delete 鍵。 更改資源物件識別子與屬性 在 Resource View 工作區右鍵點選欲更改的物件,選取 Properties 選項,在對話盒中設定識別子與屬性。

14 選單資源 選單的組成 選單是視窗介面中,提供使用者選取指令的主要工具。一個應用程式的選單,通常由好幾個子選單所組成。在每一個子選單裡,將以分門別類的方式整理應用程式可提供執行的命令,方便使用者使用。 在應用程式裡,每一個選單或是選項都有一個 ID 代表,這個 ID 稱為該選單/選項的識別子,除了可以代表該選項之外,更進一步用於建立訊息映射項目,以連結選單選項所發出的訊息與欲觸發的訊息處理函數。

15

16

17

18

19

20

21

22

23

24

25 IDR_SMENU IDR_MAINMENU File Dynamic Switch IDM_SWITCHITEM Exit IDM_EXIT Create Item IDM_CREATEITEM IDR_INSMENU IDR_POPMENU Color Popup Red IDM_RED Item IDM_ITEM1 Green IDM_GREEN Item IDM_ITEM2 Blue IDM_BLUE

26

27

28

29

30

31 IDM_DELETEITEM IDM_NEWITEM IDM_INSERTITEM

32 #include <afxwin.h>
#include "resource.h" class CMyFrame : public CFrameWnd { private: CMenu MainMenu, *SysMenu, *PopMenu, *SubMenu; COLORREF color; public: CMyFrame() { Create(NULL, "Menu Resource Demo", WS_OVERLAPPEDWINDOW | WS_VISIBLE, rectDefault, NULL, MAKEINTRESOURCE(IDR_SMENU)); SysMenu = GetSystemMenu(FALSE); color = RGB(255, 0, 0); } ~CMyFrame() { } afx_msg void OnSwitchItem() { CMenu InsMenu; InsMenu.LoadMenu(IDR_INSMENU); MainMenu.LoadMenu(IDR_MAINMENU); MainMenu.AppendMenu(MF_POPUP, (UINT) InsMenu.m_hMenu, "&Color Setting"); SubMenu = MainMenu.GetSubMenu(1); SubMenu->AppendMenu(MF_SEPARATOR); SubMenu->InsertMenu(IDM_CREATEITEM, MF_BYCOMMAND | MF_STRING, IDM_INSERTITEM, "&Insert Item"); SubMenu->InsertMenu(IDM_CREATEITEM, MF_BYCOMMAND | MF_POPUP, (UINT) InsMenu.m_hMenu, "Insert &submenu"); SetCheck(); InsMenu.Detach(); SetMenu(&MainMenu);

33 afx_msg void OnCreateItem() {
MainMenu.ModifyMenu(IDM_CREATEITEM, MF_BYCOMMAND, IDM_DELETEITEM, "&Delete Item"); SubMenu = MainMenu.GetSubMenu(1); SubMenu->AppendMenu(MF_STRING, IDM_NEWITEM, "New Item"); } afx_msg void OnDeleteItem() { MainMenu.ModifyMenu(IDM_DELETEITEM, MF_BYCOMMAND, IDM_CREATEITEM, "&Create Item"); SubMenu->DeleteMenu(IDM_NEWITEM, MF_BYCOMMAND); afx_msg void OnContextMenu(CWnd *pWnd, CPoint point) { CMenu menu; menu.LoadMenu(IDR_POPMENU); PopMenu = menu.GetSubMenu(0); PopMenu->TrackPopupMenu(TPM_CENTERALIGN | TPM_RIGHTBUTTON, point.x, point.y, this); menu.Detach(); afx_msg void OnRed() { SetUnCheck(); color = RGB(255, 0, 0); SetCheck(); afx_msg void OnGreen() { color = RGB(0, 255, 0);

34 afx_msg void OnBlue() {
SetUnCheck(); color = RGB(0, 0, 255); SetCheck(); } void SetCheck() { switch(color) { case RGB(255, 0, 0): MainMenu.CheckMenuItem(IDM_RED, MF_BYCOMMAND | MF_CHECKED); break; case RGB(0, 255, 0): MainMenu.CheckMenuItem(IDM_GREEN, MF_BYCOMMAND | MF_CHECKED); case RGB(0, 0, 255): MainMenu.CheckMenuItem(IDM_BLUE, MF_BYCOMMAND | MF_CHECKED); void SetUnCheck() { MainMenu.CheckMenuItem(IDM_RED, MF_BYCOMMAND | MF_UNCHECKED); MainMenu.CheckMenuItem(IDM_GREEN, MF_BYCOMMAND | MF_UNCHECKED); MainMenu.CheckMenuItem(IDM_BLUE, MF_BYCOMMAND | MF_UNCHECKED);

35 afx_msg void OnLButtonDown(UINT nFlags, CPoint point) { SetCapture(); }
afx_msg void OnMouseMove(UINT nFlags, CPoint point) { if(this == GetCapture()) { CClientDC aDC(this); aDC.SetPixel(point, color); } afx_msg void OnLButtonUp(UINT nFlags, CPoint point) { ReleaseCapture(); } DECLARE_MESSAGE_MAP() } ; BEGIN_MESSAGE_MAP(CMyFrame, CFrameWnd) ON_COMMAND(IDM_SWITCHITEM, OnSwitchItem) ON_COMMAND(IDM_CREATEITEM, OnCreateItem) ON_COMMAND(IDM_DELETEITEM, OnDeleteItem) ON_COMMAND(IDM_RED, OnRed) ON_COMMAND(IDM_GREEN, OnGreen) ON_COMMAND(IDM_BLUE, OnBlue) ON_WM_CONTEXTMENU() ON_WM_LBUTTONDOWN() ON_WM_MOUSEMOVE() ON_WM_LBUTTONUP() END_MESSAGE_MAP() class CMyApp : public CWinApp { public: BOOL InitInstance() { CFrameWnd *Frame = new CMyFrame; m_pMainWnd = Frame; Frame->ShowWindow(SW_SHOW); return true; CMyApp a_app;

36 加速鍵資源 加速鍵的使用 利用選單控制應用程式是一個相當方便且視覺化的方式,但在速度上卻較為緩慢。因此大多數的應用程式,都會設定加速鍵提供使用者另一個較為快速的程式操作途徑。 載入加速鍵列表需要呼叫的函式為 LoadAccelTable()。

37

38

39

40

41

42

43

44

45 Compare the Difference (work well)
class CMyFrame : public CFrameWnd { private: ...; public: afx_msg void OnSwitchItem() { CMenu InsMenu; InsMenu.LoadMenu(IDR_INSMENU); MainMenu.LoadMenu(IDR_MAINMENU); MainMenu.AppendMenu(MF_POPUP, (UINT) InsMenu.m_hMenu, "&Color Setting"); SubMenu = MainMenu.GetSubMenu(1); SubMenu->AppendMenu(MF_SEPARATOR); SubMenu->InsertMenu(IDM_CREATEITEM, MF_BYCOMMAND | MF_STRING, IDM_INSERTITEM, "&Insert Item"); SubMenu->InsertMenu(IDM_CREATEITEM, MF_BYCOMMAND | MF_POPUP, (UINT) InsMenu.m_hMenu, "Insert &submenu"); LoadAccelTable(MAKEINTRESOURCE(IDR_ACCEL)); SetCheck(); InsMenu.Detach(); SetMenu(&MainMenu); } DECLARE_MESSAGE_MAP() } ;

46 Compare the Difference (not work)
class CMyFrame : public CFrameWnd { private: ...; public: CMyFrame() { Create(NULL, "Message Processing Demo", WS_OVERLAPPEDWINDOW | WS_VISIBLE, rectDefault, NULL, MAKEINTRESOURCE(IDR_SMENU)); SysMenu = GetSystemMenu(FALSE); LoadAccelTable(MAKEINTRESOURCE(IDR_ACCEL)); color = RGB(255, 0, 0); } ~CMyFrame() { } DECLARE_MESSAGE_MAP() } ;

47

48 UPDATE_COMMAND_UI UPDATE_COMMAND_UI 的觀念
MFC 提供了 UPDATE_COMMAND_UI 訊息專司維護視窗元件的狀態。

49 UPDATE_COMMAND_UI UPDATE_COMMAND_UI 的原理
UPDATE_COMMAND_UI 訊息是一個及時檢查 UI 元件狀態的機制。 當我們按下選單,在選單尚未顯示前,該選單所有選項發出 UPDATE_COMMAND_UI 訊息,程式收到這個訊息時,同時也會收到一個 CCmdUI 物件指標,該物件指標將指向發出 UPDATE_COMMAND_UI 訊息的 UI 物件。 藉由這些發出的訊息,呼叫回應的函數,檢查該 UI 元件應該顯示的狀態。

50 #include <afxwin.h>
#include "resource.h" class CMyFrame : public CFrameWnd { private: CMenu MainMenu, *SysMenu, *PopMenu, *SubMenu; COLORREF color; public: CMyFrame() { Create(NULL, "Menu Resource Demo", WS_OVERLAPPEDWINDOW | WS_VISIBLE, rectDefault, NULL, MAKEINTRESOURCE(IDR_SMENU)); SysMenu = GetSystemMenu(FALSE); LoadAccelTable(MAKEINTRESOURCE(IDR_ACCEL)); color = RGB(255, 0, 0); } ~CMyFrame() { } afx_msg void OnSwitchItem() { CMenu InsMenu; InsMenu.LoadMenu(IDR_INSMENU); MainMenu.LoadMenu(IDR_MAINMENU); MainMenu.AppendMenu(MF_POPUP, (UINT) InsMenu.m_hMenu, "&Color Setting"); SubMenu = MainMenu.GetSubMenu(1); SubMenu->AppendMenu(MF_SEPARATOR); SubMenu->InsertMenu(IDM_CREATEITEM, MF_BYCOMMAND | MF_STRING, IDM_INSERTITEM, "&Insert Item"); SubMenu->InsertMenu(IDM_CREATEITEM, MF_BYCOMMAND | MF_POPUP, (UINT) InsMenu.m_hMenu, "Insert &submenu"); //SetCheck(); InsMenu.Detach(); SetMenu(&MainMenu);

51 afx_msg void OnCreateItem() {
MainMenu.ModifyMenu(IDM_CREATEITEM, MF_BYCOMMAND, IDM_DELETEITEM, "&Delete Item"); SubMenu = MainMenu.GetSubMenu(1); SubMenu->AppendMenu(MF_STRING, IDM_NEWITEM, "New Item"); } afx_msg void OnDeleteItem() { MainMenu.ModifyMenu(IDM_DELETEITEM, MF_BYCOMMAND, IDM_CREATEITEM, "&Create Item"); SubMenu->DeleteMenu(IDM_NEWITEM, MF_BYCOMMAND); afx_msg void OnContextMenu(CWnd *pWnd, CPoint point) { CMenu menu; menu.LoadMenu(IDR_POPMENU); PopMenu = menu.GetSubMenu(0); PopMenu->TrackPopupMenu(TPM_CENTERALIGN | TPM_RIGHTBUTTON, point.x, point.y, this); menu.Detach(); afx_msg void OnRed() { //SetUnCheck(); color = RGB(255, 0, 0); //SetCheck(); afx_msg void OnGreen() { color = RGB(0, 255, 0);

52 afx_msg void OnBlue() {
//SetUnCheck(); color = RGB(0, 0, 255); //SetCheck(); } /* void SetCheck() { switch(color) { case RGB(255, 0, 0): MainMenu.CheckMenuItem(IDM_RED, MF_BYCOMMAND | MF_CHECKED); break; case RGB(0, 255, 0): MainMenu.CheckMenuItem(IDM_GREEN, MF_BYCOMMAND | MF_CHECKED); case RGB(0, 0, 255): MainMenu.CheckMenuItem(IDM_BLUE, MF_BYCOMMAND | MF_CHECKED); void SetUnCheck() { MainMenu.CheckMenuItem(IDM_RED, MF_BYCOMMAND | MF_UNCHECKED); MainMenu.CheckMenuItem(IDM_GREEN, MF_BYCOMMAND | MF_UNCHECKED); MainMenu.CheckMenuItem(IDM_BLUE, MF_BYCOMMAND | MF_UNCHECKED); } */

53 afx_msg void OnUpdateRed(CCmdUI *aCmdUI) {
aCmdUI->SetCheck(color == RGB(255, 0, 0)); } afx_msg void OnUpdateGreen(CCmdUI *aCmdUI) { aCmdUI->SetCheck(color == RGB(0, 255, 0)); afx_msg void OnUpdateBlue(CCmdUI *aCmdUI) { aCmdUI->SetCheck(color == RGB(0, 0, 255)); afx_msg void OnLButtonDown(UINT nFlags, CPoint point) { SetCapture(); } afx_msg void OnMouseMove(UINT nFlags, CPoint point) { if(this == GetCapture()) { CClientDC aDC(this); aDC.SetPixel(point, color); afx_msg void OnLButtonUp(UINT nFlags, CPoint point) { ReleaseCapture(); } DECLARE_MESSAGE_MAP() } ;

54 BEGIN_MESSAGE_MAP(CMyFrame, CFrameWnd)
ON_COMMAND(IDM_SWITCHITEM, OnSwitchItem) ON_COMMAND(IDM_CREATEITEM, OnCreateItem) ON_COMMAND(IDM_DELETEITEM, OnDeleteItem) ON_COMMAND(IDM_RED, OnRed) ON_COMMAND(IDM_GREEN, OnGreen) ON_COMMAND(IDM_BLUE, OnBlue) ON_UPDATE_COMMAND_UI(IDM_RED, OnUpdateRed) ON_UPDATE_COMMAND_UI(IDM_GREEN, OnUpdateGreen) ON_UPDATE_COMMAND_UI(IDM_BLUE, OnUpdateBlue) ON_WM_CONTEXTMENU() ON_WM_LBUTTONDOWN() ON_WM_MOUSEMOVE() ON_WM_LBUTTONUP() END_MESSAGE_MAP() class CMyApp : public CWinApp { public: BOOL InitInstance() { CFrameWnd *Frame = new CMyFrame; m_pMainWnd = Frame; Frame->ShowWindow(SW_SHOW); return true; } } ; CMyApp a_app;

55 工具列資源 工具列的使用 除了選單之外,在視窗程式裡最常見的視窗元件還有工具列與狀態列。工具列是另一種加速視窗介面操作的工具,對於一些操作視窗時常用的指令,我們都可以將之放在宗坐列中,方便使用者使用。

56

57

58

59

60

61

62

63

64 IDM_RED (Tool Button 'R')
IDM_GREEN (Tool Button 'G') IDM_BLUE (Tool Button 'B')

65

66 #include <afxwin.h>
#include <afxext.h> #include "resource.h" class CMyFrame : public CFrameWnd { private: CMenu MainMenu, *SysMenu, *PopMenu, *SubMenu; CToolBar toolbar; COLORREF color; public: CMyFrame() { Create(NULL, "Menu Resource Demo", WS_OVERLAPPEDWINDOW | WS_VISIBLE, rectDefault, NULL, MAKEINTRESOURCE(IDR_SMENU)); SysMenu = GetSystemMenu(FALSE); LoadAccelTable(MAKEINTRESOURCE(IDR_ACCEL)); color = RGB(255, 0, 0); } ~CMyFrame() { } afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct) { if(CFrameWnd::OnCreate(lpCreateStruct)) return -1; toolbar.Create(this); toolbar.LoadToolBar(IDR_TOOLBAR); toolbar.EnableDocking(CBRS_ALIGN_TOP); toolbar.SetBarStyle(toolbar.GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC); EnableDocking(CBRS_ALIGN_ANY); DockControlBar(&toolbar); return 0;

67 afx_msg void OnSwitchItem() {
CMenu InsMenu; InsMenu.LoadMenu(IDR_INSMENU); MainMenu.LoadMenu(IDR_MAINMENU); MainMenu.AppendMenu(MF_POPUP, (UINT) InsMenu.m_hMenu, "&Color Setting"); SubMenu = MainMenu.GetSubMenu(1); SubMenu->AppendMenu(MF_SEPARATOR); SubMenu->InsertMenu(IDM_CREATEITEM, MF_BYCOMMAND | MF_STRING, IDM_INSERTITEM, "&Insert Item"); SubMenu->InsertMenu(IDM_CREATEITEM, MF_BYCOMMAND | MF_POPUP, (UINT) InsMenu.m_hMenu, "Insert &submenu"); //SetCheck(); InsMenu.Detach(); SetMenu(&MainMenu); } afx_msg void OnCreateItem() { MainMenu.ModifyMenu(IDM_CREATEITEM, MF_BYCOMMAND, IDM_DELETEITEM, "&Delete Item"); SubMenu->AppendMenu(MF_STRING, IDM_NEWITEM, "New Item"); afx_msg void OnDeleteItem() { MainMenu.ModifyMenu(IDM_DELETEITEM, MF_BYCOMMAND, IDM_CREATEITEM, "&Create Item"); SubMenu->DeleteMenu(IDM_NEWITEM, MF_BYCOMMAND);

68 afx_msg void OnContextMenu(CWnd *pWnd, CPoint point) {
CMenu menu; menu.LoadMenu(IDR_POPMENU); PopMenu = menu.GetSubMenu(0); PopMenu->TrackPopupMenu(TPM_CENTERALIGN | TPM_RIGHTBUTTON, point.x, point.y, this); menu.Detach(); } afx_msg void OnRed() { color = RGB(255, 0, 0); afx_msg void OnGreen() { color = RGB(0, 255, 0); afx_msg void OnBlue() { color = RGB(0, 0, 255); afx_msg void OnUpdateRed(CCmdUI *aCmdUI) { aCmdUI->SetCheck(color == RGB(255, 0, 0)); afx_msg void OnUpdateGreen(CCmdUI *aCmdUI) { aCmdUI->SetCheck(color == RGB(0, 255, 0)); afx_msg void OnUpdateBlue(CCmdUI *aCmdUI) { aCmdUI->SetCheck(color == RGB(0, 0, 255));

69 afx_msg void OnLButtonDown(UINT nFlags, CPoint point) { SetCapture(); }
afx_msg void OnMouseMove(UINT nFlags, CPoint point) { if(this == GetCapture()) { CClientDC aDC(this); aDC.SetPixel(point, color); } afx_msg void OnLButtonUp(UINT nFlags, CPoint point) { ReleaseCapture(); } DECLARE_MESSAGE_MAP() } ; BEGIN_MESSAGE_MAP(CMyFrame, CFrameWnd) ON_WM_CREATE() ON_COMMAND(IDM_SWITCHITEM, OnSwitchItem) ON_COMMAND(IDM_CREATEITEM, OnCreateItem) ON_COMMAND(IDM_DELETEITEM, OnDeleteItem) ON_COMMAND(IDM_RED, OnRed) ON_COMMAND(IDM_GREEN, OnGreen) ON_COMMAND(IDM_BLUE, OnBlue) ON_UPDATE_COMMAND_UI(IDM_RED, OnUpdateRed) ON_UPDATE_COMMAND_UI(IDM_GREEN, OnUpdateGreen) ON_UPDATE_COMMAND_UI(IDM_BLUE, OnUpdateBlue) ON_WM_CONTEXTMENU() ON_WM_LBUTTONDOWN() ON_WM_MOUSEMOVE() ON_WM_LBUTTONUP() END_MESSAGE_MAP()

70 class CMyApp : public CWinApp {
BOOL InitInstance() { CFrameWnd *Frame = new CMyFrame; m_pMainWnd = Frame; Frame->ShowWindow(SW_SHOW); return true; } } ; CMyApp a_app;

71 狀態列與字串表 狀態列 狀態列用於顯示目前程式的執行狀態與說明,例如顯示目前文字輸入狀態為插入或是覆蓋。此外狀態列亦可以協助使用者操作視窗,例如許多視窗程式,當你欲選取某工作列或選單選項時,將會在狀態列中顯示說明文字。 字串表 字串表的主要用途是以識別子代替某些字串,例如對話盒中顯示的文字。以字串資源代替字串的好處,在於讓程式的更改變得容易。這對於程式的 "國際化" (internationalization; i18n) 與 "本土化" (localization; l10n) 相當有幫助。

72

73

74

75

76

77

78

79 #include <afxwin.h>
#include <afxext.h> #include "resource.h" class CMyFrame : public CFrameWnd { private: CMenu MainMenu, *SysMenu, *PopMenu, *SubMenu; CToolBar toolbar; CStatusBar statusbar; COLORREF color; public: CMyFrame() { Create(NULL, "Menu Resource Demo", WS_OVERLAPPEDWINDOW | WS_VISIBLE, rectDefault, NULL, MAKEINTRESOURCE(IDR_SMENU)); SysMenu = GetSystemMenu(FALSE); LoadAccelTable(MAKEINTRESOURCE(IDR_ACCEL)); color = RGB(255, 0, 0); } ~CMyFrame() { } afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct) { if(CFrameWnd::OnCreate(lpCreateStruct)) return -1; toolbar.Create(this); toolbar.LoadToolBar(IDR_TOOLBAR); toolbar.EnableDocking(CBRS_ALIGN_TOP); toolbar.SetBarStyle(toolbar.GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC); EnableDocking(CBRS_ALIGN_ANY); DockControlBar(&toolbar); static UINT indicators[] = { ID_SEPARATOR, IDS_RED } ; statusbar.Create(this); statusbar.SetIndicators(indicators, sizeof(indicators) / sizeof(UINT)); statusbar.SetPaneStyle(1, SBPS_POPOUT); return 0;

80 afx_msg void OnSwitchItem() {
CMenu InsMenu; InsMenu.LoadMenu(IDR_INSMENU); MainMenu.LoadMenu(IDR_MAINMENU); MainMenu.AppendMenu(MF_POPUP, (UINT) InsMenu.m_hMenu, "&Color Setting"); SubMenu = MainMenu.GetSubMenu(1); SubMenu->AppendMenu(MF_SEPARATOR); SubMenu->InsertMenu(IDM_CREATEITEM, MF_BYCOMMAND | MF_STRING, IDM_INSERTITEM, "&Insert Item"); SubMenu->InsertMenu(IDM_CREATEITEM, MF_BYCOMMAND | MF_POPUP, (UINT) InsMenu.m_hMenu, "Insert &submenu"); //SetCheck(); InsMenu.Detach(); SetMenu(&MainMenu); } afx_msg void OnCreateItem() { MainMenu.ModifyMenu(IDM_CREATEITEM, MF_BYCOMMAND, IDM_DELETEITEM, "&Delete Item"); SubMenu->AppendMenu(MF_STRING, IDM_NEWITEM, "New Item"); afx_msg void OnDeleteItem() { MainMenu.ModifyMenu(IDM_DELETEITEM, MF_BYCOMMAND, IDM_CREATEITEM, "&Create Item"); SubMenu->DeleteMenu(IDM_NEWITEM, MF_BYCOMMAND);

81 afx_msg void OnContextMenu(CWnd *pWnd, CPoint point) {
CMenu menu; menu.LoadMenu(IDR_POPMENU); PopMenu = menu.GetSubMenu(0); PopMenu->TrackPopupMenu(TPM_CENTERALIGN | TPM_RIGHTBUTTON, point.x, point.y, this); menu.Detach(); } afx_msg void OnRed() { CString resstr; color = RGB(255, 0, 0); resstr.LoadString(IDS_RED); statusbar.SetPaneText(1, resstr.GetBuffer(80)); afx_msg void OnGreen() { color = RGB(0, 255, 0); resstr.LoadString(IDS_GREEN); afx_msg void OnBlue() { color = RGB(0, 0, 255); resstr.LoadString(IDS_BLUE); afx_msg void OnUpdateRed(CCmdUI *aCmdUI) { aCmdUI->SetCheck(color == RGB(255, 0, 0));

82 afx_msg void OnUpdateGreen(CCmdUI *aCmdUI) {
aCmdUI->SetCheck(color == RGB(0, 255, 0)); } afx_msg void OnUpdateBlue(CCmdUI *aCmdUI) { aCmdUI->SetCheck(color == RGB(0, 0, 255)); afx_msg void OnLButtonDown(UINT nFlags, CPoint point) { SetCapture(); } afx_msg void OnMouseMove(UINT nFlags, CPoint point) { if(this == GetCapture()) { CClientDC aDC(this); aDC.SetPixel(point, color); afx_msg void OnLButtonUp(UINT nFlags, CPoint point) { ReleaseCapture(); } DECLARE_MESSAGE_MAP() } ;

83 BEGIN_MESSAGE_MAP(CMyFrame, CFrameWnd)
ON_WM_CREATE() ON_COMMAND(IDM_SWITCHITEM, OnSwitchItem) ON_COMMAND(IDM_CREATEITEM, OnCreateItem) ON_COMMAND(IDM_DELETEITEM, OnDeleteItem) ON_COMMAND(IDM_RED, OnRed) ON_COMMAND(IDM_GREEN, OnGreen) ON_COMMAND(IDM_BLUE, OnBlue) ON_UPDATE_COMMAND_UI(IDM_RED, OnUpdateRed) ON_UPDATE_COMMAND_UI(IDM_GREEN, OnUpdateGreen) ON_UPDATE_COMMAND_UI(IDM_BLUE, OnUpdateBlue) ON_WM_CONTEXTMENU() ON_WM_LBUTTONDOWN() ON_WM_MOUSEMOVE() ON_WM_LBUTTONUP() END_MESSAGE_MAP() class CMyApp : public CWinApp { public: BOOL InitInstance() { CFrameWnd *Frame = new CMyFrame; m_pMainWnd = Frame; Frame->ShowWindow(SW_SHOW); return true; } } ; CMyApp a_app;

84 點陣圖、圖示、游標 點陣圖的用途 圖示的用途 游標資源 為了建立更華麗的視窗介面,圖檔亦被使用於建立視窗介面。
在視窗程式的左上角,通常有一個圖示 (icon) 。當我們將視窗縮到最小時,應用程式將以這個圖示代表。 游標資源 當操作應用程式時,滑鼠的游標會隨著執行的狀態有所不同。在 MFC 裡,亦提供了滑鼠游標資源,用於建立自訂滑鼠游標。

85

86

87

88

89

90

91

92 #include <afxwin.h>
#include <afxext.h> #include "resource.h" class CMyFrame : public CFrameWnd { private: CMenu MainMenu, *SysMenu, *PopMenu, *SubMenu; CToolBar toolbar; CStatusBar statusbar; COLORREF color; HCURSOR hcursor; CBitmap bmp; public: CMyFrame() { CMenu *Menu; Create(NULL, "Menu Resource Demo", WS_OVERLAPPEDWINDOW | WS_VISIBLE, rectDefault, NULL, MAKEINTRESOURCE(IDR_SMENU)); SysMenu = GetSystemMenu(FALSE); LoadAccelTable(MAKEINTRESOURCE(IDR_ACCEL)); color = RGB(255, 0, 0); bmp.LoadBitmap(IDB_BITMAP); Menu = GetMenu()->GetSubMenu(0); Menu->AppendMenu(MF_BITMAP, IDM_BMPITEM, &bmp); } ~CMyFrame() { } afx_msg void OnBmpItem() { MessageBox("Bitmap item selected.", "Message Box");

93 afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct) {
if(CFrameWnd::OnCreate(lpCreateStruct)) return -1; toolbar.Create(this); toolbar.LoadToolBar(IDR_TOOLBAR); toolbar.EnableDocking(CBRS_ALIGN_TOP); toolbar.SetBarStyle(toolbar.GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC); EnableDocking(CBRS_ALIGN_ANY); DockControlBar(&toolbar); static UINT indicators[] = { ID_SEPARATOR, IDS_RED } ; statusbar.Create(this); statusbar.SetIndicators(indicators, sizeof(indicators) / sizeof(UINT)); statusbar.SetPaneStyle(1, SBPS_POPOUT); return 0; } afx_msg void OnSwitchItem() { CMenu InsMenu; InsMenu.LoadMenu(IDR_INSMENU); MainMenu.LoadMenu(IDR_MAINMENU); MainMenu.AppendMenu(MF_POPUP, (UINT) InsMenu.m_hMenu, "&Color Setting"); SubMenu = MainMenu.GetSubMenu(1); SubMenu->AppendMenu(MF_SEPARATOR); SubMenu->InsertMenu(IDM_CREATEITEM, MF_BYCOMMAND | MF_STRING, IDM_INSERTITEM, "&Insert Item"); SubMenu->InsertMenu(IDM_CREATEITEM, MF_BYCOMMAND | MF_POPUP, (UINT) InsMenu.m_hMenu, "Insert &submenu"); //SetCheck(); InsMenu.Detach(); SetMenu(&MainMenu);

94 afx_msg void OnCreateItem() {
MainMenu.ModifyMenu(IDM_CREATEITEM, MF_BYCOMMAND, IDM_DELETEITEM, "&Delete Item"); SubMenu = MainMenu.GetSubMenu(1); SubMenu->AppendMenu(MF_STRING, IDM_NEWITEM, "New Item"); } afx_msg void OnDeleteItem() { MainMenu.ModifyMenu(IDM_DELETEITEM, MF_BYCOMMAND, IDM_CREATEITEM, "&Create Item"); SubMenu->DeleteMenu(IDM_NEWITEM, MF_BYCOMMAND); afx_msg void OnContextMenu(CWnd *pWnd, CPoint point) { CMenu menu; menu.LoadMenu(IDR_POPMENU); PopMenu = menu.GetSubMenu(0); PopMenu->TrackPopupMenu(TPM_CENTERALIGN | TPM_RIGHTBUTTON, point.x, point.y, this); menu.Detach(); afx_msg void OnRed() { CString resstr; color = RGB(255, 0, 0); resstr.LoadString(IDS_RED); statusbar.SetPaneText(1, resstr.GetBuffer(80)); afx_msg void OnGreen() { color = RGB(0, 255, 0); resstr.LoadString(IDS_GREEN);

95 afx_msg void OnBlue() {
CString resstr; color = RGB(0, 0, 255); resstr.LoadString(IDS_BLUE); statusbar.SetPaneText(1, resstr.GetBuffer(80)); } afx_msg void OnUpdateRed(CCmdUI *aCmdUI) { aCmdUI->SetCheck(color == RGB(255, 0, 0)); afx_msg void OnUpdateGreen(CCmdUI *aCmdUI) { aCmdUI->SetCheck(color == RGB(0, 255, 0)); afx_msg void OnUpdateBlue(CCmdUI *aCmdUI) { aCmdUI->SetCheck(color == RGB(0, 0, 255)); afx_msg void OnLButtonDown(UINT nFlags, CPoint point) { hcursor = AfxGetApp()->LoadCursor(IDC_CURSOR); ::SetCursor(hcursor); SetCapture(); afx_msg void OnMouseMove(UINT nFlags, CPoint point) { if(this == GetCapture()) { CClientDC aDC(this); aDC.SetPixel(point, color); afx_msg void OnLButtonUp(UINT nFlags, CPoint point) { ReleaseCapture(); }

96 DECLARE_MESSAGE_MAP()
} ; BEGIN_MESSAGE_MAP(CMyFrame, CFrameWnd) ON_WM_CREATE() ON_COMMAND(IDM_SWITCHITEM, OnSwitchItem) ON_COMMAND(IDM_CREATEITEM, OnCreateItem) ON_COMMAND(IDM_DELETEITEM, OnDeleteItem) ON_COMMAND(IDM_RED, OnRed) ON_COMMAND(IDM_GREEN, OnGreen) ON_COMMAND(IDM_BLUE, OnBlue) ON_COMMAND(IDM_BMPITEM, OnBmpItem) ON_UPDATE_COMMAND_UI(IDM_RED, OnUpdateRed) ON_UPDATE_COMMAND_UI(IDM_GREEN, OnUpdateGreen) ON_UPDATE_COMMAND_UI(IDM_BLUE, OnUpdateBlue) ON_WM_CONTEXTMENU() ON_WM_LBUTTONDOWN() ON_WM_MOUSEMOVE() ON_WM_LBUTTONUP() END_MESSAGE_MAP() class CMyApp : public CWinApp { public: BOOL InitInstance() { CFrameWnd *Frame = new CMyFrame; m_pMainWnd = Frame; Frame->SetIcon(LoadIcon(IDI_ICON), TRUE); Frame->ShowWindow(SW_SHOW); return true; } CMyApp a_app;


Download ppt "Visual C++ Windows Programming"

Similar presentations


Ads by Google