MFC状态栏的编程(SDI)

来源:互联网 发布:网络盗刷信用卡能查到 编辑:程序博客网 时间:2024/06/05 06:23

1状态栏是窗口的最下面,分为左边和右边,左边主要显示按钮的提示行功能,右边主要显示键盘上指示器。CStatuBar类。

继续追踪CStatuBar的类成员函数在CMainFram.cpp中,有着对其定义的的OnCreat类,

if (!m_wndStatusBar.Create(this) ||
  !m_wndStatusBar.SetIndicators(indicators,
    sizeof(indicators)/sizeof(UINT)))
 {
  TRACE0("未能创建状态栏\n");
  return -1;      // 未能创建
 }


关注indicators数组


static UINT indicators[] =
{
 ID_SEPARATOR,           // 状态行指示器
 ID_INDICATOR_CAPS,
 ID_INDICATOR_NUM,
 ID_INDICATOR_SCRL,
};

所以可以在资源管理中增加想要添加的资源

在这里我们增加ID_TIMER字符串


下面我们说一下怎样获得系统时钟,这里介绍CTime类。

 CTime t=CTime::GetCurrentTime();
 CString str=t.Format ("%H:%M:%S");


获得系统时钟并按照::格式输出。


下面怎样将str与时间对应,用到CStatusBar::SetPaneText()函数;


 m_wndStatusBar.SetPaneText(1,str);


在不知道索引号的时候,用函数ComdToIndex();可以查找。


2.在状态栏显示时候不能全部显示时间,下面要将显示字符的大小获取,并且设置状态栏显示时候的大小。

CClientDC dc(this);
 CSize sz=dc.GetTextExtent(str);
 m_wndStatusBar.SetPaneInfo(1,IDS_TIMER,SBPS_NORMAL,sz.cx);


3.动态显示系统时间还是用定时器,SetTimer()

然后在系统消息中OnTimer函数

void CMainFrame::OnTimer(UINT_PTR nIDEvent)
{
 // TODO: 在此添加消息处理程序代码和/或调用默认值
 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(1,str);

 CFrameWnd::OnTimer(nIDEvent);
}时间就动起来了。







0 0