如何制作具有声音效果的动画桌面精灵

类别:VC语言 点击:0 评论:0 推荐:
  如何制作具有声音效果的动画桌面精灵

作者:徐景周

 

下载实例代码:http://www.csdn.net/cnshare/soft/7/7085.shtm

http://www.csdn.net/cnshare/soft/6/6620.shtm

 

你想制作出一个在桌面上透明显示,动态唱歌的桌面小精灵么?下面我来用我原来做的一个例子来教你如何来实现它。例子运行界面如下:

 

基本思路:采用透明位图方法在桌面上显示位图,定时更换位图以实现动画效果,再采用播放内部WAV资源文件方法来播放自带WAV文件既可(右键可关闭此程序)。

 

具体实现步骤如下:

1、  在新建的工程文件中(VC6.0)中导入一WAV文件,取名“WEST”,再导入两幅位图取名“WEST1”和“WEST2” 。

2、  新建一.h文件,取名TransparentWnd.h

代码内容如下:

#if !defined(AFX_TRANSPARENTWND_H__6508F000_5685_11D3_9001_CBBD225E6BC4__INCLUDED_)

#define AFX_TRANSPARENTWND_H__6508F000_5685_11D3_9001_CBBD225E6BC4__INCLUDED_

 

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

// TransparentWnd.h : header file

//

//////////////////////////////////////

//作者: 徐景周. 2000.12

//功能:透明位图及WAV资源播放

//////////////////////////////////////

// TransparentWnd window

 

class TransparentWnd : public CWnd

{

// Construction

public:

TransparentWnd();

 

void CreateTransparent(LPCTSTR pTitle, RECT &rect);

void SetupRegion(CDC *pDC);

void DoChange(void);

void SoundPlay(void);

 

CBitmap m_bmpDraw;

    int m_iAniSeq;

 

// Attributes

public:

 

// Operations

public:

 

// Overrides

// ClassWizard generated virtual function overrides

//{{AFX_VIRTUAL(TransparentWnd)

//}}AFX_VIRTUAL

 

// Implementation

public:

virtual ~TransparentWnd();

 

// Generated message map functions

protected:

//{{AFX_MSG(TransparentWnd)

afx_msg void OnLButtonDown(UINT nFlags, CPoint point);

afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);

afx_msg BOOL OnEraseBkgnd(CDC* pDC);

afx_msg void OnTimer(UINT nIDEvent);

afx_msg void OnRButtonDown(UINT nFlags, CPoint point);

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

 

/////////////////////////////////////////////////////////////////////////////

 

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

 

#endif // !defined(AFX_TRANSPARENTWND_H__6508F000_5685_11D3_9001_CBBD225E6BC4__INCLUDED_)

3、  新建一.cpp文件,取名:TransparentWnd.cpp

代码内容如下:

// TransparentWnd.cpp : implementation file

//////////////////////////////////////

//作者: 徐景周. 2000.12

//功能:透明位图及WAV资源播放

//////////////////////////////////////

#include "stdafx.h"

#include "West.h"

#include "TransparentWnd.h"

#include "WestDlg.h"

 

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

 

/////////////////////////////////////////////////////////////////////////////

// TransparentWnd

 

TransparentWnd::TransparentWnd()

{

m_iAniSeq=0; //图像变化初始值

}

 

TransparentWnd::~TransparentWnd()

{

}

 

BEGIN_MESSAGE_MAP(TransparentWnd, CWnd)

//{{AFX_MSG_MAP(TransparentWnd)

ON_WM_LBUTTONDOWN()

ON_WM_CREATE()

ON_WM_ERASEBKGND()

ON_WM_TIMER()

ON_WM_RBUTTONDOWN()

ON_COMMAND(IDR_HELP, OnHelp)

ON_COMMAND(IDR_EXIT, OnExit)

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

 

//********************************************************************************

//* CreateTransparent()

//*

//* Creates the main application window transparent

//********************************************************************************

void TransparentWnd::CreateTransparent(LPCTSTR pTitle, RECT &rect)

{

// 创建一个隐藏窗口

CreateEx(   0, 

     AfxRegisterWndClass(0,AfxGetApp()->LoadStandardCursor(IDC_ARROW)),

             pTitle,

             WS_POPUP ,

             rect,

             NULL,

             NULL,

             NULL);

DoChange();

}

 

//********************************************************************************

//* SetupRegion()

//*

//* Set the Window Region for transparancy outside the mask region

//********************************************************************************

void TransparentWnd::SetupRegion(CDC *pDC)

{

CDC                 memDC;

CBitmap         &cBitmap=m_bmpDraw;

CBitmap*        pOldMemBmp = NULL;

COLORREF        col,colMask;

CRect               cRect;

int                 x, y;

CRgn                wndRgn, rgnTemp;

 

GetWindowRect(&cRect);

CPoint ptOrg=cRect.TopLeft();

 

BITMAP bmInfo;

cBitmap.GetObject(sizeof(bmInfo),&bmInfo);

CRect rcNewWnd=CRect(ptOrg,CSize(bmInfo.bmWidth,bmInfo.bmHeight));

 

memDC.CreateCompatibleDC(pDC);

pOldMemBmp = memDC.SelectObject(&cBitmap);

colMask=memDC.GetPixel(0,0);

 

wndRgn.CreateRectRgn(0, 0, rcNewWnd.Width(), rcNewWnd.Height());

for(x=0; x<=rcNewWnd.Width(); x++)

{

     for(y=0; y<=rcNewWnd.Height(); y++)

     {

         col = memDC.GetPixel(x, y);

         if(col == colMask)

         {

             rgnTemp.CreateRectRgn(x, y, x+1, y+1);

             wndRgn.CombineRgn(&wndRgn, &rgnTemp, RGN_XOR);

             rgnTemp.DeleteObject();

         }

     }

}

if (pOldMemBmp) memDC.SelectObject(pOldMemBmp);

SetWindowRgn((HRGN)wndRgn, TRUE);

MoveWindow(rcNewWnd);

}

 

void TransparentWnd::DoChange(void)

{

  char szBmp[20];

 

   //不断替换图像

   sprintf(szBmp,"WEST%d",m_iAniSeq%2+1);

 

m_bmpDraw.DeleteObject();

m_bmpDraw.LoadBitmap(szBmp);

CWindowDC dc(this);

SetupRegion(&dc);

Invalidate();

 

}

 

void TransparentWnd::SoundPlay(void)

{

 //先关闭原声音播放

PlaySound("WEST",AfxGetResourceHandle(),SND_RESOURCE|SND_PURGE|SND_NODEFAULT  );

    SetTimer(2,61080,NULL); //设置播放声音时间61.08秒

 //资源WAV文件的ID须加双引号,用下API函数播放

    PlaySound("WEST",AfxGetResourceHandle(),SND_RESOURCE|SND_ASYNC|SND_NODEFAULT  );

}

 

void TransparentWnd::OnLButtonDown(UINT nFlags, CPoint point)

{

 

CWnd::OnLButtonDown(nFlags, point);

 

//实现无标题拖动

PostMessage(WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(point.x,point.y));

}

 

int TransparentWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)

{

if (CWnd::OnCreate(lpCreateStruct) == -1)

     return -1;

    SetTimer(1,1000,NULL);  //开始时的图像显示时间

    SetTimer(2,500,NULL); //启动声音播放计时器

 

SetWindowPos(&wndTopMost,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE); //窗体总在总前面  

return 0;

}

 

BOOL TransparentWnd::OnEraseBkgnd(CDC* pDC)

{

    CRect    rect;

GetWindowRect(&rect);

 

CDC memDC;

CBitmap         &cBitmap=m_bmpDraw;;

CBitmap*        pOldMemBmp = NULL;

CFont* pOldMemFont=NULL;

 

memDC.CreateCompatibleDC(pDC);

pOldMemBmp = memDC.SelectObject(&cBitmap);

pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY);

 

if (pOldMemBmp) memDC.SelectObject( pOldMemBmp );

 

return TRUE;   

//   return CWnd::OnEraseBkgnd(pDC);

}

 

void TransparentWnd::OnTimer(UINT nIDEvent)

{

   switch(nIDEvent)

{

case(1)://变换图像

     DoChange();

     break;

case(2): //播放声音

     SoundPlay();

     break;

default:

     break;

}

m_iAniSeq++;

if(m_iAniSeq==2)

      m_iAniSeq=0;

CWnd::OnTimer(nIDEvent);

}

 

void TransparentWnd::OnRButtonDown(UINT nFlags, CPoint point)

{

CWnd::OnRButtonDown(nFlags, point);

 

   DestroyWindow(); //鼠标右键点击时关闭此程序

}

 

4、  在应用类(West.cpp)代码中初始化实例涵数改为下面既可:

  BOOL CWest::InitInstance()

{

        srand(time(NULL));

   

    //一次只运行一个程序实例,如果已运行则退出

    if( FindWindow(NULL,"大话西游经典系列之--情感天地")) exit(0);

 

    TransparentWnd* pFrame = new TransparentWnd;

    m_pMainWnd = pFrame;

   

    // create and load the frame with its resources

    CRect rect(0, 0, 100, 100);

    pFrame->CreateTransparent("大话西游经典系列之--情感天地", rect);//IDB_MASK, IDB_BACK);

    pFrame->CenterWindow();  //初始显示窗体位置

    pFrame->ShowWindow(SW_SHOW);

   

    return TRUE;

}

 

其中具体实现细节,请在下载实例代码后仔细查看,~o~

 

作者EMAIL:[email protected]

未来工作室(Future Studio)

本文地址:http://com.8s8s.com/it/it2813.htm