mfc从CImageList中获取CBitmap位图对象

清泛原创

通过位图的索引号从CImageList中获取CBitmap位图对象的函数如下:

//CImageList中获取CBitmap位图对象
void GetListImage(CImageList &ImageList, CBitmap &Bitmap, int nImageIdx)
{   
	CClientDC   dcClient(NULL);

	IMAGEINFO   ImageInfo;
	ImageList.GetImageInfo(nImageIdx, &ImageInfo);
	int   nWidth  = abs(ImageInfo.rcImage.right - ImageInfo.rcImage.left);
	int   nHeight = abs(ImageInfo.rcImage.bottom - ImageInfo.rcImage.top);
	Bitmap.CreateCompatibleBitmap(&dcClient, nWidth, nHeight);
	CDC         dcMemory;
	dcMemory.CreateCompatibleDC((CDC*)NULL) ;
	CBitmap  *  pBmpOld = dcMemory.SelectObject(&Bitmap);
	dcMemory.FillSolidRect(0, 0, nWidth, nHeight, RGB(255,255,255));
	ImageList.Draw(&dcMemory, nImageIdx, CPoint(0,0), ILD_NORMAL);
	dcMemory.SelectObject(pBmpOld);
	dcMemory.DeleteDC();
	::DeleteObject( ImageInfo.hbmImage );
	::DeleteObject( ImageInfo.hbmMask );
}

调用方法:

CBitmap bitmap;
GetListImage(m_ImageList, bitmap, 0); //第一个位图

然后通过CDC的BitBlt(或者StretchBlt、TransparentBlt)绘制到界面上,效果如图:

CImageList CBitmap 位图

分享到:
评论加载中,请稍后...
创APP如搭积木 - 创意无限,梦想即时!
回到顶部