View
下两个消息事件:ON_WM_CREATE
ON_WM_TIMER
BEGIN_MESSAGE_MAP(CMFCApplication1View, CView)
// 标准打印命令
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
ON_WM_CREATE() // 1
ON_WM_TIMER() // 2
END_MESSAGE_MAP()
OnCreate:
int CMFCApplication1View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
//设置定时器
//1: 定时器id
//500: 时间间隔, 毫秒
//NULL: 使用系统默认的处理函数,每个500毫秒,触发WM_TIMER
SetTimer(1, 100, NULL);
return 0;
}
OnTimer:
//#define SHOW_THE_MESSAGEBOX
void CMFCApplication1View::OnTimer(UINT_PTR nIDEvent)
{
if (nIDEvent == 1)
{
#ifdef SHOW_THE_MESSAGEBOX
static int i = 0;
i++;
CString str;
str.Format(TEXT("%d"), i);
if (i == 5)
{
KillTimer(1);
}
MessageBox(str);
#endif // SHOW_THE_MESSAGEBOX
#ifndef SHOW_THE_MESSAGEBOX
static int w = 0;
w += 5;
CString str = TEXT("塑造自己的过程很疼,但是你可以得到一个更好的自己");
//获取字符串的长度(宽度)
CClientDC dc(this);
CSize size = dc.GetTextExtent(str);
//如果宽度大于字符串的宽度,重新开始
if (w > size.cx)
{
w = 0;
//重新刷新窗口
Invalidate(); //Qt 中Update()
}
int x = 50;
int y = 50;
CRect rect(x, y, x + w, y + size.cy);
//黑色写一次
dc.TextOutW(x, y, str);
//设置字体颜色
dc.SetTextColor(RGB(255, 0, 0));
// 写在这个矩形里面,覆盖在上面的黑色上面,矩形的长度多长就显示多长区域。
dc.DrawText(str, &rect, DT_LEFT);
#endif // !SHOW_THE_MESSAGEBOX
}
CView::OnTimer(nIDEvent);
}
标签:MESSAGEBOX,定时器,int,WM,dc,用法,MFC,str,CView
From: https://www.cnblogs.com/huvjie/p/17990912