How to make an animated desktop elf with sound effects
Author: Xu Jing Zhou
Download instance code: http://www.9cbs.net/cnshare/soft/7/7085.shtm
http://www.9cbs.net/cnshare/soft/6/6620.shtm
Do you want to make a desktop elf that is transparent on the desktop, dynamically singing? Below I use an example of my original to teach you how to implement it. The example running interface is as follows:
Basic ideas: Use the transparent bitmap method to display the bitmap on the desktop, replace the bitmap at the timing to achieve the animation effect, and then play the internal WAV resource file method to play with the WAV file or (right click).
The specific implementation steps are as follows:
1. Import a WAV file in the new project file (VC6.0), name "West", and import two bitmap name "West1" and "West2".
2, create a new .h file, name TransparentWnd.h
The code content is as follows:
#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 overce
#ENDIF / / 100 m _ _
// TransparentWnd.h: Header File
//
//
// Author: Xu Jing Zhou 2000.12
// Function: Transparent bitmap and WAV resource play
//
// TransparentWnd Window
Class TransparentWnd: Public CWND
{
// construction
PUBLIC:
Transparentwnd ();
Void CreateTransparent (LPCTSTSTR PTIRLE, 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 Immediate Line. # Endif //! Defined (AFX_TRANSPARENTWND_H__6508F000_5685_11D3_9001_CBBD225E6BC4__included_)
3, create a new .CPP file, name: TransparentWnd.cpp
The code content is as follows:
// transparentWnd.cpp: importation file
//
// Author: Xu Jing Zhou 2000.12
// Function: Transparent bitmap and WAV resource play
//
#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; // Image change initial value
}
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 PTILE, Rect & Re)
{
// Create a hidden window
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 * POLDMEMBMPMP = NULL;
ColorRef col, colmak;
CRECT CRECT;
INT X, Y;
CRGN WNDRGN, RGNTEMP;
GetWindowRect (& CRECT);
Cpoint ptorg = crect.topleft ();
Bitmap bminfo;
CBitMap.GetObject (Sizeof (Bminfo), & Bminfo;
CRECT RCNEWND = CRECT (ptorg, csize (bminfo.bmwidth, bminfo.bmheight);
MEMDC.CREATECOMPALEDC (PDC);
Poldmembmp = MEMDC.SELECTOBJECT (& CBITMAP);
Colmask = MEMDC.GETPIXEL (0, 0);
WNDRGN.CREATERECTRGN (0, 0, rcnewwwnd.width (), rcnewwwnd.Height ());
FOR (x = 0; x <= rcnewwwnd.width (); x )
{
FOR (Y = 0; y <= rcnewwwnd.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 (RCNEWND);
}
Void TransparentWnd :: Dochange (Void)
{
Char szbmp [20];
// Continuously replace the image
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)
{
// turn off the original sound play
Playsound ("West", AFXGETRESOURHANDLE (), SND_RESOURCE | SND_PURGE | SND_NODEFAULT);
SetTimer (2,61080, null); // Setting played voice time 61.08 seconds
/ / The ID of the resource WAV file must be plus double quotes, play the API function to play
Plays ("West", AFXGETRESOTHANDLE (), SND_RESOURCE | SND_ASYNC | SND_NODEFAULT);
}
Void TransparentWnd :: ONLBUTTONDOWN (UINT NFLAGS, CPOINT POINT)
{
CWnd :: ONLBUTTONDOWN (NFLAGS, POINT);
// Realize the title Drag POSTMESSAGE (WM_NCLBUTTONDOWN, HTCAPTION, MAKELPAR (Point.x, Point.y));
}
Int TransparentWnd :: OnCreate (lpcreatestruct lpcreatestruct)
{
IF (cwnd :: oncreate (lpcreatestruct) == -1)
Return -1;
SetTimer (1,100, null); // Image display time
SetTimer (2,500, null); // Start the sound play timer
SetWindowPos (& Wndtopmost, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE); // The form is always in front
Return 0;
}
Bool TransparentWnd :: OneRaseBkGnd (CDC * PDC)
{
CRECT RECT;
GetWindowRect (& Re);
CDC MEMDC;
CBITMAP & CBITMAP = M_BMPDRAW ;;
CBITMAP * POLDMEMBMPMP = NULL;
CFONT * POLDMEMFONT = NULL;
MEMDC.CREATECOMPALEDC (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): // transform image
Dochange ();
Break;
Case (2): // Play the sound
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 (); // Mouse right Turn this program when you click
}
4. The initialization instances of the initialization instances in the application class (west.cpp) code can be changed below:
Bool cwest :: initInstance ()
{
SRAND (Time (null);
// Only run a program instance, if you have run, exit
IF (FindWindow (Null, "Westward Journey Classic Series - Emotional World")) EXIT (0);
TransparentWnd * pframe = new transparentw
m_pmainwnd = pframe;
// Create and Load The Frame with its resources
CRECT RECT (0, 0, 100, 100);
Pframe-> CreateTransparent ("Westward Journey classic series - emotional world", Rect); // idb_mask, idb_back;
Pframe-> centerWindow (); // Initial display Form Location
Pframe-> showwindow (sw_show); return true;
}
Among them, the details are detailed, please check carefully after downloading instance code, ~ o ~
Email: jingzhou_xu@163.com
Future Studio (Future Studio)