首先來說MFC中工具列的建立方法,可以有兩種方法建立工具列:
第一種方式的步驟如下:
(1)建立工具列資源。這個在IDE中的資源編輯器中完成。
(2)構造一個CToolBar對象。
(3)調用Create()函數或者CreateEx()函數建立Windows工具列,並把它與已經建立好的CToolBar對象關聯起來。(兩個Create函數是CTollBar的成員函數)。原型看看:
BOOL Create( CWnd* pParentWnd, DWORD dwStyle = WS_CHILD | WS_VISIBLE | CBRS_TOP, UINT nID = AFX_IDW_TOOLBAR );BOOL CreateEx(CWnd* pParentWnd, DWORD dwCtrlStyle = TBSTYLE_FLAT, DWORD dwStyle = WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP, CRect rcBorders = CRect(0, 0, 0, 0), UINT nID = AFX_IDW_TOOLBAR);
參數含義參考MSDN;
(4)調用LoadToolBar函數載入工具列資源。
如下在CMainFrame類中的OnCreate函數中:
if (!m_newToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_RIGHT | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || !m_newToolBar.LoadToolBar(IDR_TOOLBAR1)){ TRACE0("Failed to create toolbar\n"); return -1; // fail to create}
第二種建立工具列的方式:
(1)構造一個CToolBar對象;
(2)調用CreateEx或者Create函數建立工具列,並把它與以建立的CToolBar對象關聯起來。
(3)調用LoadBitmap函數載入包含工具列按鈕的映像位元影像。
(4)調用SerButtons函數設定按鈕的樣式;原型:
BOOL SetButtons( const UINT* lpIDArray, int nIDCount );
下面說說MFC建立工具列的過程,在CMainFrame類的OnCreate函數中,首先調用CToolBar類成員函數OnCreateEx()建立工具列,接著調用LoadToolbar()函數載入工具列資源,接著CMainFrame類的OnCreate函數調用工具列對象的父類CControlBar的成員函數EnableDocking()設定工具列的固定位置,其可取值表如下:
CBRS_ALIGN_TOP Allows docking at the top of the client area.
CBRS_ALIGN_BOTTOM Allows docking at the bottom of the client area.
CBRS_ALIGN_LEFT Allows docking on the left side of the client area.
CBRS_ALIGN_RIGHT Allows docking on the right side of the client area.
CBRS_ALIGN_ANY Allows docking on any side of the client area.
CBRS_FLOAT_MULTI Allows multiple control bars to be floated in a single mini-frame window.
接下來CMainFrame類的OnCreate函數又調用一個EnableDocking函數,先前第一次調用的EnableDocking()函數是工具俊朗對象(父類中)的成員函數,目的是讓工具列對象可以停靠,而第二次調用的是CFrameWnd對象的EnableDocking()成員函數,目的是讓主架構視窗可停靠。停靠表可是上表的前五項組成。
隱藏顯示工具列:
m_newToolBar.ShowWindow(SW_HIDE);m_newToolBar.ShowWindow(SW_SHOW);
接下來是狀態列編程:
我們可以看到在CMainFrame的OnCreate函數中已經有了建立狀態列響應的代碼,Indicator[]數組中有狀態列的資訊如果要增加,可以在String Table中加入一個IDS_Timer(自訂的狀態列Item的ID),然後將其加入到數組中
if (!m_wndStatusBar.Create(this) ||!m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT))){TRACE0("Failed to create status bar\n");return -1; // fail to create}
static UINT indicators[] =//indicator數組{ID_SEPARATOR, // status line indicatorIDS_TIMER,IDS_PROGRESS,ID_INDICATOR_CAPS,ID_INDICATOR_NUM,ID_INDICATOR_SCRL,};
當然我們可以在狀態列中添加一些狀態的顯示,還有可以加入以下狀態顯示的控制項!比如:CProgressCtrl控制項。
要記得最重要的——MSDN!
記錄下自己的所學,雖然淺薄!!!如果我是一隻蝸牛,那就只能慢慢向上爬!