Chinaunix首页 | 论坛 | 博客
  • 博客访问: 530799
  • 博文数量: 576
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 5020
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:47
文章分类

全部博文(576)

文章存档

2011年(1)

2008年(575)

我的朋友

分类:

2008-10-14 15:01:05

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

--------------------next---------------------

阅读(406) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~