常规方法:
1..h文件中添加成员变量CStatusBar m_wndStatusBar;
2.在OnInitDialog()中加入:
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS, //CAP lock indicator.
ID_INDICATOR_NUM, //NUM lock indicator.
ID_INDICATOR_SCRL, //SCRL lock indicator.
};
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status barn");
return -1; // fail to create
}
UINT nID; //控制状态栏里面的分栏
m_wndStatusBar.SetPaneInfo(0,nID,SBPS_STRETCH|SBPS_NOBORDERS,100); //返回值存nID中
m_wndStatusBar.SetPaneText(0,"就绪");
m_wndStatusBar.SetPaneInfo(1,nID,SBPS_NORMAL,100);
m_wndStatusBar.SetPaneText(1,"大写");
m_wndStatusBar.SetPaneInfo(2,nID,SBPS_POPOUT,100);
m_wndStatusBar.SetPaneText(2,"数字");
//----------------让这个状态栏最终显示在对话框中-------------
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
其中: static UINT indicators[]最好写在.cpp最开头的位置, ID_SEPARATOR等变量需要在String Table中添加,注意ID和值不能重复,标题任意,SetPaneInfo中可以设置需要显示的内容
非常规方法
即用 Rec xxxRec指定了窗体显示位置的情况下,状态栏的位置也需要手动指定,不然无法显示出来
1..h声明变量CStatusBar m_bar; //状态栏 CRect m_statusBarRect; //状态栏区域位置
2..cpp外部static UINT BASED_CODE indicators[] = { IDS_Bar_One, IDS_Bar_Two,IDS_Bar_Three,IDS_Bar_Four}; //IDS_Bar_One声明方法同上
3.oninit()中实现
//创建状态栏
m_Statusbar.Create(this);
m_Statusbar.SetIndicators(indicators, sizeof(indicators) / sizeof(UINT));
CRect rect;
GetClientRect(&rect);
m_Statusbar.SetPaneInfo(0, IDS_Bar_One, SBPS_NORMAL, rect.Width()/4);
m_Statusbar.SetPaneInfo(1, IDS_Bar_Two, SBPS_STRETCH, rect.Width()/4);
m_Statusbar.SetPaneInfo(2, IDS_Bar_Three, SBPS_STRETCH, rect.Width()/4);
m_Statusbar.SetPaneInfo(3, IDS_Bar_Four, SBPS_STRETCH, rect.Width()/4);
CString m_verForApp,m_verForDb,Tips[4];
::GetPrivateProfileString("General Info", "Ver", "", m_verForApp.GetBuffer(MAX_PATH), MAX_PATH, Util::GetWorkPath() + "\\ConcreteSync.ini");
::GetPrivateProfileString("Database Info", "Ver", "", m_verForDb.GetBuffer(MAX_PATH), MAX_PATH, Util::GetWorkPath() + "\\ConcreteSync.ini");
m_verForApp.ReleaseBuffer();
m_verForDb.ReleaseBuffer();
Tips[0].Format("拌合站数据提取客户端: Ver %s", m_verForApp);
Tips[1].Format("当前导入的数据库版本: %s", m_verForDb);
Tips[2].Format("当前登录用户: [%s]", g_strLogin);
Tips[3].Format("当前时间 ");
//m_Statusbar.GetStatusBarCtrl().SetBkColor(RGB(0, 0, 0));//黑色背景
m_Statusbar.SetPaneText(0, Tips[0]);
m_Statusbar.SetPaneText(1, Tips[1]);
m_Statusbar.SetPaneText(2, Tips[2]);
m_Statusbar.SetPaneText(3, Tips[3]);
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, AFX_IDW_CONTROLBAR_FIRST);
UpdatePannelPosition();//设置各个Rec的参数
4. 设置显示位置(UpdatePannelPosition();的部分实现)
GetClientRect(&m_clientRect);
int cx = m_clientRect.right - 10;
int leftPanelWidth = 360, rightPanelWidth = 480;
if (cx <= 1440) {
leftPanelWidth = 240;
rightPanelWidth = 300;
}
//状态栏
m_statusBarRect.top = m_clientRect.bottom - 40; //高40px
m_statusBarRect.bottom = m_clientRect.bottom; //紧贴窗口底部
m_statusBarRect.left = m_clientRect.left+10; //左侧距离10px
m_statusBarRect.right = m_clientRect.right-10; //右侧距离10px
m_bar.MoveWindow(m_statusBarRect); //让之前的m_bar显示在我们指定的位置
补充:实时显示时间
1.添加WM_TIME消息映射,就自动生成OnTime函数了//关于MFC的OnTimer的说明
2.
void CMainWindow::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CTime t = CTime::GetCurrentTime();
CString str;
str.Format("当前时间: %04d/%02d/%02d %02d:%02d:%02d", t.GetYear(), t.GetMonth(), t.GetDay(), t.GetHour(), t.GetMinute(), t.GetSecond());
m_Statusbar.SetPaneText(3, str);
CDialogEx::OnTimer(nIDEvent);
}
3.创建状态栏之后紧跟SetTimer(1, 1000, NULL);
成果如图: