步骤:
1,添加String Table中的资源,IDS_TIMER
2,在MainFrm.cpp文件中的indicators数组中添加IDS_TIMER
3,在OnCreate函数中设置一个定时器。
4.添加消息为WM_TIMER的函数OnTimer。
代码:
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)


{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))

{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}

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
}

// TODO: Delete these three lines if you don't want the toolbar to
// be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
int indexStatusBar = 0;
CTime t = CTime::GetCurrentTime();
CString strTime = t.Format("%H:%M:%S");
CClientDC dc(this);
CSize sz = dc.GetTextExtent(strTime);
indexStatusBar = m_wndStatusBar.CommandToIndex(IDS_TIMER);
m_wndStatusBar.SetPaneInfo(indexStatusBar,IDS_TIMER,SBPS_NORMAL,sz.cx);
m_wndStatusBar.SetPaneText(indexStatusBar,strTime);

SetTimer(1,1000,NULL);

return 0;
}




void CMainFrame::OnTimer(UINT nIDEvent)


{
// TODO: Add your message handler code here and/or call default
int indexStatusBar = 0;
CTime t = CTime::GetCurrentTime(); //获得当前时间
CString strTime = t.Format("%H:%M:%S"); //格式化当前时间
CClientDC dc(this);
CSize sz = dc.GetTextExtent(strTime); //获得时间显示的宽度
indexStatusBar = m_wndStatusBar.CommandToIndex(IDS_TIMER); //获得窗格的索引
m_wndStatusBar.SetPaneInfo(indexStatusBar,IDS_TIMER,SBPS_NORMAL,sz.cx); //改变窗格的宽度
m_wndStatusBar.SetPaneText(indexStatusBar,strTime); //设置窗格的内容

CFrameWnd::OnTimer(nIDEvent);
}

注意在CMainFrame函数中的那个OnCreate函数中的部分代码,设置显示时间的,其实是一个初始化的功能,因为开始的时候如果没有这个代码是不显示时间的。当然,如果仅仅是一个计时器,那么后面根据这个代码,也是会自动更新时间的,只是初始化的话未知时间。
吸取的一点就是:懂得了如何改变状态栏的宽度,这样就不会出现前面的那个不知道状态栏太窄怎么办的问题。
解决方法是:
得到显示的CSize,然后使用CommandToIndex来得到是需要改变那个的宽度(即得到显示内容的窗格的索引,正确的改变那一个,而不是所有的宽度),设置SetPaneInfo来改变宽度。最终设置内容就OK了。
详细点就是:真正改变宽度的是SetPaneInfo的最后一个参数,而这个正式CSize的的宽度cx。
posted on 2010-02-17 22:17
deercoder 阅读(511)
评论(0) 编辑 收藏 引用