While working on a project of mine using skinned buttons, I ran into problems when I tried to use a
CImageList as the source of the state images.
CImageList does not provide any direct access to the separate images within the list. I searched, and searched and searched and search... well you get the idea... Anyway after banging my head against the keyboard for 4 days, I finally found a solution.
The GetImageFromList function
The result of my headaches is the
GetImageFromList function. If you are familiar with using CDCs then you are probably already calling this an obvious solution - but then, why are you reading this article in the first place?
Here is the function I managed to come up with (with the help of sites like this):
It takes 3 parameters, and returns nothing.
lstImages: A pointer to the
CImageList object containing all of the images.
nImage: The index of the image that is going to be extracted.
destBitmap: A pointer to the
CBitmap object that is going to contain the extracted image.
The function makes a copy of the image list, and moves the requested image to the front of that list.
It then draws the requested image into the destination bitmap.
void CMyWindowClass::GetImageFromList(CImageList *lstImages,
int nImage, CBitmap* destBitmap)
tmpList.Copy( 0, nImage, ILCF_SWAP );
CDC dcMem; dcMem.CreateCompatibleDC (GetWindowDC());
CRect rect (lastImage.rcImage);
rect.Width (), rect.Height ());
CBitmap* pBmpOld = dcMem.SelectObject (destBitmap);
tmpList.DrawIndirect (&dcMem, 0, CPoint (0, 0),
CSize (rect.Width (), rect.Height ()), CPoint (0, 0));
It looks big, but remove the comments and you have a mere 12 lines of code.
I hope that this article can save at least one person from going through what I went through to find the answer.
Good Luck, and Happy Coding!!