一.基础知识:
1.状态条类: CStatusBar.(在MSDN中查看其成员Member)
2.在状态条中加入对应字符串ID:
- BOOL SetIndicators(
- const UINT* lpIDArray, //字符串ID
- int nIDCount //个数
- );
3.设置状态条的显示状态:
- void SetPaneInfo(
- int nIndex, //状态条的索引
- UINT& nID, //状态条的字符ID
- UINT& nStyle, //状态条的样式
- int& cxWidth //状态条的宽度
- ) const;
4.设置状态条的显示信息:
- //Call this member function to set the pane text to the string pointed to by lpszNewText.
- BOOL SetPaneText(
- int nIndex,
- LPCTSTR lpszNewText,
- BOOL bUpdate = TRUE
- );
二.具体实现:
1.在资源视图里面,创建字符串资源,
重设ID为:IDR_STATUSBAR_STRING. 标题: 就绪。
2.在对话框类的头文件声明中,添加保护成员:
- CStatusBar m_wndStatusBar; //状态条
3.写入代码创建状态条的代码:
(1)创建字符ID的数组:
- static UINT indicators[] =
- {
- IDR_STATUSBAR_STRING
- };
(2)添加CreatStatusBar函数并写入代码:
- // create status bar
- void CPCRemoteDlg::CreatStatusBar(void)
- {
- if (!m_wndStatusBar.Create(this) ||
- !m_wndStatusBar.SetIndicators(indicators,sizeof(indicators)/sizeof(UINT))) //创建状态条并设置字符资源的ID
- {
- TRACE0("Failed to create status bar\n");
- return ; // fail to create
- }
- CRect rc;
- ::GetWindowRect(m_wndStatusBar.m_hWnd,rc);
- m_wndStatusBar.MoveWindow(rc); //移动状态条到指定位置
- }
(3)在OnInitDialog函数中添加:
- CreatStatusBar(); // 初始化消息条
(4)在OnSize 中添加代码:
- if(m_wndStatusBar.m_hWnd!=NULL){ //当对话框大小改变时 状态条大小也随之改变
- CRect rc;
- rc.left=0; // 消息条的左坐标
- rc.top=cy-20; // 消息条的上坐标
- rc.right=cx; // 消息条的右坐标
- rc.bottom=cy; // 消息条的下坐标
- m_wndStatusBar.MoveWindow(rc);
- m_wndStatusBar.SetPaneInfo(0, m_wndStatusBar.GetItemID(0),SBPS_POPOUT, cx-10);
- }
4.写入状态上显示文字的代码:
①在对话框类的头文件声明中,添加保护成员:
- int iCount; // 记录上线的数量.
②在构造函数中,初始化:
- iCount=0;
③在ShowMessage函数尾部,添加如下代码:
- CString strStatusMsg;
- if (strMsg.Find("上线")>0) //处理上线还是下线消息
- {
- iCount++;
- }else if (strMsg.Find("下线")>0)
- {
- iCount--;
- }else if (strMsg.Find("断开")>0)
- {
- iCount--;
- }
- iCount=(iCount<=0?0:iCount); //防止iCount 有-1的情况
- strStatusMsg.Format("上线: %d",iCount);
- m_wndStatusBar.SetPaneText(0,strStatusMsg); //在状态条上显示文字
5.运行效果: