天天看點

MFC用GDI+動感歌詞的制作

源代碼:

1.       插入一個對話框的資源,删除預設控件,并為對話框建立一個類,命名為ClyricDlg;

2.       在對話框的頭檔案中添加GDI+相關的頭檔案和動态庫

#define UNICODE

#ifndef ULONG_PTR

#define ULONG_PTR unsigned long*

#endif

#include "GDIPlus\\Includes\\GdiPlus.h"   ////Modify your path

using namespace Gdiplus; 

#pragma comment(lib, "GDIPlus\\Lib\\gdiplus.lib") //Modify your lib path

3.       新增公有成員變量:

int m_kind;

int cx;

BOOL UpdateDisplay(int Transparent=255);

         HINSTANCE hFuncInst ;

typedef BOOL (WINAPI *MYFUNC)(HWND,HDC,

POINT*,SIZE*,HDC,POINT*,COLORREF,BLENDFUNCTION*,DWORD);         

MYFUNC UpdateLayeredWindow;

BLENDFUNCTION m_Blend;

        HDC m_hdcMemory;

4.       新增私有成員變量:

BOOL m_bBack;

GdiplusStartupInput gdiplusStartupInput;

     ULONG_PTR           gdiplusToken;

5.       在構造函數中初始化如下成員變量:

m_bBack=false;

m_kind=cx=0;

     GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

6.       為對話框添加OnCreate函數,并編輯代碼如下:

hFuncInst = LoadLibrary("User32.DLL");

BOOL bRet=FALSE;

if(hFuncInst)

           UpdateLayeredWindow=(MYFUNC)GetProcAddress(hFuncInst, "UpdateLayeredWindow");

else

{

           AfxMessageBox("User32.dll ERROR!");

           exit(0);

}

// Initialize GDI+.

m_Blend.BlendOp=0; //theonlyBlendOpdefinedinWindows2000

m_Blend.BlendFlags=0; //nothingelseisspecial...

m_Blend.AlphaFormat=1; //...

m_Blend.SourceConstantAlpha=255;//AC_SRC_ALPHA

7.       實作其成員函數UpdataDisplay

BOOL CLyricDlg::UpdateDisplay(int Transparent)  

{  

//獲得DC,建立相容DC  

HDC hdcTemp=GetDC()->m_hDC;  

m_hdcMemory=CreateCompatibleDC(hdcTemp);  

HBITMAP hBitMap=CreateCompatibleBitmap(hdcTemp,755,350);  

SelectObject(m_hdcMemory,hBitMap);  

//設定透明度最大值為100  

if(Transparent<0||Transparent>100)      Transparent=100;  

    //将透明度參數,傳遞給BLENDFUNCTION;  

m_Blend.SourceConstantAlpha=int(Transparent*2.55);//1~255  

RECT rct;  

//擷取視窗的螢幕位置  

GetWindowRect(&rct);  

//視窗左上角的坐标  

POINT ptWinPos={rct.left,rct.top};  

    //建立2個相容DC的畫筆,Graphics類在GDI+中定義  

Graphics graph(m_hdcMemory);  

Graphics graphics(m_hdcMemory);  

    //設定平滑模式  

graphics.SetSmoothingMode(SmoothingModeAntiAlias);  

graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);  

    //設定字型,FontFamily也在GDI+中定義  

FontFamily fontFamily(L"Arial Black");  

StringFormat strformat;  

//擷取系統時間  

CTime time=CTime::GetCurrentTime();  

CString timestr=time.Format("%H-%M-%M");  

wchar_t pszbuf[][80]={{L"http://blog.csdn.net/nuptboyzhb"},  

{L"南京郵電大學鄭海波"},  

{L"I wish you will lead a  happy life!"},  

{L"[email protected]"},  

{L"NUPT"}  

};  

    //建立一個畫筆的路徑  

GraphicsPath path;  

path.AddString(pszbuf[m_kind],wcslen(pszbuf[m_kind]),&fontFamily,  

           FontStyleRegular,38,Point(10,10),&strformat);  

    //建立一支畫筆  

Pen pen(Color(155,215,215,215),3);  

    //畫筆畫出已經建立的路徑  

graphics.DrawPath(&pen,&path);  

/*畫出字型的邊緣部分*/  

for(int i=1; i<9; i+=1)  

    {  

        Pen pen(Color(62, 0, 2, 2),(float)i);  

        pen.SetLineJoin(LineJoinRound);  

        graphics.DrawPath(&pen, &path);  

    }   

SolidBrush brush(Color(25,228,228,228));  

Pen pen1(Color(155,223,223,223));  

Pen pen2(Color(55,223,223,223));    

Image image(L"1.png");  

if(m_bBack)//畫背景和圖檔  

           graphics.FillRectangle(&brush,3,5,750,90);  

           graphics.DrawRectangle(&pen1,2,6,751,91);  

           graphics.DrawRectangle(&pen2,1,5,753,93);  

           graphics.DrawImage(&image,600,5);  

}  

//建立線性漸變畫刷  

LinearGradientBrush linGrBrush(  

           Point(0,0),Point(0,90),  

           Color(255,255,255,255),  

           Color(255,30,120,195));  

LinearGradientBrush linGrBrushW(  

           Point(0,10),Point(0,60),  

           Color(15,1,1,1));  

//用線性漸變畫刷填充路徑  

graphics.FillPath(&linGrBrush,&path);  

graphics.FillPath(&linGrBrushW,&path);  

//設定視窗的風格  

DWORD dwExStyle=GetWindowLong(m_hWnd,GWL_EXSTYLE);  

if((dwExStyle&0x80000)!=0x80000)  

           SetWindowLong(m_hWnd,GWL_EXSTYLE,dwExStyle^0x80000);  

//更新視窗層  

SIZE sizeWindow={755,350};  

POINT ptSrc={0,0};  

BOOL bRet=FALSE;  

HDC hdcScreen=::GetDC (m_hWnd);  

//UpdateLayeredWindow功能是更新一個視窗的位置、大小、形狀、内容和透明度  

bRet= UpdateLayeredWindow( m_hWnd,hdcScreen,&ptWinPos,  

           &sizeWindow,m_hdcMemory,&ptSrc,0,&m_Blend,2);  

graph.ReleaseHDC(m_hdcMemory);  

::ReleaseDC(m_hWnd,hdcScreen);  

hdcScreen=NULL;  

::ReleaseDC(m_hWnd,hdcTemp);  

hdcTemp=NULL;  

DeleteObject(hBitMap);  

DeleteDC(m_hdcMemory);  

m_hdcMemory=NULL;  

return bRet;  

8.       添加OnTimer(UINT nIDEvent)消息響應函數,編輯代碼如下:

cx+=1;

if(cx>20)

           m_kind++;

           m_bBack=false;

           UpdateDisplay();

           cx=0; 

}                

if(m_kind>3)

           m_kind=0;

9.       編輯OnInitDialog()函數

// TODO: Add extra initialization here

UpdateDisplay();

      SetTimer(1,50,NULL);

繼續閱讀