思路:

1.设置定时器SetTime,在Ontime()里面确定显示矩形的大小,让后用DrawText把字铁道矩形上面;

2.

int nTextHei = dc.GetTextExtent( m_strText ).cy; // length of horizontal label文字的高度
int nTextWei = dc.GetTextExtent( m_strText ).cx; // length of horizontal label文字的宽度

获取文字高度和宽度。

3.设置矩形的大小

矩形的宽度可以每次都加1;

void CShowFontView::OnTimer(UINT nIDEvent)
{ CClientDC dc(this);
CFont font;
CRect rt;
GetClientRect( &rt );
TEXTMETRIC tm;
dc.GetTextMetrics(&tm);
static int nindex = ; m_nWithRect += ;
if( !nindex )
m_strText = GetGeci( nindex );
int nTextHei = dc.GetTextExtent( m_strText ).cy; // length of horizontal label文字的高度
int nTextWei = dc.GetTextExtent( m_strText ).cx; // length of horizontal label文字的宽度
RectShow.SetRect( , , +nTextWei+ , + nTextHei ); if( m_nWithRect > nTextWei )
{
m_nWithRect = ;
nindex ++;
m_strText = GetGeci( nindex );
Sleep();
} rectRetangle.SetRect( nTextWei + - m_nWithRect , , nTextWei + , +nTextHei );
rectColor.SetRect( , , +m_nWithRect , +nTextHei );
Sleep();
DrawText( m_strText ); CView::OnTimer(nIDEvent);
}

然后呢就把字贴出来;怎样贴呢就根据需求;

void CShowFontView::DrawText( CString strText )
{
if( strText.IsEmpty() )
{
return;
}
static int ncout = ;
CClientDC dc( this );
CRect rtclient;
GetClientRect( &rtclient );
CBitmap MemBitmap;
CDC MemDC; //首先定义一个显示设备对象
MemDC.CreateCompatibleDC( &dc );
MemBitmap.CreateCompatibleBitmap( &dc , rtclient.Width() , rtclient.Height() );
MemDC.SelectObject( &MemBitmap );
MemDC.FillSolidRect( , , rtclient.Width() , rtclient.Height() , RGB( , , ));
MemDC.SetTextColor( RGB( , , ));
MemDC.DrawText( strText , &rectRetangle , DT_LEFT );//字幕向左滚动; MemDC.SetTextColor( RGB( , , ) );//先把这段歌词全部显示
MemDC.DrawText( strText , &RectShow , DT_LEFT ); MemDC.SetTextColor( RGB( , , ));//在把要变色的再显示一遍;
MemDC.DrawText( strText , &rectColor , DT_LEFT ); dc.BitBlt( , , rtclient.Width() , rtclient.Height() , &MemDC , , , SRCCOPY );
MemBitmap.DeleteObject();
MemDC.DeleteDC();
ncout ++; }
int CShowFontView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -)
return -;
readGeci();
SetTimer( , , );
return ;
} BOOL CShowFontView::OnEraseBkgnd(CDC* pDC)
{ return CView::OnEraseBkgnd(pDC);
}
void CShowFontView::readGeci()
{
CStdioFile dlg;
if( dlg.Open("res\\text.txt" , CFile::modeRead ) )
{
CString strText;
while ( dlg.ReadString( strText ))
{
m_ListCtring.AddHead( strText );
}
dlg.Close();
}
}
CString CShowFontView::GetGeci( int nindex )
{
CString strText;
if( nindex < m_ListCtring.GetCount()&& m_ListCtring.GetCount()> )
strText = m_ListCtring.GetAt( m_ListCtring.FindIndex( m_ListCtring.GetCount() -nindex - ));
else
KillTimer();
return strText;
}
05-13 11:37