在状态栏中添加一个时钟功能

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);
}

运行即可;

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值