MFC时钟和文本输出应用

1.消息映射

ON_WM_TIMER()

2.初始化

 CClientDC cdc(this);
 CString s = TEXT("we are the champions");
 cdc.TextOutW(0, 0, s);
 SetTimer(1, 100, NULL);

3.处理函数

afx_msg void CTextEditorDlg::OnTimer(UINT_PTR nID)
{
 static int textwdth = 0;
 textwdth += 5;
 TEXTMETRIC tm;
 CClientDC cdc(this);
 GetTextMetrics(cdc.m_hDC, &tm);
 CString s = TEXT("we are the champions");
 CSize size=cdc.GetTextExtent(s);
 if (textwdth > size.cx)
 {
  textwdth = 0;
  cdc.TextOutW(0, 0, s);
  return;
 }
 CRect rect(0, 0, textwdth, tm.tmHeight);
 cdc.SetTextColor(RGB(255, 0, 0));
 cdc.DrawText(s, &rect, DT_LEFT);
}

4.运行效果

在这里插入图片描述

你可能感兴趣的:(C++,#,MFC,timer,drawtext)