工具栏与状态栏

来源:互联网 发布:js class选择器 编辑:程序博客网 时间:2024/06/05 08:06

1,工具栏是CToolBar类一个对象,状态栏是CStatusBar类的对象.CToolBar类和CStatusBar类均由CControlBar类派生.CControlBar类则由CWnd类派生.
CControlBar类所支持的控制栏窗口位于主框架窗口内,并且这些控制栏窗口能够随着主框架窗口的尺寸改变或移动自动调整自己的尺寸与位置.
控制栏对象的构造与析构以及窗口的创建都是由应用程序框架来管理的,其向导产生的代码位于框架头文件和代码文件中.

2,工具栏的按钮被按下是会象菜单或加速键一样,发送相应的命令消息(该命令消息会象菜单命令一样进行传递.原因:工具栏对象以及状态栏对象都与主框架窗口联系.).
更新命令UI消息控制函数一方面可以用来更新工具栏中的按钮的状态,另一方面可以用来修改工具栏中的按钮图形.
整个工具栏只有一个位图,每一个按钮在其中占一个高15像素宽16像素的位图片.
注意:不要直接编辑工具栏位图,而是应该使用DeveloperStudio的特定工具栏编辑工具进行编辑.

3,更新命令消息控件函数主要是用来对菜单项进行禁止和复选的,但也可以作用于工具栏按钮.
1)CCmdUI::Enable
virtual void Enable( BOOL bOn = TRUE );
//bOn: TRUE to enable the item, FALSE to disable it.
//Call this member function to enable or disable the user-interface item for this command.
2)CCmdUI::SetCheck
virtual void SetCheck( int nCheck = 1 );
//nCheck: Specifies the check state to set. If 0, unchecks; if 1, checks; and if 2, sets indeterminate.
//Call this member function to set the user-interface item for this command to the appropriate check state. This member function works for menu items and toolbar buttons. The indeterminate state applies only to toolbar buttons.

4,当某菜单项所在的弹出式菜单弹出时候,对该菜单项进行处理的更新命令UI消息控制函数才会被调用.
工具栏总是处于显示状态,对它进行处理的更新命令UI控制函数是在应用程序的空状态处理过程中被调用的.
当同一个更新命令UI控制函数既需要对某菜单项处理,又需要对某工具栏按钮进行处理,则在空状态处理过程及当该菜单项所在的弹出式菜单弹出时候,该控制函数都会被调用.

5,创建工具提示:
把提示文本加在菜单提示的后面,前面加一个换行符.如在Prompt:打印活动文档n打印

6,几个重要函数
1)CWnd::GetParentFrame 
CFrameWnd* GetParentFrame( ) const;
//Return Value:A pointer to a frame window if successful; otherwise NULL.
//Call this member function to retrieve the parent frame window. The member function searches up the parent chain until a CFrameWnd (or derived class) object is found.
//MDI中得到的是MDI子框架窗口.
2)AfxGetApp 
CWinApp* AfxGetApp( );
//Return Value:A pointer to the single CWinApp object for the application.
//The pointer returned by this function can be used to access application information such as the main message-dispatch code or the topmost window.
通过应用程序获得主框架窗口指针:(单文档和多文档程序均适用)
 CMainFrame* pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;
 CToolBar* pToolBar=&pFrame->m_wndToolBar;
注:在SDI程序中,当视图的OnCreate函数被调用时候,m_pMainWnd还没有被设置,这时候可通过CWnd::GetParentFrame来获取主框架窗口指针.
说明:
在MDI程序中AppWizard向导自动生成了对m_pMainWain的赋值代码.而在SDI中,框架是在视图的创建的过程中对m_pMainWain的赋值代码的.


7,工具栏编辑器的使用:用鼠标选中某按钮,然后按DEL键可以擦除该按钮的像素;如果想删掉某个按钮,只需要把它拖出工具栏即可.

8,工具栏的停靠:
在CMainFrame::OnCreate函数中可以看到如下代码,该代码允许工具栏在框架窗口任何边筐处停靠:
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
说明:
1)CControlBar::EnableDocking
//Call this function to enable a control bar to be docked. The sides specified must match one of the sides enabled for docking in the destination frame window, or the control bar cannot be docked to that frame window.
2)CFrameWnd::EnableDocking
//Call this function to enable dockable control bars in a frame window. By default, control bars will be docked to a side of the frame window in the following order: top, bottom, left, right.
3)CFrameWnd::DockControlBar
//Causes a control bar to be docked to the frame window. The control bar will be docked to one of the sides of the frame window specified in the calls to both CControlBar::EnableDocking and CFrameWnd::EnableDocking. The side chosen is determined by nDockBarID.

9,状态栏既不接受用户输入也不产生命令消息,它的作用就是在程序的控制下在窗格(pane)中显示一些文本.
状态栏支持两种类型文本窗格(信息行窗格 和 状态指示器窗格).
注:为实现在状态栏中显示一些应用程序的特殊数据,必须先禁止标准状态栏显示菜单提示及键盘状态.(具体方法?)
1)AppWizard在MainFrm.cpp文件中产生的静态数组indicators是用以定义状态栏的.
static UINT indicators[] =
{
 ID_SEPARATOR,           // status line indicator
 ID_INDICATOR_CAPS, //以下均为串资源ID
 ID_INDICATOR_NUM,
 ID_INDICATOR_SCRL,
};
2)CStatusBar::SetIndicators//根据indicators[]数组内容对状态栏进行设置.
BOOL SetIndicators( const UINT* lpIDArray, int nIDCount );
//Sets each indicator’s ID to the value specified by the corresponding element(对应元素) of the array lpIDArray, loads the string resource specified by each ID, and sets the indicator’s text to the string.
//SetIndicators在应用程序的派生框架类中被调用.在CMainFrame::OnCreate函数中可以看见.
3)信息行窗格所显示的是储蓄动态提供的字符串.
对信息行的设置:首先访问状态栏对象,然后再用从0开始的索引参数来调用CStatusBar::SetPaneText函数设置.
如:
 CMainFrame* pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;
 CStatusBar* pStatus=&pFrame->m_wndStatusBar;
 pStatus->SetPaneText(0,"message line for first pane");
4)状态指示器窗格总是与一个字符串相连,该字符串是由字符串资源来提供的(在string table里设置),它是否被显示完全取决于相应的更新命令UI控制函数.
指示器是由一个字符串资源ID来标识的,该ID也被用来传递更新命令UI消息.
afx_msg void CMainFrame::OnUpdateKeyCapsLock(CCmdUI *pCmdUI);
ON_UPDATE_COMMAND_UI(ID_INDICATOR_CAPS,OnUpdateKeyCapsLock)
void CMainFrame::OnUpdateKeyCapsLock(CCmdUI *pCmdUI)
{
 pCmdUI->Enable(::GetKeyState(VK_CAPITAL)&1);
}
说明:
状态栏的更新命令UI消息控制函数是在空闲处理阶段被调用的.(推测:调用的时候传入CCmdUI指针).
5)状态指示器窗格的长度即为相应字符串资源的长度.


11,获取状态栏的控制.
1)用于键盘状态指示器的更新命令UI控制函数被嵌入在主框架窗口类中,它们与资源字符串ID(indicators[]中定义的串资源ID)相联.
2)状态栏实际上有一个子窗口(即status line indicator),其默认ID在CMainFrm::OnCreate函数中的CStatusBar::Create函数中设置为AFX_IDW_STATUS_BAR.改变这个ID可以实现禁止框架在0号窗格中显示菜单提示.
具体实现如下:
将CMainFrm::OnCreate函数中的CStatusBar::Create函数由m_wndStatusBar.Create(this)改成m_wndStatusBar.Create( this,WS_CHILD | WS_VISIBLE | CBRS_BOTTOM, ID_MY_STATUS_BAR(自定义的ID)).


11,注意,左右鼠标按钮跟键盘上的键一样有虚键码,可以通过GetKeyState函数获取。

原创粉丝点击