5 楼
iori97king [专家分:280] 发布于 2006-03-03 21:18:00
#ifndef __Bitmap_h
#define __Bitmap_h
#include "windows.h"
class Bitmap
{
protected:
HBITMAP m_bitmap;
UINT m_iWidth,m_iHeight;
public:
Bitmap(LPTSTR name);
Bitmap(HINSTANCE hInstance,UINT uiResID);
Bitmap(HDC hdc,UINT width,UINT height,COLORREF color=RGB(0,0,0));
void Draw(HDC hdc,int x,int y,bool flag=0,COLORREF color=RGB(0,0,0));
void Draw(HDC hdc,int x,int y,int left,int top,int width,int height,bool flag=0,COLORREF color=RGB(0,0,0));
UINT GetWidth();
UINT GetHeight();
};
#endif
#include "Bitmap.h"
Bitmap::Bitmap(LPTSTR name)
{
/////读取位图文件SAMPLE.BMP
BITMAP bmp;
m_bitmap=(HBITMAP)LoadImage(NULL,name,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
GetObject(m_bitmap, sizeof(BITMAP), &bmp);
m_iWidth = bmp.bmWidth;
m_iHeight = bmp.bmHeight;
}
Bitmap::Bitmap(HINSTANCE hInstance,UINT uiResID)
{
BITMAP bmp;
m_bitmap=(HBITMAP)LoadImage(hInstance, MAKEINTRESOURCE(uiResID), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
GetObject(m_bitmap, sizeof(BITMAP), &bmp);
m_iWidth = bmp.bmWidth;
m_iHeight = bmp.bmHeight;
}
Bitmap::Bitmap(HDC hdc,UINT width,UINT height,COLORREF color)
{
HDC mhdc;
m_bitmap=(HBITMAP)CreateCompatibleBitmap(hdc,width,height);
mhdc=CreateCompatibleDC(hdc);
HBITMAP oldmap=(HBITMAP)SelectObject(mhdc,m_bitmap);
HBRUSH oldbrush=(HBRUSH)SelectObject(mhdc,(HBRUSH)CreateSolidBrush(color));
Rectangle(mhdc,0,0,width,height);
SelectObject(mhdc,oldbrush);
DeleteObject(oldbrush);
m_iWidth = width;
m_iHeight = height;
}
void Bitmap::Draw(HDC hdc,int x,int y,bool flag,COLORREF color)
{
HDC mhdc;
mhdc=CreateCompatibleDC(hdc);
HBITMAP oldmap=(HBITMAP)SelectObject(mhdc,m_bitmap);
if(flag)
{
TransparentBlt(hdc,x,y,GetWidth(),GetHeight(),mhdc,0,0,GetWidth(),GetHeight(),color);
}
else
{
BitBlt(hdc,x,y,GetWidth(),GetHeight(),mhdc,0,0,SRCCOPY);
}
SelectObject(mhdc,oldmap);
DeleteDC(mhdc);
}
void Bitmap::Draw(HDC hdc,int x,int y,int left,int top,int width,int height,bool flag,COLORREF color)
{
HDC mhdc;
mhdc=CreateCompatibleDC(hdc);
void* oldmap=SelectObject(mhdc,m_bitmap);
if(flag)
{
TransparentBlt(hdc,x,y,width,height,mhdc,left,top,width,height,color);
}
else
{
StretchBlt(hdc,x,y,width,height,mhdc,left,top,width,height,SRCCOPY);
}
SelectObject(mhdc,oldmap);
DeleteDC(mhdc);
}
UINT Bitmap::GetWidth()
{
return m_iWidth;
}
UINT Bitmap::GetHeight()
{
return m_iHeight;
}