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

首先来说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 indicator
	IDS_TIMER,
	IDS_PROGRESS,
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

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

要记得最重要的——MSDN!

       记录下自己的所学,虽然浅薄!!!如果我是一只蜗牛,那就只能慢慢向上爬!奋斗

posted on 2011-08-04 21:48  Pe.潘义  阅读(1730)  评论(0编辑  收藏  举报

导航