📄 transparentbitmap.cpp
字号:
// TransparentBitmap.cpp : implementation file
//
#include "stdafx.h"
#include "TransparentBitmap.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTransparentBitmap
CTransparentBitmap::CTransparentBitmap() : m_poBitmap(NULL), m_bErase(FALSE), m_dX(1.0), m_dY(1.0)
{
}
void CTransparentBitmap::Initialize(HBITMAP hBitmap, COLORREF oMaskColor, double dX, double dY)
{
m_oMaskColor = oMaskColor;
m_poBitmap = CBitmap::FromHandle(hBitmap);
BITMAP oBmp;
m_poBitmap->GetObject(sizeof(BITMAP), &oBmp);
m_iSizeX = oBmp.bmWidth;
m_iSizeY = oBmp.bmHeight;
m_dX = dX;
m_dY = dY;
//Resize to the Bitmap Dimensions
CRect oRect;
GetWindowRect(&oRect);
GetParent()->ScreenToClient(&oRect);
oRect.right = oRect.left + m_iSizeX;
oRect.bottom = oRect.top + m_iSizeY;
MoveWindow(&oRect);
//Force Repainting
InvalidateRect(NULL);
UpdateWindow();
}
//Changing the Current Displayed Bitmap
void CTransparentBitmap::ChangeBitmap(HBITMAP hBitmap)
{
m_poBitmap = CBitmap::FromHandle(hBitmap);
//Force Repainting
m_bErase = TRUE;
InvalidateRect(NULL);
UpdateWindow();
}
BEGIN_MESSAGE_MAP(CTransparentBitmap, CStatic)
//{{AFX_MSG_MAP(CTransparentBitmap)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTransparentBitmap message handlers
void CTransparentBitmap::OnPaint()
{
if(NULL == m_poBitmap)
return;
CPaintDC dc(this);
if(m_bErase == TRUE)
{
//Fill with COLOR_3DFACE
CRect oClientRect;
GetClientRect(&oClientRect);
dc.FillSolidRect(&oClientRect, GetSysColor(COLOR_3DFACE));
m_bErase = FALSE;
}
COLORREF cColor;
CBitmap oBmpAndBack, oBmpAndObject, oBmpAndMem, oBmpSave;
CBitmap *poBmpBackOld, *poBmpObjectOld, *poBmpMemOld, *poBmpSaveOld;
CDC oDcMem, oDcBack, oDcObject, oDcTemp, oDcSave;
oDcTemp.CreateCompatibleDC(&dc);
// Select the bitmap
oDcTemp.SelectObject(m_poBitmap);
// Create some DCs to hold temporary data
oDcBack.CreateCompatibleDC(&dc);
oDcObject.CreateCompatibleDC(&dc);
oDcMem.CreateCompatibleDC(&dc);
oDcSave.CreateCompatibleDC(&dc);
oBmpAndBack.CreateBitmap(m_iSizeX, m_iSizeY, 1, 1, NULL);
// Monochrome DC
oBmpAndObject.CreateBitmap(m_iSizeX, m_iSizeY, 1, 1, NULL);
// Initializes a bitmap that is compatible with the device specified by &dc
oBmpAndMem.CreateCompatibleBitmap(&dc, m_iSizeX, m_iSizeY);
oBmpSave.CreateCompatibleBitmap(&dc, m_iSizeX, m_iSizeY);
// Each DC must select a bitmap object to store pixel data
poBmpBackOld = (CBitmap*)oDcBack.SelectObject(oBmpAndBack);
poBmpObjectOld = (CBitmap*)oDcObject.SelectObject(oBmpAndObject);
poBmpMemOld = (CBitmap*)oDcMem.SelectObject(oBmpAndMem);
poBmpSaveOld = (CBitmap*)oDcSave.SelectObject(oBmpSave);
// Set proper mapping mode.
oDcTemp.SetMapMode(dc.GetMapMode());
oDcSave.BitBlt(0, 0, m_iSizeX, m_iSizeY, &oDcTemp, 0, 0, SRCCOPY);
cColor = oDcTemp.SetBkColor(m_oMaskColor);
oDcObject.BitBlt(0, 0, m_iSizeX, m_iSizeY, &oDcTemp, 0, 0, SRCCOPY);
// Set the background color of the source DC back to the original color
oDcTemp.SetBkColor(cColor);
// Create the inverse of the object mask.
oDcBack.BitBlt(0, 0, m_iSizeX, m_iSizeY, &oDcObject, 0, 0, NOTSRCCOPY);
// Copy the background of the main DC to the destination
oDcMem.BitBlt(0, 0, m_iSizeX, m_iSizeY, &dc, 0, 0, SRCCOPY);
// Mask out the places where the bitmap will be placed
oDcMem.BitBlt(0, 0, m_iSizeX, m_iSizeY, &oDcObject, 0, 0, SRCAND);
// Mask out the transparent colored pixels on the bitmap
oDcTemp.BitBlt(0, 0, m_iSizeX, m_iSizeY, &oDcBack, 0, 0, SRCAND);
// XOR the bitmap with the background on the destination DC
oDcMem.BitBlt(0, 0, m_iSizeX, m_iSizeY, &oDcTemp, 0, 0, SRCPAINT); // Combines pixels
dc.StretchBlt(0, 0, int(m_iSizeX*m_dX), int(m_iSizeY*m_dY), &oDcMem, 0, 0, m_iSizeX, m_iSizeY, SRCCOPY);
// Place the original bitmap back into the bitmap sent here
oDcTemp.BitBlt(0, 0, m_iSizeX, m_iSizeY, &oDcSave, 0, 0, SRCCOPY);
// Delete the memory DCs
oDcMem.DeleteDC();
oDcBack.DeleteDC();
oDcObject.DeleteDC();
oDcSave.DeleteDC();
oDcTemp.DeleteDC();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -