1、在CMainFrame中定义一个状态栏的对象:
protected:
CStatusBar m_wndStatusBar; //一般都是系统自己已经设置好的
2、在String Table中添加资源 IDS_TIMER 时钟
2、在CMainFrame中添加全局变量:
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
IDS_TIMER, //新加的
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
3、在CMainFrame的OnCreate中的代码:
if (!m_wndStatusBar.Create(this) || //create(this)是创建一个状态栏
!m_wndStatusBar.SetIndicators(indicators, //SetIndicators是设置一个指示器,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
4、运行既可以看到在状态栏中有一个窗格中出现“时钟”
5、获取系统当前时间
在CMainFrame的OnCreate中的代码:
CTime t = CTime::GetCurrentTime(); //获取当前系统时间
CString str=t.Format("%H:%M:%S");//将时间格式化
CClientDC dc(this);
CSize sz = dc.GetTextExtent(str); //获取str的字节数
m_wndStatusBar.SetPaneInfo(1,IDS_TIMER,SBPS_NORMAL,sz.cx); //设定指示器面板的信息
m_wndStatusBar.SetPaneText(m_wndStatusBar.CommandToIndex(IDS_TIMER),str);//在状态栏面板上显示时间
运行可以看到显示了系统当前的时间;
6、让显示的时间随着时间的变化而变化
在OnTimer中添加代码如下:
void CMainFrame::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
static int index=1;
SetClassLong(m_hWnd,GCL_HICON,(LONG)m_hIcon[index]);
index = ++index%3; //让变量在0-2之间变换;
CTime t = CTime::GetCurrentTime(); //获取当前系统时间
CString str=t.Format("%H:%M:%S");//将时间格式化
CClientDC dc(this);
CSize sz = dc.GetTextExtent(str);
m_wndStatusBar.SetPaneInfo(1,IDS_TIMER,SBPS_NORMAL,sz.cx);
m_wndStatusBar.SetPaneText(m_wndStatusBar.CommandToIndex(IDS_TIMER),str);//在状态栏面板上显示时间
m_progress.StepIt();
CFrameWnd::OnTimer(nIDEvent);
}
运行即可;