电子时钟的制作

来源:互联网 发布:标准差数据统计软件 编辑:程序博客网 时间:2024/04/26 04:20

微笑偷笑敲打得意                                                                                                                        数字时钟的制作

  这个小程序还是比较简单的

主要步骤是1,首先创建一个对话框程序。

                      2.在对话框中插入一个编辑框,主要是显示时间的。然后就是编程了,

先了解定时器的使用方法,这个是关键

UINT SetTimer(
      HWND
 hWnd,              // handle of window for timer messages
      UINT nIDEvent,          // timer identifier
      UINT uElapse,           // time-out value
      TIMERPROC lpTimerFunc       // address of timer procedure
);参数一是指向CWnd的指针,参数二是定时器的ID,也就是定时器的标志,参数三是时间延迟,单位是毫秒,也就是每隔uElapse毫秒调用一次OnTimer函数

在void CTime2Dlg::OnPaint() 函数中设置一个定时器

SetTimer(1,1000,NULL);//设置定时器

在CTimeDlg类中添加WM_TIMER的消息响应函数,

void CTime2Dlg::OnTimer(UINT nIDEvent) 
{
// TODO: Add your message handler code here and/or call default
     CString str;//定义字符串存放时间
CTime time;
     time=CTime::GetCurrentTime();//获取当前的时间
str=time.Format("%H:%M:%S");//格式化时间
CFont *m_Font;
        m_Font = new CFont; 
        m_Font->CreateFont(40,20,0,0,100,
        FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,
        CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,FF_SWISS,"Arial");
        CEdit *m_Edit=(CEdit *)GetDlgItem(IDD_CLOCK);
        m_Edit->SetFont(m_Font,FALSE);//改变输出的时间数字大小,
        GetDlgItem(IDD_CLOCK)->SetFont(m_Font);

     ((CEdit*)GetDlgItem(IDD_CLOCK))->SetWindowText(str);//将时间在编辑框中显示出来
CDialog::OnTimer(nIDEvent);
}

3.改变对话框的背景颜色

在CTimeDlg类中添加WM_CTLCOLOR的消息响应函数

HBRUSH CTime2Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

// TODO: Change any attributes of the DC here
// TODO: Return a different brush if the default is not desired
return hbr;
}

这是刚添加之后的响应函数,要改变背景颜色,添加一个成员变量CBrush m_brush

在构造函数中初始化,在响应函数中返回此画刷即可。








































0 0