⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transparentwnd.cpp

📁 随机走跑动的小
💻 CPP
字号:
//********************************************************************************
//* TransparentWindow.CPP
//*
//* A transparent window class.
//*
//* Based on the idea of Jason Wylie
//*
//* (C) 1998 by Franz Polzer
//*
//* Visit me at:	stud1.tuwien.ac.at/~e9225140
//* Write to me:	e9225140@student.tuwien.ac.at
//********************************************************************************

/*
modified by wenyy 闻怡洋
add timer switcher to deal with current event
visit me at : http://vchelp.163.net
write to me : wyy_cq@21cn.com

*/
#include "stdafx.h"
#include "TransparentWnd.h"
#include <assert.h>
#include "littlePig.h"
#include "LittlePigDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define TIMER_SET			1
#define TIMER_WALK			2
#define TIMER_IDLE			3
#define TIMER_RUN			4
#define TIMER_HIDE			5
#define TIMER_SHOW			6
#define TIMER_WAITSHOW		7

//********************************************************************************
//* Constructor
//********************************************************************************

CTransparentWnd::CTransparentWnd()
{
	m_iAniSeq=0;
	m_fontLogo.CreateFont(13, 0, 0, 0, FW_NORMAL, 0, 0,0,0,0,0,0,0, "宋体");
}


//********************************************************************************
//* Destructor
//********************************************************************************

CTransparentWnd::~CTransparentWnd()
{
}


BEGIN_MESSAGE_MAP(CTransparentWnd, CWnd)
	//{{AFX_MSG_MAP(CTransparentWnd)
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	ON_WM_LBUTTONDOWN()
	ON_WM_CREATE()
	ON_WM_TIMER()
	ON_WM_DESTROY()
	ON_WM_RBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


//********************************************************************************
//* CreateTransparent()
//*
//* Creates the main application window transparent
//********************************************************************************
void CTransparentWnd::CreateTransparent(LPCTSTR pTitle, RECT &rect)
{
	CreateEx(	0,
		AfxRegisterWndClass(0,AfxGetApp()->LoadStandardCursor(IDC_ARROW)),
						pTitle,
						WS_POPUP | WS_SYSMENU,
						rect,
						NULL,
						NULL,
						NULL );
	Reset();
//	m_bmpDraw.LoadBitmap(MaskID);
//	SetupRegion(GetWindowDC(), MaskID);
}

//********************************************************************************
//* SetupRegion()
//*
//* Set the Window Region for transparancy outside the mask region
//********************************************************************************
void CTransparentWnd::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 CTransparentWnd::Reset(void)
{
	char szBmp[20];
	sprintf(szBmp,"VCHELP");
	m_bmpDraw.DeleteObject();
	m_bmpDraw.LoadBitmap(szBmp);
	CWindowDC dc(this);
	SetupRegion(&dc);
	Invalidate();
}

void CTransparentWnd::DoSet(void)
{
	int iSW=GetSystemMetrics(SM_CXFULLSCREEN);
	int iSH=GetSystemMetrics(SM_CYFULLSCREEN);

	KillTimer(1);
	switch(rand()%5)
	{
		case(0)://walk
		case(1):
		m_ptDest.x=rand()%(iSW - 40);
		m_ptDest.y=rand()%(iSH - 24);
		SetTimer(2,300,NULL);
		break;
		case(2):
		case(3):
		m_ptDest.x=rand()%(iSW - 40);
		m_ptDest.y=rand()%(iSH - 24);
		SetTimer(4,200,NULL);
		break;
	case(4):
		SetTimer(3,300,NULL);
		break;
	}
}

void CTransparentWnd::DoWalk(void)
{
	CRect rcW;
	GetWindowRect(rcW);
	if(m_ptDest.x> rcW.left && m_ptDest.x < rcW.right)
	{
		KillTimer(2);
		if(rand()%3==0)
		{
			m_iAniSeq=0;
			SetTimer(TIMER_HIDE,200,NULL);
		}
		else
			SetTimer(TIMER_SET,10,NULL);
		return;
	}
	CPoint ptOffset((m_ptDest.x > rcW.left)?3:-3,(m_ptDest.y > rcW.top)?3:-3);
	rcW+=ptOffset;
	MoveWindow(rcW);

	char szBmp[20];
	if(m_ptDest.x<rcW.left)
	{
		sprintf(szBmp,"WALK%d",m_iAniSeq%3+1);
		m_iLastDirection=1;
	}
	else
	{// towards right
		sprintf(szBmp,"WALKR%d",m_iAniSeq%3+1);
		m_iLastDirection=2;
	}
	m_bmpDraw.DeleteObject();
	m_bmpDraw.LoadBitmap(szBmp);
	CWindowDC dc(this);
	SetupRegion(&dc);
	Invalidate();
}
void CTransparentWnd::DoRun(void)
{
	CRect rcW;
	GetWindowRect(rcW);
	if(m_ptDest.x> rcW.left && m_ptDest.x < rcW.right)
	{
		KillTimer(4);
		SetTimer(1,10,NULL);
		return;
	}
	CPoint ptOffset((m_ptDest.x > rcW.left)?15:-15,(m_ptDest.y > rcW.top)?5:-5);
	rcW+=ptOffset;
	MoveWindow(rcW);

	char szBmp[20];
	if(m_ptDest.x<rcW.left)
	{
		sprintf(szBmp,"RUN%d",m_iAniSeq%4+1);
		m_iLastDirection=1;
	}
	else
	{// towards right
		sprintf(szBmp,"RUNR%d",m_iAniSeq%4+1);
		m_iLastDirection=2;
	}
	m_bmpDraw.DeleteObject();
	m_bmpDraw.LoadBitmap(szBmp);
	CWindowDC dc(this);
	SetupRegion(&dc);
	Invalidate();
}

void CTransparentWnd::DoIdle(void)
{
	if(m_iAniSeq%10==0)
	{
		KillTimer(3);
		SetTimer(1,10,NULL);
		return;
	}

	char szBmp[20];
	if(m_iLastDirection==1)
		sprintf(szBmp,"IDLE%d",m_iAniSeq%2+1);
	else
		sprintf(szBmp,"IDLER%d",m_iAniSeq%2+1);
	m_bmpDraw.DeleteObject();
	m_bmpDraw.LoadBitmap(szBmp);
	CWindowDC dc(this);
	SetupRegion(&dc);
	Invalidate();
}

void CTransparentWnd::DoWaitShow(void)
{
	KillTimer(TIMER_WAITSHOW);
	m_iAniSeq=0;
	SetTimer(TIMER_SHOW,200,NULL);
	char szBmp[20]="OUT1";
	m_bmpDraw.DeleteObject();
	m_bmpDraw.LoadBitmap(szBmp);
	CWindowDC dc(this);
	SetupRegion(&dc);
	Invalidate();
	ShowWindow(SW_SHOW);
}

void CTransparentWnd::DoShow(void)
{
	if(m_iAniSeq>=16)
	{
		KillTimer(TIMER_SHOW);
		SetTimer(TIMER_SET,10,NULL);
	}
	char szBmp[20];
	sprintf(szBmp,"OUT%d",m_iAniSeq%4 + 1);
	m_bmpDraw.DeleteObject();
	m_bmpDraw.LoadBitmap(szBmp);
	CWindowDC dc(this);
	SetupRegion(&dc);
	Invalidate();
	ShowWindow(SW_SHOW);
}

void CTransparentWnd::DoHide(void)
{
	if(m_iAniSeq>=18 && m_iAniSeq%18 == 0)
	{
		KillTimer(5);
		SetTimer(TIMER_WAITSHOW,3000,NULL);// wait show
		ShowWindow(SW_HIDE);
		return;
	}
	char szBmp[20];
	int iSeq=(m_iAniSeq>=2)?(m_iAniSeq-2)%4+2 : m_iAniSeq;
	if(m_iLastDirection==1)
		sprintf(szBmp,"IN%d",iSeq+1);
	else
		sprintf(szBmp,"INR%d",iSeq+1);
	m_bmpDraw.DeleteObject();
	m_bmpDraw.LoadBitmap(szBmp);
	CWindowDC dc(this);
	SetupRegion(&dc);
	Invalidate();
}
//********************************************************************************
//* CTransparentWnd message handlers
//********************************************************************************

void CTransparentWnd::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CWnd::OnLButtonDown(nFlags, point);
	PostMessage(WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(point.x,point.y));
}

int CTransparentWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	SetTimer(1,300,NULL);
	SetWindowPos(&wndTopMost,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);
	return 0;
}

BOOL CTransparentWnd::OnEraseBkgnd(CDC* pDC) 
{
	CRect	rect;
	GetWindowRect(&rect);

	CDC memDC;
	CBitmap			&cBitmap=m_bmpDraw;;
	CBitmap*		pOldMemBmp = NULL;
	CFont* pOldMemFont=NULL;

	//cBitmap.LoadBitmap(m_BitmapID);
	memDC.CreateCompatibleDC(pDC);
	pOldMemBmp = memDC.SelectObject(&cBitmap);
	//pOldMemFont = memDC.SelectObject(&m_fontLogo);
	//CTellHimApp* pApp=(CTellHimApp*)AfxGetApp();
	//int iTotal=pApp->m_arrSpeak.GetUpperBound()+1;
	//CString szT=pApp->m_arrSpeak.GetAt(m_iSpeakSeq%iTotal);
	//CRect rcD(5,2,95,52);
	//memDC.SetBkMode(TRANSPARENT);
	//memDC.DrawText(szT,rcD,DT_LEFT|DT_WORDBREAK);
	pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY);

	if (pOldMemBmp) memDC.SelectObject( pOldMemBmp );
	//if (pOldMemFont) memDC.SelectObject( pOldMemFont );
	return TRUE;
}

void CTransparentWnd::OnPaint()
{
	CPaintDC dc(this);
	// Add your drawing code here!
}


void CTransparentWnd::OnTimer(UINT nIDEvent) 
{
	
	switch(nIDEvent)
	{
	case(1)://judge
		DoSet();
		break;
	case(2)://walk
		DoWalk();
		break;
	case(3)://idle
		DoIdle();
		break;
	case(4)://run
		DoRun();
		break;
	case(5):
		DoHide();
		break;
	case(6):
		DoShow();
		break;
	case(7):
		DoWaitShow();
		break;
	default:
		break;
	}
	m_iAniSeq++;

	CWnd::OnTimer(nIDEvent);
}

void CTransparentWnd::OnDestroy() 
{
	CWnd::OnDestroy();
}

void CTransparentWnd::OnRButtonDown(UINT nFlags, CPoint point) 
{
	CWnd::OnRButtonDown(nFlags, point);
	CLittlePigDlg dlg;
	if(IDOK!=dlg.DoModal())
		DestroyWindow();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -