首页 > 代码库 > 发一个自己封装的PNG透明图片类。

发一个自己封装的PNG透明图片类。

//-----------pnglib.h#include "stdafx.h"#include "afxdialogex.h"#include "atlimage.h"class DrawImage{public:	DrawImage(UINT, LPCTSTR);	DrawImage(HWND, UINT, LPCTSTR);	~DrawImage();public:	void Draw(int, int, int, int);	void Destroy();	BOOL LoadPNG(UINT, LPCTSTR);	BOOL LoadPNG(HWND, UINT, LPCTSTR);	int GetWidth();	int GetHeight();private:	BOOL Load_Resource(CImage *, UINT, LPCTSTR);	CImage img;	HDC hDC;	HWND hWnd;};

 

//----------------pnglib.cpp#include "stdafx.h"#include "pnglib.h"DrawImage::DrawImage(UINT nResID, LPCTSTR lpTyp){	LoadPNG(nResID, lpTyp);}DrawImage::DrawImage(HWND hWndin, UINT nResID, LPCTSTR lpTyp){	LoadPNG(hWndin, nResID, lpTyp);}DrawImage::~DrawImage(){	Destroy();}int DrawImage::GetWidth(){	return img.GetWidth();}int DrawImage::GetHeight(){	return img.GetHeight();}void DrawImage::Draw(int xDest, int yDest, int WidthDest, int HeightDest){	img.Draw(hDC, xDest, yDest, WidthDest, HeightDest);}void DrawImage::Destroy(){	img.Destroy();	ReleaseDC(hWnd, hDC);}BOOL DrawImage::LoadPNG(UINT nResID, LPCTSTR lpTyp){	hDC = ::GetDC(hWnd);	return Load_Resource(&img, nResID, lpTyp);}BOOL DrawImage::LoadPNG(HWND hWndin, UINT nResID, LPCTSTR lpTyp){	hWnd = hWndin;	hDC = ::GetDC(hWnd);	return Load_Resource(&img, nResID, lpTyp);}BOOL DrawImage::Load_Resource(CImage *pImage, UINT nResID, LPCTSTR lpTyp){	if (pImage == NULL)	{		return false;	}	pImage->Destroy();	HRSRC hRsrc = http://www.mamicode.com/::FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(nResID), lpTyp);>

 用法:

把你的PNG添加到工程里面,修改资源ID为   PIC_PNG1用法1:默认在当前窗口中绘图DrawImage m_png(PIC_PNG1,"png");m_png.Draw(100,100,m_png.GetWidth(),m_png.GetHeight());//其中两个100是图片左上角的坐标用法2:可以在任何窗口中绘图HWND hWndin = 桌面句柄;//在桌面绘图,需要自己去获取DrawImage m_png(hWndin,PIC_PNG1,"png");m_png.Draw(100,100,m_png.GetWidth(),m_png.GetHeight());

 

作者:天楼桦

发一个自己封装的PNG透明图片类。