VC中GDI+双缓冲实现Picture控件中显示png图片
资源下载地址:http://download.csdn.net/detail/zzzili/4177527
#ifndef _CMYSTATIC
#define _CMYSTATIC
#include "StdAfx.h"
#include "gdiplus.h"
#pragma comment(lib,"gdiplus.lib")
using namespace Gdiplus;
/******************************************************************************
* Class Name : CMyImage
*
* Description : 资源图片类,继承自 Image 类
******************************************************************************/
class CMyImage:public Image
{
public:
CMyImage(UINT uID, LPCTSTR pszRsrcType)
{
// 初始化 Image 成员变量
nativeImage = NULL;
lastResult = InvalidParameter;
// 查找图片资源
HINSTANCE hInst = GetModuleHandle(NULL);
HRSRC hRsrc = FindResource(hInst, MAKEINTRESOURCE(uID), pszRsrcType);
if (!hRsrc) {
return;
}
// 加载资源并获取其指针
HGLOBAL hMemRsrc = LoadResource(hInst, hRsrc);
BYTE * pMemRsrc = (BYTE *)LockResource(hMemRsrc);
// 为流分配全局内存空间
size_t sizeRsrc = SizeofResource(hInst, hRsrc);
HGLOBAL hMemStrm = GlobalAlloc(GMEM_FIXED, sizeRsrc);
if (!hMemStrm) {
return;
}
// 拷贝图片资源到全局内存空间中
BYTE * pMemStrm = (BYTE *)GlobalLock(hMemStrm);
memcpy(pMemStrm, pMemRsrc, sizeRsrc);
// 使用图片资源流构造 Image 对象
IStream * pStrm = NULL;
CreateStreamOnHGlobal(hMemStrm, TRUE, &pStrm);
lastResult = DllExports::GdipLoadImageFromStream(pStrm, &nativeImage);
pStrm->Release();
}
};
/******************************************************************************
* Class Name : CMyCStatic
*
* Description : Picture Control 类,实现png图片的显示,继承自 CStatic 类
*
*使用方法:1.使用前初始化GDI+
2.在CXXDlg类中关联Pinture控件,用SetPngPicFrom..从文件或资源中获取要
显示的png图片,然后在CXXDlg中添加消息映射
BEGIN_MESSAGE_MAP(CMyStatic, CStatic)
//{{AFX_MSG_MAP(CFlowCtrl)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
******************************************************************************/
class CMyStatic : public CStatic
{
private:
BOOL m_isSetfile;
DWORD m_col;
CDC *m_pDC;
RECT m_rect;
CMyImage *m_image;
public:
DECLARE_MESSAGE_MAP()
private:
afx_msg void OnPaint(void)
{
if(m_isSetfile)
{
m_pDC = this->GetDC();
CDC dcMem;
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(m_pDC,m_rect.right-m_rect.left,m_rect.bottom-m_rect.top);
dcMem.CreateCompatibleDC(m_pDC);
CBitmap *pOld = dcMem.SelectObject(&bitmap);
CBrush br;
br.CreateSolidBrush(m_col);
Graphics *graphics=NULL;
graphics=Graphics::FromHDC(dcMem);
dcMem.FillRect(&m_rect, &br);//纯色覆盖
graphics->DrawImage(m_image,0,0,m_image->GetWidth(),m_image->GetHeight());
m_pDC->BitBlt(0,0,300,300,&dcMem,0,0,SRCCOPY); //双缓冲
bitmap.DeleteObject();
ReleaseDC(&dcMem);
ReleaseDC(m_pDC);
}
};
public:
void SetPngPicFromFile(CString path)
{
m_isSetfile = TRUE;
m_col = GetSysColor(COLOR_3DFACE);//获取对话框背景颜色
this->GetClientRect(&m_rect);
m_image =(CMyImage*)new Image(path.GetBuffer(0));
SetWindowPos(&wndTop,0,0, m_image->GetWidth(),m_image->GetHeight(),SWP_SHOWWINDOW);
OnPaint();
}
CMyStatic()
{
m_isSetfile = FALSE;
}
void SetPngPicFromResource(int id,CString type)
{
m_isSetfile = TRUE;
m_col = GetSysColor(COLOR_3DFACE);
this->GetClientRect(&m_rect);
m_image = new CMyImage(id,type.GetBuffer(0));
SetWindowPos(&wndTop,0,0, m_image->GetWidth(),m_image->GetHeight(),SWP_SHOWWINDOW);
OnPaint();
}
~CMyStatic()
{
}
};
#endif
文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。
原文链接:zzzili.blog.csdn.net/article/details/8265434
- 点赞
- 收藏
- 关注作者
评论(0)