MFC學習筆記之七————工具列編程與狀態列編程

來源:互聯網
上載者:User

首先來說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!

       記錄下自己的所學,雖然淺薄!!!如果我是一隻蝸牛,那就只能慢慢向上爬!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.