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

 

 

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

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

 }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值