Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / Visual C++ 9.0

To Create A COOL Desktop Lyrics Demo (A Gift To All People On CP)

Rate me:
Please Sign up or sign in to vote.
4.59/5 (27 votes)
1 Jan 2010CPOL2 min read 446.2K   9.6K   80   49
Many popular MP3 players have cool desktop lyrics, this demo uses a simple method to realize it. Happy new year!
GDIAPP

gdiplus2.jpg

Introduction

The code demonstrates how to use GDI plus create an application with VC6. Many popular MP3 players have desktop lyrics. It's really cool, I like it very much. So I decided tp try to realize it. But I found it's really hard. Here,I should thank all the CodeProject contributors first because I learned much from GDI+ section. After I refreshed the GDI+ functions, I wrote the code. Hope it will help you. This is a gift I send to the members on CodeProject. Happy new year! Download the demo right now and have fun!

Explanation of the Code

Now, let's have a look at the code. How was this demo created. The demo was based on a dialog, not a class. But, I will not talk more about the basic steps of generation of dialog. I suppose you know about the usage of MFC. To look for more, read other articles or MSDN online.

Step 1

We use GDI plus functions, so the first thing is include the header and lib files.

C++
#define UNICODE
#ifndef ULONG_PTR
#define ULONG_PTR unsigned long*
#endif
#include "gdiplus.h"   		////Modify your path
using namespace Gdiplus;   		//Using the namespace of GDI+
#pragma comment(lib, "gdiplus.lib") 	//Modify your lib path

I add the snippets into the dialog's header. Of course, #include "gdiplus.h" you can also modify your own path of your computer. Be sure the path is correct, or the VC can't find it.

Step 2

We use the class wizard to add several messages deal functions. WM_CREATE WM_TIMER WM_CONTENTMENU WM_DESTROY WM_LBUTTONDOWN... for more, download the resources.

We'll use GDI+, so GdiplusStartup() must be invoked before we use its functions. Before the application exits, we need to invoke another function GdiplusShutdown().

C++
//The snippets put into header file
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;

//Following code put into construction method
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

//Invoke function to clear
void CGDIAPPDlg::OnDestroy() 
{
 CDialog::OnDestroy();
 GdiplusShutdown(gdiplusToken);
 // TODO: Add your message handler code here
}

Okay! We have finished doing the first job.

Step 3

Initializations for the dialog. We want to invoke the UpdateLayeredWindow() in the "user32.dll", so we should get the address of the function.

C++
int CGDIAPPDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
 if (CDialog::OnCreate(lpCreateStruct) == -1)
  return -1;
 
    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
 
 return 0;
}

Step 4

Okay! Now we begin to think how to draw on desktop. The method is:

  1. Create a compatible bitmap.
  2. Draw on the bitmap.
  3. Draw bitmap on the device content.
  4. Invoke the UpdateLayeredWindows().

I write a method to do all the jobs like others do (others' snippets). The following is the code. Let's have a look into the details:

C++
BOOL CGDIAPPDlg::UpdateDisplay(int Transparent)
{
 HDC hdcTemp=GetDC()->m_hDC;
 m_hdcMemory=CreateCompatibleDC(hdcTemp);   //Create the compatibleDC
 HBITMAP hBitMap=CreateCompatibleBitmap(hdcTemp,755,350); //Create the compatible bitmap
 SelectObject(m_hdcMemory,hBitMap);//Select bitmap into device content
 if(Transparent<0||Transparent>100) Transparent=100;

 m_Blend.SourceConstantAlpha=int(Transparent*2.55);//1~255
 HDC hdcScreen=::GetDC (m_hWnd);
 RECT rct;
 GetWindowRect(&rct);
 POINT ptWinPos={rct.left,rct.top};

 Graphics graph(m_hdcMemory);

 Graphics graphics(m_hdcMemory);

 graphics.SetSmoothingMode(SmoothingModeAntiAlias);
 graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);

 FontFamily fontFamily(L"Arial Black");
 StringFormat strformat;

//Here is the text we want to show on the desktop, we can read it from a file.
//I'll modify it later.
 wchar_t pszbuf[][80]={{L"Hello everyone!"},
 {L"Happy New Year!"},
 {L"I wish you will lead a  happy life!"},
 {L"Love you!"},
 {L"Thanks all the people on CP!"}
 };

//The following code is used to draw the text on dc
 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);

 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(255,255,255,255),
  Color(15,1,1,1));
/*Draw shade ,I study from others. */
   for(int i=1; i<9; i+=1)
    {
        Pen pen(Color(62, 0, 2, 2), 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"ly.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);
   }

 graphics.FillPath(&linGrBrush,&path);
 graphics.FillPath(&linGrBrushW,&path);
 
 SIZE sizeWindow={755,350};
 POINT ptSrc={0,0};
 DWORD dwExStyle=GetWindowLong(m_hWnd,GWL_EXSTYLE);	//Don't forget to set 
						//the dialog's extent style.
 if((dwExStyle&0x80000)!=0x80000)
  SetWindowLong(m_hWnd,GWL_EXSTYLE,dwExStyle^0x80000);

 BOOL bRet=FALSE;
 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;
}

When the dialog is created, call this method and it does work.

Points of Interest

I learned many new ideas and functions while writing the article. Hope you will also like it! You can use the code in any way, and to create much more stronger applications. Thanks for your support again! Happy new year!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
China China
Secret..

Comments and Discussions

 
GeneralRe: hei Pin
Aric Wang1-Jan-10 17:45
Aric Wang1-Jan-10 17:45 
GeneralNice article Pin
JunWang1631-Jan-10 3:18
JunWang1631-Jan-10 3:18 
GeneralMy vote of 1 Pin
lusores1-Jan-10 0:32
lusores1-Jan-10 0:32 
GeneralRe: My vote of 1 Pin
Aric Wang1-Jan-10 3:07
Aric Wang1-Jan-10 3:07 
GeneralRe: My vote of 1 Pin
lusores1-Jan-10 10:16
lusores1-Jan-10 10:16 
GeneralRe: My vote of 1 Pin
enhzflep1-Jan-10 13:23
enhzflep1-Jan-10 13:23 
GeneralRe: My vote of 1 Pin
lusores1-Jan-10 14:02
lusores1-Jan-10 14:02 
GeneralRe: My vote of 1 Pin
Aric Wang1-Jan-10 14:46
Aric Wang1-Jan-10 14:46 
Yeah!Sorry,I havn't test the app on Win 7 or vista system.I only test on xp.You are right,that's my fault.I found that too.When my demo run in win7,it has a frame borders.If you try to run in xp,it will not.

I am not a genius, but shed more sweat!

GeneralRe: My vote of 1 Pin
lusores1-Jan-10 15:00
lusores1-Jan-10 15:00 
GeneralRe: My vote of 1 Pin
Aric Wang1-Jan-10 15:14
Aric Wang1-Jan-10 15:14 
GeneralRe: My vote of 1 Pin
lusores1-Jan-10 15:16
lusores1-Jan-10 15:16 
GeneralRe: My vote of 1 Pin
Aric Wang1-Jan-10 15:30
Aric Wang1-Jan-10 15:30 
GeneralWhat is the window border fix ? Pin
ETA7-Jan-10 21:08
ETA7-Jan-10 21:08 
GeneralRe: What is the window border fix ? Pin
Aric Wang8-Jan-10 23:08
Aric Wang8-Jan-10 23:08 
GeneralRe: What is the window border fix ? Pin
Aric Wang9-Jan-10 0:08
Aric Wang9-Jan-10 0:08 
GeneralRe: What is the window border fix ? Pin
ETA9-Jan-10 1:32
ETA9-Jan-10 1:32 
GeneralRe: What is the window border fix ? Pin
Aric Wang9-Jan-10 1:46
Aric Wang9-Jan-10 1:46 
GeneralRe: My vote of 1 Pin
Aric Wang1-Jan-10 3:10
Aric Wang1-Jan-10 3:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.