VC 歌词的渐变实现

代码:

//.h
#if !defined(AFX_LYRICSGRADIENTDLG_H__980CAB18_34B1_4DAC_9497_41394B911021__INCLUDED_)
#define AFX_LYRICSGRADIENTDLG_H__980CAB18_34B1_4DAC_9497_41394B911021__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/
// CLyricsGradientDlg dialog

class CLyricsGradientDlg : public CDialog
{
// Construction
public:
CLyricsGradientDlg(CWnd* pParent = NULL);	// standard constructor
// Dialog Data
//{{AFX_DATA(CLyricsGradientDlg)
enum { IDD = IDD_LYRICSGRADIENT_DIALOG };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CLyricsGradientDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CLyricsGradientDlg)
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
virtual void OnOK();
afx_msg void OnDestroy();
afx_msg void OnTimer(UINT nIDEvent);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
COLORREF m_colorFirst;
    COLORREF m_colorSecond;
UINT m_nStep;
CRgn m_rgnClip;
CString m_szLyrics[6];
CRect m_rectClient;
CFont m_font;
UINT m_nWidth;
BOOL m_bDraw;
    unsigned int m_nindex;
bool m_bEraseBk;
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_LYRICSGRADIENTDLG_H__980CAB18_34B1_4DAC_9497_41394B911021__INCLUDED_)
//.cpp
#include "stdafx.h"
#include "LyricsGradient.h"
#include "LyricsGradientDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CLyricsGradientDlg::CLyricsGradientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CLyricsGradientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CLyricsGradientDlg)
// 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);
m_nStep=5;
m_colorFirst=RGB(255,0,0);
m_colorSecond=RGB(0,255,0);
m_szLyrics[0]="不打扰是我的温柔~~~AAAAA";
m_szLyrics[1]="不知道;";
m_szLyrics[2]="不想要;";
m_szLyrics[3]="不明了;";
        m_szLyrics[4]="我的心。";
m_szLyrics[5]="明明是想靠近,却感觉到距离!";
m_font.CreateFont(30,30,
0,0,FW_THIN,
false,false,false,
GB2312_CHARSET,OUT_CHARACTER_PRECIS,
CLIP_CHARACTER_PRECIS,DEFAULT_QUALITY,
FF_MODERN,"宋体");
m_nWidth=10;
m_bDraw=TRUE;
m_nindex=0;
m_bEraseBk=false;
}

void CLyricsGradientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLyricsGradientDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLyricsGradientDlg, CDialog)
//{{AFX_MSG_MAP(CLyricsGradientDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_DESTROY()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()


BOOL CLyricsGradientDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Set the icon for this dialog.  The framework does this automatically
//  when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE);	// Set big icon
SetIcon(m_hIcon, FALSE);	// Set small icon

// TODO: Add extra initialization here

return TRUE;  // return TRUE  unless you set the focus to a control
}


void CLyricsGradientDlg::OnPaint() 
{
CPaintDC dc(this);
GetClientRect(&m_rectClient);

CFont *pOldFont=dc.SelectObject(&m_font);
CSize size=dc.GetTextExtent(m_szLyrics[m_nindex]);
    dc.SetBkMode(TRANSPARENT);
if(m_bDraw)
{
::BeginPath(dc.GetSafeHdc());
dc.TextOut((m_rectClient.Width()-size.cx)/2,(m_rectClient.Height()-size.cy)/2,m_szLyrics[m_nindex]);
::EndPath(dc.GetSafeHdc());   
CBrush brush1(m_colorSecond),*pOldBrush1;
    pOldBrush1=dc.SelectObject(&brush1);
    
CPen pen1(PS_SOLID,1,m_colorSecond);
CPen *pOldPen1=dc.SelectObject(&pen1);
//dc.StrokePath();
dc.StrokeAndFillPath();
dc.SelectObject(pOldPen1);
dc.SelectObject(pOldBrush1);
m_bDraw=FALSE;
m_bEraseBk=false;
}
::BeginPath(dc.GetSafeHdc());
dc.TextOut((m_rectClient.Width()-size.cx)/2,(m_rectClient.Height()-size.cy)/2,m_szLyrics[m_nindex]);
::EndPath(dc.GetSafeHdc());   
CBrush brush1(m_colorSecond),*pOldBrush1;
    pOldBrush1=dc.SelectObject(&brush1);
    
CPen pen1(PS_SOLID,1,m_colorSecond);
CPen *pOldPen1=dc.SelectObject(&pen1);
//dc.StrokePath();
dc.StrokeAndFillPath();
dc.SelectObject(pOldPen1);
dc.SelectObject(pOldBrush1);
m_bDraw=FALSE;
m_bEraseBk=false;
}

::BeginPath(dc.GetSafeHdc());
dc.TextOut((m_rectClient.Width()-size.cx)/2,(m_rectClient.Height()-size.cy)/2,m_szLyrics[m_nindex]);
::EndPath(dc.GetSafeHdc());
CPen pen2(PS_SOLID,1,m_colorFirst);
CBrush brush2(m_colorFirst),*pOldBrush2;
    pOldBrush2=dc.SelectObject(&brush2);

CPen *pOldPen2=dc.SelectObject(&pen2);
    m_rgnClip.CreateRectRgn(0,0,m_nWidth+=m_nStep,m_rectClient.Height());
dc.SelectClipRgn(&m_rgnClip);
//dc.StrokePath();
dc.StrokeAndFillPath();
    dc.SelectObject(pOldPen2);
dc.SelectObject(pOldBrush2);
m_rgnClip.DeleteObject();
if(m_nWidth>=(m_rectClient.Width()-size.cx)/2+size.cx)
{
   
       
   m_bDraw=TRUE;
   m_nindex++;

   if (m_nindex>=5)
   {
   m_nindex=0;
   }
   CSize size=dc.GetTextExtent(m_szLyrics[m_nindex]);
   m_nWidth=(m_rectClient.Width()-size.cx)/2;
   m_bEraseBk=TRUE;
}
dc.SelectObject(pOldFont);
if (IsIconic())
{
//CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);

}
else
{
CDialog::OnPaint();
}

HCURSOR CLyricsGradientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CLyricsGradientDlg::OnOK() 
{
// TODO: Add extra validation here
SetTimer(1,50,NULL);
//CDialog::OnOK();
}

void CLyricsGradientDlg::OnDestroy() 
{
CDialog::OnDestroy();

// TODO: Add your message handler code here
m_font.DeleteObject();
}

void CLyricsGradientDlg::OnTimer(UINT nIDEvent) 
{
// TODO: Add your message handler code here and/or call default
this->Invalidate(m_bEraseBk);
    UpdateWindow();
CDialog::OnTimer(nIDEvent);
}

效果图: 效果图

转载于:https://my.oschina.net/u/221120/blog/651230

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值