65.9K
CodeProject 正在变化。 阅读更多。
Home

创建一个很酷的桌面歌词演示( 送给所有人)

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.59/5 (24投票s)

2009年12月31日

CPOL

2分钟阅读

viewsIcon

451056

downloadIcon

9659

许多流行的 MP3 播放器都有很酷的桌面歌词,这个演示使用一个简单的方法来实现它。 新年快乐!

GDIAPP

gdiplus2.jpg

引言

代码演示了如何使用 VC6 和 GDI+ 创建一个应用程序。 许多流行的 MP3 播放器都有桌面歌词。 这非常酷,我非常喜欢。 所以我决定尝试实现它。 但是我发现这真的很难。 在这里,我首先要感谢所有 CodeProject 的贡献者,因为我从 GDI+ 部分学到了很多。 在我刷新了 GDI+ 函数之后,我编写了代码。 希望它能帮助你。 这是我送给 CodeProject 成员的一份礼物。 新年快乐! 立即下载演示并享受乐趣!

代码解释

现在,让我们看一下代码。 这个演示是如何创建的。 该演示是基于一个对话框,而不是一个类。 但是,我不会过多地谈论生成对话框的基本步骤。 我假设您了解 MFC 的用法。 要了解更多信息,请阅读其他文章或 MSDN 在线文档。

步骤 1

我们使用 GDI plus 函数,所以第一件事是包含头文件和 lib 文件。

#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

我将代码片段添加到对话框的头文件中。 当然,#include "gdiplus.h" 您也可以修改您计算机上的路径。 确保路径正确,否则 VC 找不到它。

第二步

我们使用类向导添加几个消息处理函数。 WM_CREATE WM_TIMER WM_CONTENTMENU WM_DESTROY WM_LBUTTONDOWN... 更多信息,请下载资源。

我们将使用 GDI+,所以在使用其函数之前必须调用 GdiplusStartup()。 在应用程序退出之前,我们需要调用另一个函数 GdiplusShutdown()

//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
}

好的! 我们已经完成了第一项工作。

步骤 3

对话框的初始化。 我们想在 "user32.dll" 中调用 UpdateLayeredWindow(),所以我们应该获取该函数的地址。

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;
}

步骤 4

好的! 现在我们开始思考如何在桌面上绘制。 方法是

  1. 创建一个兼容的位图。
  2. 在位图上绘制。
  3. 在设备内容上绘制位图。
  4. 调用 UpdateLayeredWindows()。

我编写了一个方法来完成所有工作,就像其他人所做的那样(其他人的代码片段)。 以下是代码。 让我们看看细节

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;
}

创建对话框时,调用此方法,它就会工作。

关注点

在编写文章时,我学到了许多新的想法和函数。 希望你也会喜欢它! 您可以以任何方式使用该代码,并创建更强大的应用程序。 再次感谢您的支持! 新年快乐!

创建酷炫的桌面歌词演示(送给 CP 上所有人的礼物)- CodeProject - 代码之家
© . All rights reserved.