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

📄 transparentwnd.cpp

📁 这是一个关于自动售货机的C++类设计可能有一点问题希望大家看一看就当锻炼一下自己的能力
💻 CPP
字号:
// TransparentWnd.cpp : implementation file
//
// Modified by jingzhou xu. 2000.12

#include "stdafx.h"
#include "OhMyGod.h"
#include "TransparentWnd.h"
#include "BCMenu.h"  //添加位图菜单
#include "OhMyGodDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

BCMenu popmenu;  //定义位图菜单变量
bool AnswerFlag; //答案图像显示标志位
/////////////////////////////////////////////////////////////////////////////
// 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)
	ON_COMMAND(IDR_PROBLEM, OnProblem)
	//}}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)
{
  int iSW=GetSystemMetrics(SM_CXFULLSCREEN);
  int iSH=GetSystemMetrics(SM_CYFULLSCREEN);

	CRect rcW;
	GetWindowRect(rcW);

	if(rcW.left<50) xcounter=10;
	if(rcW.top<50) ycounter=10;
	if( rcW.left <50) m_iLastDirection=1;  //向右移动
	
	if(rcW.left>iSW-50) xcounter=-10;
	if(rcW.top>iSH-50) ycounter=-10;
    if(rcW.left >iSW-50) m_iLastDirection=0; //向左移动
	
	CPoint ptOffset(xcounter,ycounter);
 	rcW+=ptOffset;
	if(!AnswerFlag)
	  MoveWindow(rcW);

	char szBmp[20];
  
	if(AnswerFlag) //显示答案图像
      sprintf(szBmp,"IDB_ANSWER");
    else if(m_iLastDirection==0) //向右移动时显示时图像
	  sprintf(szBmp,"RUNNER%d",m_iAniSeq%12+13);
	else//向左移动时显示时图像
      sprintf(szBmp,"RUNNER%d",m_iAniSeq%12+1);


	m_bmpDraw.DeleteObject();
	m_bmpDraw.LoadBitmap(szBmp);
	CWindowDC dc(this);
	SetupRegion(&dc);
	Invalidate();
}

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;
	
	xcounter=10,ycounter=10;
	m_iLastDirection=1;  //跑动的方向标志位,开始为向右
	SetTimer(1,500,NULL);  //开始时的图像显示时间
	AnswerFlag=false; //初始时不显示答案图像
	
	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;
	default:
		break;
	}
	m_iAniSeq++; 
	
	CWnd::OnTimer(nIDEvent);
}

void TransparentWnd::OnRButtonDown(UINT nFlags, CPoint point) 
{
	
	CWnd::OnRButtonDown(nFlags, point);

   //添加右键快捷图标菜单(用于下拉式菜单)
  popmenu.LoadMenu(IDR_MENU);
 
  popmenu.ModifyODMenu(NULL, IDR_HELP,IDB_HELP);
  popmenu.ModifyODMenu(NULL,IDR_EXIT,IDB_EXIT);
  popmenu.ModifyODMenu(NULL,IDR_PROBLEM,IDB_PROBLEM);
  
  ClientToScreen(&point);
  BCMenu *psub = (BCMenu *)popmenu.GetSubMenu(0); 
  psub->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,this);
  popmenu.DestroyMenu();
}

void TransparentWnd::OnHelp() 
{
	COhMyGodDlg dlg; 

	//此对话框只显示一次
  if( !FindWindow(NULL,"帮助"))
	    dlg.DoModal();	
}

void TransparentWnd::OnExit() 
{
  DestroyWindow();	
}

void TransparentWnd::OnProblem() 
{
  AnswerFlag=true;	
}

⌨️ 快捷键说明

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