用paint()函数实现在对话框中显示滚动字幕!

来源:互联网 发布:数据结构与算法怎么学 编辑:程序博客网 时间:2024/06/02 20:33

 

 

//在对话框头文件中定义

 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);//获取字体结构

 }

 

原创粉丝点击