发布网友 发布时间:2022-04-23 10:31
共1个回答
热心网友 时间:2023-10-11 13:05
/***************************************************
步骤三:将内存DC图拷贝到屏幕DC上进行显示。
1.BitBlt会将内存里的图片块拷贝到一个指定大小位置的屏幕(也可以说是窗口)矩形区域。
2.BitBlt前4个参数nXDest,nYDest,nWidth,nHeight.确定屏幕矩形区域的大小。
3.BitBlt后面2个坐标参数x,y确定从内存图片块的哪个位置开始拷贝到屏幕DC上
**************************************************/
BitBlt(hScrDC,0,0,ClientWidth,ClientHeight,hMemDC,0,0,SRCCOPY);
/****************************************************************************
CDrawBitmap封装了一些画图片的函数。
调用如下:
CDrawBitmap g_bmp1("D:\\My Documents\\My Pictures\\风景图片\\4244123431.bmp");
g_bmp1.Draw(m_hWnd);
CDrawBitmap g_bmp2("D:\\My Documents\\My Pictures\\风景图片\\plane.bmp");
g_bmp2.DrawTransparent(m_hWnd);
CDrawBitmap g_bmp3("D:\\My Documents\\My Pictures\\风景图片\\jzg2.bmp");
g_bmp3.DrawTransparent(m_hWnd);
****************************************************************************/
//使用TransparentBlt需要添加 #pragma comment(lib,"msimg32.lib")
#pragma comment(lib,"msimg32.lib")
class CDrawBitmap
{
private:
HBITMAP m_bitmap;
int m_bitmap_cx;
int m_bitmap_cy;
public:
CDrawBitmap(){}
CDrawBitmap(TCHAR * PicturePath){LoadPicture(PicturePath);}
CDrawBitmap(WORD ResourceID){LoadResource(ResourceID);}
~CDrawBitmap(){ ::DeleteObject(m_bitmap);}
bool LoadPicture(TCHAR * PicturePath)
{
m_bitmap=(HBITMAP)LoadImage(NULL,//应用程序实例
PicturePath,//路径
IMAGE_BITMAP,
0,//如果不置0,则此参数指定m_bitmap的宽(位图被拉伸)
0,//如果不置0,则此参数指定m_bitmap的高(位图被拉伸)
LR_LOADFROMFILE|LR_CREATEDIBSECTION|LR_DEFAULTSIZE);
if (!m_bitmap)
return false;
BITMAP bmp;
GetObject(m_bitmap,sizeof(BITMAP),(LPSTR)&bmp);
m_bitmap_cx=bmp.bmWidth;
m_bitmap_cy=bmp.bmHeight;
return true;
}
bool LoadResource(WORD ResourceID)
{
m_bitmap=LoadBitmap(GetMoleHandle(NULL),MAKEINTRESOURCE (ResourceID) );
if (!m_bitmap)
return false;
BITMAP bmp;
GetObject(m_bitmap,sizeof(BITMAP),(LPSTR)&bmp);
m_bitmap_cx=bmp.bmWidth;
m_bitmap_cy=bmp.bmHeight;
return true;
}
void Draw(HWND hWnd=NULL,int x=0,int y=0,int width=100,int height=100)
{
HDC hScrDC=GetDC(hWnd);//创建屏幕DC
Draw(hScrDC,x,y,width,height);
::ReleaseDC(hWnd,hScrDC) ;//释放屏幕DC
}
void Draw(HDC hScrDC,int x=0,int y=0,int width=100,int height=100)
{
HDC hMemDC=CreateCompatibleDC(hScrDC);//创建内存DC
::SelectObject(hMemDC,m_bitmap);//把位图选进内存DC
BitBlt(hScrDC,x,y,width,height,hMemDC,0,0,SRCCOPY);//内存DC映射到屏幕DC
::DeleteDC(hMemDC) ;//删除内存DC
}
void DrawTransparent(HWND hWnd=NULL,int x=0,int y=0)
{
HDC hScrDC=GetDC(hWnd);//创建屏幕DC
DrawTransparent(hScrDC,x,y);
::ReleaseDC(hWnd,hScrDC) ;//释放屏幕DC
}
//不进行拉伸处理,如需要,请自行修改代码TransparentBlt中的参数
//使用TransparentBlt需要添加 #pragma comment(lib,"msimg32.lib")
void DrawTransparent(HDC hScrDC,int x=0,int y=0,UINT crTransparent=RGB(255,255,255))
{
HDC hMemDC=CreateCompatibleDC(hScrDC);//创建内存DC
::SelectObject(hMemDC,m_bitmap);//把位图选进内存DC
::TransparentBlt(hScrDC,x,y,m_bitmap_cx,m_bitmap_cy,hMemDC,0,0,m_bitmap_cx,m_bitmap_cy,crTransparent);
::DeleteDC(hMemDC) ;//删除内存DC
}
void DrawStretch(HWND hWnd=NULL,int x=0,int y=0,int width=100,int height=100)
{
HDC hScrDC=GetDC(hWnd);//创建屏幕DC
DrawStretch(hScrDC,x,y,width,height);
::ReleaseDC(hWnd,hScrDC) ;//释放屏幕DC
}
void DrawStretch(HDC hScrDC=NULL,int x=0,int y=0,int width=100,int height=100)
{
HDC hMemDC=CreateCompatibleDC(hScrDC);//创建内存DC
::SelectObject(hMemDC,m_bitmap);//把位图选进内存DC
SetStretchBltMode(hScrDC,COLORONCOLOR);//一定要先调用这个
StretchBlt(hScrDC,x,y,width,height,hMemDC,0,0,m_bitmap_cx,m_bitmap_cy,SRCCOPY);//内存DC映射到屏幕DC
::DeleteDC(hMemDC) ;//删除内存DC
}
};
void DrawBitmap(HBITMAP m_bitmap,HDC hScrDC,int x=0,int y=0,int width=100,int height=100)
{
HDC hMemDC=CreateCompatibleDC(hScrDC);//创建内存DC
::SelectObject(hMemDC,m_bitmap);//把位图选进内存DC
BitBlt(hScrDC,x,y,width,height,hMemDC,0,0,SRCCOPY);//内存DC映射到屏幕DC
::DeleteDC(hMemDC) ;//删除内存DC
}
void DrawBitmap(HBITMAP m_bitmap,HWND hWnd=NULL,int x=0,int y=0,int width=100,int height=100)
{
HDC hScrDC=GetDC(hWnd);//创建屏幕DC
DrawBitmap(m_bitmap,hScrDC,x,y,width,height);
::ReleaseDC(hWnd,hScrDC) ;//释放屏幕DC
}