直接上图
代码.h:
public:
CStatusBarCtrl m_wndStatusBar;
int Updata();
代码.cpp
int CMFCStatusBarDlg::Updata()
{
// TODO: 在此处添加实现代码.
m_wndStatusBar.Create(WS_CHILD | WS_VISIBLE | SBT_OWNERDRAW, CRect(0, 0, 0, 0), this, 0);
int strPartDim[4] = {100,200,300,-1}; //分割数量
m_wndStatusBar.SetParts(4, strPartDim);
//设置状态栏文本
m_wndStatusBar.SetText(_T("分栏一"), 0, 0);
m_wndStatusBar.SetText(_T("分栏二"), 1, 0);
m_wndStatusBar.SetText(_T("分栏三"), 2, 0);
CTime t1;
t1 = CTime::GetCurrentTime();
m_wndStatusBar.SetText((t1.Format("%H:%M:%S")), 3, 0);
//下面是在状态栏中加入图标
// m_wndStatusBar.SetIcon(1,SetIcon(AfxGetApp()->LoadIcon(IDR_MAINFRAME),FALSE));//为第二个分栏中加的图标
return 0;
}
截图:
其他方式:
.h
public:
CStatusBar m_wndStatusBar;
int Updata();
afx_msg void OnTimer(UINT_PTR nIDEvent);
.cpp
int CMFCStatusBarDlg::Updata()
{
// TODO: 在此处添加实现代码.
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 bar\n");
return -1; // fail to create
}
UINT nID =0; //控制状态栏里面的分栏
//m_wndStatusBar.SetPaneInfo(0, nID, SBPS_STRETCH | SBPS_NOBORDERS, 100); //返回值存nID中
//m_wndStatusBar.SetPaneText(0, (_T("就绪")));
//m_wndStatusBar.SetPaneInfo(1, nID, SBPS_NORMAL, 100);
//m_wndStatusBar.SetPaneText(1, (_T("大写")));
/*m_wndStatusBar.SetPaneInfo(0, nID, SBPS_POPOUT, 100);
m_wndStatusBar.SetPaneText(0, (_T("数字")));*/
//SetPaneInfo()函数的第三个参数的可选项如下:
//The following indicator styles are supported:
// SBPS_NOBORDERS No 3-D border around the pane.
// SBPS_POPOUT Reverse border so that text "pops out."
// SBPS_DISABLED Do not draw text.
// SBPS_STRETCH Stretch pane to fill unused space. Only one pane per status bar can have this style.
// SBPS_NORMAL No stretch, borders, or pop-out.
// 启动定时器,定时器ID为1,定时时间为1000ms,即1s
SetTimer(1, 1000, NULL);
//----------------让这个状态栏最终显示在对话框中-------------
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
return 0;
}
void CMFCStatusBarDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CString strTime;
// 获取系统当前时间,并保存到curTime
CTime curTime = CTime::GetCurrentTime();
// 格式化curTime,将字符串保存到strTime
strTime = curTime.Format(_T("%H:%M:%S"));
// 在状态栏的时间窗格中显示系统时间字符串
m_wndStatusBar.SetPaneText(0, strTime);
CDialogEx::OnTimer(nIDEvent);
}
截图: