class Bitmap
{
protected:
HBITMAP m_hBitmap ;
int m_iHeight,m_iWidth;
void Free (); //析构函数 释放资源
public:
Bitmap ();
Bitmap (HDC hDC,LPSTR szFileName); //从文件读取bmp
Bitmap (HDC hDC,UINT uiResID,HINSTANCE hInstance); //从资源读取bmp
Bitmap (HDC hDC,int iWidth,int iHeight,COLORREF crColor = RGB(0,0,0)); //一个背景为黑色的 //构造函数
virtual ~Bitmap();
// the method
BOOL Create(HDC hDC,LPSTR szFileName);
BOOL Create(HDC hDC,UINT uiResID,HINSTANCE hInstance);
BOOL Create(HDC hDC,int iWidth,int iHeight,COLORREF crColor);
void Draw(HDC hDC,
int x,
int y,
BOOL bTrans=FALSE,
COLORREF crTransColor = RGB(255,0,255)); //是否绘制以索引为crTransColor色透明的图像
void DrawPart(HDC hDC, //画图像的一部分
int y, //y坐标int x, //x坐标
int Xpart, //图像中x轴坐标
int Ypart, //图像中y轴坐标
int wPart, //以图像的坐标开始,延伸的宽度
int hPart, //以图像的坐标开始,延伸的高度
BOOL bTrans,
COLORREF crTransColor=RGB(255,0,255));
int GetWidth() {return m_iWidth;};
int GetHeight() {return m_iHeight;};
};
Bitmap.cpp
#include "Bitmap.h"
//
// Construction/Destruction
//
Bitmap::Bitmap():m_hBitmap(NULL),m_iHeight(0),m_iWidth(0)
{
}
Bitmap::Bitmap(HDC hDC,LPSTR szFileName) :m_hBitmap(NULL),m_iHeight(0),m_iWidth(0)
{
Create(hDC,szFileName);
}
Bitmap::Bitmap(HDC hDC,UINT uiResID,HINSTANCE hInstance):m_hBitmap(0),m_iHeight(0),m_iWidth(0)
{
Create(hDC,uiResID,hInstance);
}
Bitmap::Bitmap(HDC hDC,int iWidth,int iHeight,COLORREF crColor):m_hBitmap(NULL),m_iHeight(0),m_iWidth(0)
{
Create(hDC,iWidth,iHeight,crColor);
}
Bitmap::~Bitmap()
{
Free();
}
void Bitmap::Free()
{
if (m_hBitmap !=NULL)
{
DeleteObject(m_hBitmap);
m_hBitmap = NULL;
}
}
BOOL Bitmap::Create(HDC hDC,LPSTR szFileName)
{
Free(); //Free any previous bitmap info
//open the bitmap file
HANDLE hFile = CreateFile(szFileName,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
return FALSE;
// Read the bitmap file header
BITMAPFILEHEADER bmfHeader;
DWORD dwByteRead;
BOOL bOK = ReadFile(hFile,&bmfHeader,sizeof(BITMAPFILEHEADER),&dwByteRead,NULL);
if ((!bOK) || (dwByteRead != sizeof(BITMAPFILEHEADER)) ||
(bmfHeader.bfType != 0x4D42)) //0x4D42= "BM ",是Bitmap文件的标志。
{
CloseHandle(hFile);
return FALSE;
}
BITMAPINFO* pBitmapInfo = (new BITMAPINFO);
if (pBitmapInfo != NULL)
{
bOK = ReadFile(hFile,pBitmapInfo,sizeof(BITMAPINFOHEADER),&dwByteRead,NULL);
if ((!bOK) || (dwByteRead != sizeof(BITMAPFILEHEADER)))
{
CloseHandle(hFile);
Free();
return FALSE;
}
//store the width and height of the bitmap
m_iWidth = (int)pBitmapInfo->bmiHeader.biHeight;
m_iHeight = (int)pBitmapInfo->bmiHeader.biHeight;
//Get a handle to bitmap and copy the image bits
PBYTE pBitmapBits;
m_hBitmap = CreateDIBSection(hDC, pBitmapInfo, DIB_RGB_COLORS,
(PVOID*)&pBitmapBits, NULL, 0);
if ((m_hBitmap != NULL) && (pBitmapBits != NULL))
{
SetFilePointer(hFile,bmfHeader.bfOffBits,NULL,FILE_BEGIN);
bOK = ReadFile(hFile,pBitmapBits,pBitmapInfo->bmiHeader.biSizeImage,&dwByteRead,NULL);
if(bOK)
return TRUE;
}
}
//Something went wrong,so cleanup everything
Free();
return FALSE;
}
BOOL Bitmap::Create(HDC hDC,UINT uiResID,HINSTANCE hInstance)
{
//Free any Previous DIB info
Free();
//Find the bitmap DIB info
HRSRC hResInfo = FindResource(hInstance,MAKEINTRESOURCE(uiResID),RT_BITMAP);
if(hResInfo == NULL)
return FALSE;
//load the bitmap resourch
HGLOBAL hMemBitmap = LoadResource(hInstance,hResInfo);
if(hMemBitmap == NULL)
return FALSE;
//lock the resourch and access the entire bitmap
PBYTE pBitmapImage = (BYTE*)LockResource(hMemBitmap);
if (pBitmapImage == NULL)
{
FreeResource(hMemBitmap);
return FALSE;
}
//store the width and height of the bitmap
BITMAPINFO* pBitmapInfo = (BITMAPINFO*)pBitmapImage;
m_iWidth = (int)pBitmapInfo->bmiHeader.biWidth;
m_iHeight = (int)pBitmapInfo->bmiHeader.biHeight;
//Get a handle to the bitmap and copy the image bits
PBYTE pBitmapBits;
m_hBitmap = CreateDIBSection(hDC,pBitmapInfo,DIB_RGB_COLORS,(PVOID*)&pBitmapBits,NULL,0);
if ((m_hBitmap != NULL) && (pBitmapBits != NULL))
{
const PBYTE pTempBits = pBitmapImage + pBitmapInfo->bmiHeader.biSize +
pBitmapInfo->bmiHeader.biClrUsed * sizeof(RGBQUAD);
CopyMemory(pBitmapBits,pTempBits,pBitmapInfo->bmiHeader.biSizeImage);
//unlock and free the bitmap graphics object
UnlockResource(hMemBitmap);
FreeResource(hMemBitmap);
return TRUE;
}
//something went wrong,so cleanup everything
UnlockResource(hMemBitmap);
FreeResource(hMemBitmap);
Free();
return FALSE;
}
BOOL Bitmap::Create(HDC hDC,int iWidth,int iHeight,COLORREF crColor)
{
//Create a bland bitmap
m_hBitmap = CreateCompatibleBitmap(hDC,iWidth,iHeight);
if(m_hBitmap == NULL)
return FALSE;
//Set the width and height
m_iHeight = iHeight;
m_iWidth = iWidth;
//Create a Memory device Context to draw on the bitmap
HDC hMemDC = CreateCompatibleDC(hDC);
//Create a solid brush to fill the bitmap
HBRUSH hBrush = CreateSolidBrush(crColor);
//Selete the bitmap into the device context
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC,m_hBitmap);
//Fill the bitmap with a solid color
RECT rcBitmap = {0,0,m_iWidth,m_iHeight};
FillRect(hMemDC,&rcBitmap,hBrush);
//CleanUp
SelectObject(hMemDC,hOldBitmap);
DeleteObject(hMemDC);
DeleteObject(hBrush);
return TRUE;
}
void Bitmap::Draw(HDC hDC,int x,int y,BOOL bTrans,COLORREF crTransColor)
{
DrawPart(hDC,x,y,0,0,GetWidth(),GetHeight(),bTrans,crTransColor);
}
void Bitmap::DrawPart(HDC hDC, int x, int y, int Xpart, int Ypart, int wPart, int hPart, BOOL bTrans, COLORREF crTransColor)
{
if (m_hBitmap != NULL)
{
HDC hMemDC = CreateCompatibleDC(hDC);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC,m_hBitmap);
if(bTrans)
TransparentBlt(hDC,x,y,wPart,hPart,hMemDC,Xpart,Ypart,wPart,hPart,crTransColor);
else
BitBlt(hDC,x,y,wPart,hPart,hMemDC,Xpart,Ypart,SRCCOPY);
SelectObject(hMemDC,hOldBitmap);
DeleteDC(hMemDC);
}
}