MFC学习笔记之七————工具栏编程与状态栏编程

首先来说MFC中工具栏的创建方法,可以有两种方法创建工具栏:

     第一种方式的步骤如下:

(1)创建工具栏资源。这个在IDE中的资源编辑器中完成。

(2)构造一个CToolBar对象。

(3)调用Create()函数或者CreateEx()函数创建Windows工具栏,并把它与已经创建好的CToolBar对象关联起来。(两个Create函数是CTollBar的成员函数)。原型看看:

1 BOOL Create( CWnd* pParentWnd, DWORD dwStyle = WS_CHILD | WS_VISIBLE | CBRS_TOP, UINT nID = AFX_IDW_TOOLBAR );
2 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函数中:

1 if (!m_newToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_RIGHT
2   | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
3   !m_newToolBar.LoadToolBar(IDR_TOOLBAR1))
4 {
5   TRACE0("Failed to create toolbar\n");
6   return -1;      // fail to create
7 }

  第二种创建工具栏的方式:

(1)构造一个CToolBar对象;

(2)调用CreateEx或者Create函数创建工具栏,并把它与以创建的CToolBar对象关联起来。

(3)调用LoadBitmap函数加载包含工具栏按钮的图像位图。

(4)调用SerButtons函数设置按钮的样式;原型:

1 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()成员函数,目的是让主框架窗口可停靠。停靠表可是上表的前五项组成。

隐藏显示工具栏:

1 m_newToolBar.ShowWindow(SW_HIDE);
2 m_newToolBar.ShowWindow(SW_SHOW);

  

接下来是状态栏编程:

我们可以看到在CMainFrame的OnCreate函数中已经有了创建状态栏响应的代码,Indicator[]数组中有状态栏的信息如果要增加,可以在String Table中加入一个IDS_Timer(自定义的状态栏Item的ID),然后将其加入到数组中

1 if (!m_wndStatusBar.Create(this) ||
2         !m_wndStatusBar.SetIndicators(indicators,
3           sizeof(indicators)/sizeof(UINT)))
4     {
5         TRACE0("Failed to create status bar\n");
6         return -1;      // fail to create
7     }

  

1 static UINT indicators[] =//indicator数组
2 {
3     ID_SEPARATOR,           // status line indicator
4     IDS_TIMER,
5     IDS_PROGRESS,
6     ID_INDICATOR_CAPS,
7     ID_INDICATOR_NUM,
8     ID_INDICATOR_SCRL,
9 };

  当然我们可以在状态栏中添加一些状态的显示,还有可以加入以下状态显示的控件!比如:CProgressCtrl控件。    

要记得最重要的——MSDN!

posted @ 2011-11-25 16:10  jeffkuang  阅读(188)  评论(0)    收藏  举报