作者:Future Studio.徐景周
版權所有:徐景周
轉載請聯系作者
你想制作出一個在桌面上透明顯示,動態唱歌的桌面小精靈麼?下面我來用我原來做的一個例子來教你如何來實現它。例子運行界面如下:
基本思路: 采用透明位圖方法在桌面上顯示位圖,定時更換位圖以實現動畫效果,再采用播放內部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;
}