Fetching CBitmap out of CImageList
I inserted bitmaps in CImageList in one Function and needed to change some of the images later in another function. But I am unable to extract the CBitmap. The code goes something like this:
CBitmap GetIn开发者_开发问答dividualBitmap(CImageList oImgList, int nBmpNo)
{
IMAGEINFO imgInfo;
imagelist.GetImageInfo(index,imgInfo);
CBitmap bmp;
bmp.FromHandle(imgInfo.hbmImage);
return bmp;
}
However all I get is a black screen. Could anyone please point out where I am going wrong?
Ok there are a number of errors in your code
1)You are passing the Image list by object which means it will copy it across. Passing it by reference is a far better plan.
2) You are not passing a pointer to the IMAGEINFO struct into the GetImageInfo.
3) You misunderstand how "FromHandle" works. FromHandle is a static function that returns a pointer to a Bitmap. In your code you are calling the function and then ignoring the CBitmap* returned and returning a copy of your newly constructed object (ie it contains nothing) which results in your black screen.
Taking all those into account you should have code that looks like this:
CBitmap* GetIndividualBitmap(CImageList& oImgList, int nBmpNo)
{
IMAGEINFO imgInfo;
oImgList.GetImageInfo( nBmpNo, &imgInfo );
return CBitmap::FromHandle( imgInfo.hbmImage );
}
精彩评论