新建一个mfc[exe]文件,在View类里添加WM_LBUTTONDOWN和M_TIMER消息。
在OnLButtonDown类里添加定时函数:SetTimer(1,1000,NULL);
在OnTimer(UINT nIDEvent)函数里添加以下代码:
// TODO: Add your message handler code here and/or call default
CDC *hdc;
hdc=GetDC();
CFont font;
font.CreatePointFont(1000, "MyFont");
LOGFONT pLogFont;
font.GetLogFont(&pLogFont);
hdc->SelectObject(font);
hdc->SetTextColor(RGB(255,0,0));
CString str;
char ch[2];
itoa(m_iCount--,ch,10);
str=ch;
CBrush pBrush(RGB(255,255,255));
CRect rect;
GetClientRect(&rect);
hdc->FillRect(rect,&pBrush);
if(m_iCount>=0)
hdc->TextOut((rect.left+rect.right)/2-50/*-pLogFont.lfWidth/2*/,(rect.top+rect.bottom)/2-50/*-pLogFont.lfHeight/2*/,str);
else
{
hdc->TextOut((rect.left+rect.right)/2-120,(rect.top+rect.bottom)/2-80,"Boom!");
KillTimer(1);
}
ReleaseDC(hdc);
CView::OnTimer(nIDEvent);
编译运行,点击鼠标左键开始倒计时,呵呵,小小程序。