CISBitmap derived VC++ bitmap transparent class instance

  • 2020-04-02 02:32:10
  • OfStack

This paper is described as a VC++ bitmap transparent class derived from CISBitmap, can facilitate the transparent processing of BMP image, mainly contains two files, the use of the main need to introduce it into your C++ project can be, the specific class code is as follows:

The file code of cisbitmap.cpp is as follows:


#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);
}

The file code of cisbitmap. h is as follows:


//Please save as the file name: 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_)

Interested friends can put the code described in this article into their own VC++ project files to test the running effect, I believe it will play a certain role in the VC++ project development.


Related articles: