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

📄 transparentwnd.cpp

📁 Visual_C++.NET精彩案例237.rar
💻 CPP
字号:
// TransparentWnd.cpp : 实现文件
//

#include "stdafx.h"
#include "Transparent.h"
#include "TransparentWnd.h"
#include "assert.h"


// CTransparentWnd

IMPLEMENT_DYNAMIC(CTransparentWnd, CWnd)
CTransparentWnd::CTransparentWnd()
{
}

CTransparentWnd::~CTransparentWnd()
{
}


BEGIN_MESSAGE_MAP(CTransparentWnd, CWnd)
	ON_WM_ERASEBKGND()
END_MESSAGE_MAP()



// CTransparentWnd 消息处理程序
//创建透明窗口
void CTransparentWnd::CreateTransparent(LPCTSTR pTitle, RECT &rect, unsigned short MaskID, unsigned short BitmapID)
{	

	CreateEx(	0,
				AfxRegisterWndClass(0),	pTitle,	WS_POPUP | WS_SYSMENU,
				rect,NULL,NULL,NULL );
	//调用SetupRegion函数
	SetupRegion(GetWindowDC(), MaskID);
	//将背景位图赋值给m_BitmapID
	m_BitmapID = BitmapID;
}
//创建不规则区域
void CTransparentWnd::SetupRegion(CDC *pDC, unsigned short MaskID)
{
	CDC				memDC;
	CBitmap			cBitmap;
	CBitmap*		pOldMemBmp = NULL;
	COLORREF		col;
	CRect			cRect;
	int				x, y;
	CRgn			wndRgn, rgnTemp;
	//获得当前矩形窗口
	GetWindowRect(&cRect);
	//加载位图资源
	cBitmap.LoadBitmap(MaskID);
	//创建与设备类型兼容的MFC类
	memDC.CreateCompatibleDC(pDC);
	//将位图加载到MFC设备环境的类中
	pOldMemBmp = memDC.SelectObject(&cBitmap);

	wndRgn.CreateRectRgn(0, 0, cRect.Width(), cRect.Height());
	//将两幅位图进行叠加
	for(x=0; x<=cRect.Width(); x++)
	{
		for(y=0; y<=cRect.Height(); y++)
		{
			col = memDC.GetPixel(x, y);
			if(col == 0)
			{
				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);
}

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

	CDC memDC;
	CBitmap			cBitmap;
	CBitmap*		pOldMemBmp = NULL;

	cBitmap.LoadBitmap(m_BitmapID);
	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;
}

⌨️ 快捷键说明

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