双缓冲显示字幕(卡拉ok字幕)

思路:

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 = 0;

    m_nWithRect += 1;
    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( 300 , 300 , 300+nTextWei+20 , 300+ nTextHei );
    
    

    if( m_nWithRect > nTextWei )
    {
        m_nWithRect = 10;
        nindex ++;
        m_strText = GetGeci( nindex );
        Sleep(1000);        
    }

    rectRetangle.SetRect( nTextWei +20 - m_nWithRect , 30 , nTextWei +20 , 30+nTextHei );
    rectColor.SetRect( 300 , 300 , 300+m_nWithRect , 300+nTextHei );
    Sleep(30);
    DrawText( m_strText );
    
    CView::OnTimer(nIDEvent);
}

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

void CShowFontView::DrawText( CString strText )
{
    if( strText.IsEmpty() )
    {
        return;
    }
    static int ncout = 0;
    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( 0  ,0 , rtclient.Width() , rtclient.Height() , RGB( 199 , 237 , 208));
    MemDC.SetTextColor( RGB( 255 , 0 , 0));
    MemDC.DrawText( strText , &rectRetangle , DT_LEFT );//字幕向左滚动;


    MemDC.SetTextColor( RGB( 0 , 0 , 0 ) );//先把这段歌词全部显示
    MemDC.DrawText( strText , &RectShow , DT_LEFT );
    
    MemDC.SetTextColor( RGB( 0, 0 , 255 ));//在把要变色的再显示一遍;
    MemDC.DrawText( strText , &rectColor , DT_LEFT );


    dc.BitBlt( 0 , 0 , rtclient.Width() , rtclient.Height() , &MemDC , 0, 0 , SRCCOPY );
    MemBitmap.DeleteObject();
    MemDC.DeleteDC();    
    ncout ++;
    
    
}
int CShowFontView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CView::OnCreate(lpCreateStruct) == -1)
        return -1;
    readGeci();
    SetTimer( 0 , 3 ,0 );
    return 0;
}

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()>0 )
        strText = m_ListCtring.GetAt( m_ListCtring.FindIndex( m_ListCtring.GetCount() -nindex -1  ));
    else
        KillTimer(0);
    return strText;
}

 

 

posted @ 2013-10-10 17:06  友琼  阅读(433)  评论(0编辑  收藏  举报