在SDI工程的视图中的OnDraw()函数中动态显示系统的时间

本文介绍了一个使用MFC实现的定时更新视图中显示时间的方法。通过SetTimer和OnTimer函数组合,每隔一秒更新视图上的时间显示,实现了动态的时间显示效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、void 工程中视图名::OnDraw(CDC* pDC)
{
    CRetertrtrDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    // TODO: add draw code for native data here
 
    CDC *pDC1=GetDC();
    CString strTime; 
    CTime tTime = CTime::GetCurrentTime();      
    strTime = tTime.Format("%Y-%m-%d %H:%M:%S");   //根据得到的当前时间格式化为字符串形式  
 
    pDC1->TextOut(100,100,strTime);
}
2、创建OnInitialUpdate()函数
void工程中视图名::OnInitialUpdate()
{
    CView::OnInitialUpdate();
    // TODO: Add your specialized code here and/or call the base class
   //在View中不写对象名
    SetTimer(1,1000,NULL);
}
3、因SetTimer()函数中的最后一项函数名为NULL,调用OnTimer()函数
void 工程中视图名::OnTimer(UINT nIDEvent)
{
    // TODO: Add your message handler code here and/or call default
 
    CDC * pDC=GetDC();
    CString strTime; 
    CTime tTime = CTime::GetCurrentTime();      
    strTime = tTime.Format("%Y-%m-%d %H:%M:%S");   //根据得到的当前时间格式化为字符串形式  
 
    SetDlgItemText(IDC_StaticFileName, strTime);
   
    pDC->TextOut(100,100,strTime);
 
    CView::OnTimer(nIDEvent);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值