CISBitmap派生的VC++位图透明类实例
本文所述为一个由CISBitmap派生的VC++位图透明类,可以方便实现BMP图像的透明处理,主要包含两个文件,使用时主需要将其引入到你的C++工程中即可,具体的类代码如下:
CISBitmap.cpp文件代码如下:
#include <stdafx.h> #include "CISBitmap.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif CCISBitmap::CCISBitmap() { m_crBlack = 0; m_crWhite = RGB(255,255,255); } CCISBitmap::~CCISBitmap() { } int CCISBitmap::Width() { BITMAP bm; GetBitmap(&bm); return bm.bmWidth; } int CCISBitmap::Height() { BITMAP bm; GetBitmap(&bm); return bm.bmHeight; } void CCISBitmap::DrawTransparent(CDC * pDC, int x, int y, COLORREF crColour) { COLORREF crOldBack = pDC->SetBkColor(m_crWhite); COLORREF crOldText = pDC->SetTextColor(m_crBlack); CDC dcImage, dcTrans; dcImage.CreateCompatibleDC(pDC); dcTrans.CreateCompatibleDC(pDC); CBitmap* pOldBitmapImage = dcImage.SelectObject(this); CBitmap bitmapTrans; int nWidth = Width(); int nHeight = Height(); bitmapTrans.CreateBitmap(nWidth, nHeight, 1, 1, NULL); CBitmap* pOldBitmapTrans = dcTrans.SelectObject(&bitmapTrans); dcImage.SetBkColor(crColour); dcTrans.BitBlt(0, 0, nWidth, nHeight, &dcImage, 0, 0, SRCCOPY); pDC->BitBlt(x, y, nWidth, nHeight, &dcImage, 0, 0, SRCINVERT); pDC->BitBlt(x, y, nWidth, nHeight, &dcTrans, 0, 0, SRCAND); pDC->BitBlt(x, y, nWidth, nHeight, &dcImage, 0, 0, SRCINVERT); dcImage.SelectObject(pOldBitmapImage); dcTrans.SelectObject(pOldBitmapTrans); pDC->SetBkColor(crOldBack); pDC->SetTextColor(crOldText); }
CISBitmap.h文件代码如下:
//请保存为文件名:CISBitmap.h #if !defined(AFX_CISBITMAP_H__08BA6EB3_DB4C_11D1_8A89_0040052E2D91__INCLUDED_) #define AFX_CISBITMAP_H__08BA6EB3_DB4C_11D1_8A89_0040052E2D91__INCLUDED_ #if _MSC_VER >= 1000 #pragma once #endif // _MSC_VER >= 1000 class CCISBitmap : public CBitmap { public: CCISBitmap(); virtual ~CCISBitmap(); int Height(); int Width(); virtual void DrawTransparent(CDC* pDC, int x, int y, COLORREF crColour); private: COLORREF m_crBlack; COLORREF m_crWhite; }; #endif // !defined(AFX_CISBITMAP_H__08BA6EB3_DB4C_11D1_8A89_0040052E2D91__INCLUDED_)
感兴趣的朋友可以将本文所述代码放到自己的VC++工程文件中测试一下运行效果,相信会对大家的VC++项目开发起到一定的帮助作用。
赞 (0)