//在对话框头文件中定义
CString str;
int WidthX;//输出文本的水平位置
TEXTMETRIC tm;//字体结构
//在购造函数中赋初值
CScrollTextDlg::CScrollTextDlg(CWnd* pParent /*=NULL*/)
: CDialog(CScrollTextDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CScrollTextDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
str="欢迎学习使用VISUAL C++!";
WidthX=100;
}
//在初始化中定义时间器
SetTimer(1,150,NULL);//设定定时器
void CScrollTextDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CRect rect;
rect.top=45;
rect.bottom=rect.top+tm.tmHeight+10;
rect.left=WidthX-10;
rect.right=rect.left+str.GetLength()*tm.tmAveCharWidth+20;
InvalidateRect(&rect);
CRect rect2;
GetClientRect(&rect2); //获取对话框客户端大小
if(WidthX<-str.GetLength()*tm.tmAveCharWidth) //判断是否到了最左边
{
WidthX=rect2.right;
}
WidthX=WidthX-6;
UpdateWindow();
CDialog::OnTimer(nIDEvent);
}
//在onpaint()函数中实现字幕显示
else
{
// CDialog::OnPaint();
CPaintDC dc(this);
dc.SetTextColor(RGB(18,0,210));
dc.SetBkMode(TRANSPARENT);
dc.TextOut(WidthX,45,str);
dc.GetTextMetrics(&tm);//获取字体结构
}
本文介绍了一个使用Visual C++实现的简单滚动文本效果。通过设置定时器,使得文本可以在对话框上从右向左移动。该效果利用了Windows API进行绘制,并在每次定时器触发时更新文本位置。
3098

被折叠的 条评论
为什么被折叠?



