开发者

Fetching CBitmap out of CImageList

开发者 https://www.devze.com 2022-12-16 19:38 出处:网络
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:

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 );
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号