dirty 13℃

今日無事.

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

问题: 使用VC++, 每隔 5秒 钟,向一个txt中输出一次系统时间

思路: 使用定时器,5秒触发一次,在定时器触发事件内获取系统时间并写入文件.

步骤:
------------------------------------------------------------
1 建立MFC单文档程序

2 定义定时器 ID,声明文件对象
  #define IDTIMER   101
  CStdioFile file;

3 在 CMainFrame::OnCreate() 内打开文件, 设置定时器
  file.Open("ts.txt",CFile::modeCreate|CFile::modeWrite);
  SetTimer(IDTIMER,5000,0);

4 在 CMainFrame::OnTimer() 内定时获取系统时间并写入文件
    switch(nIDEvent)
    {
    case IDTIMER:
        {
            CString u_time;
            CTime time = CTime::GetCurrentTime();
            u_time.Format("%4d-%02d-%02d %02d:%02d:%02d\n",
                          time.GetYear(),time.GetMonth(),time.GetDay(),
                          time.GetHour(),time.GetMinute(),time.GetSecond());
            file.Seek(0,CFile::end);
            file.WriteString( u_time );
            break;
        }
    }

5 在 CMainFrame::OnDestroy() 内销毁定时器与关闭文件
    KillTimer(IDTIMER);
    file.Close();


参考:
------------------------------------------------------------
1 MFC中定时器的使用
  http://www.programbbs.com/doc/2934.htm
2 MFC - 文本文件的简单操作
  http://www.programbbs.com/doc/2906.htm
3 vc++取得系统时间
  http://www.programbbs.com/doc/4966.htm
 

posted on 2009-04-26 20:38  xbride  阅读(297)  评论(0)    收藏  举报