我使用以下代码将图像添加到CImageList:
CImageList ImageList;
ImageList.Create(64, 64, ILC_COLOR32, 0, 1);
Find image path and load image . . .
HBITMAP hBitmap = LoadBitmapFromFile(finder.GetFilePath(), NULL, 64, 64);
int nImage = ImageList.Add(CBitmap::FromHandle(hBitmap), RGB(0,0,0));
HBITMAP LoadBitmapFromFile(const char* pszFilePath, HDC hDC, long lMaxHeight, long lMaxWidth)
{
// Macro that MFC needs to manage its state within a DLL
AFX_MANAGE_STATE(AfxGetStaticModuleState())
// The item object which will be used to read in the file
CxImage image;
// Load the image from the file
if(image.Load(pszFilePath))
{
ResizeImage(image, lMaxHeight, lMaxWidth);
// Get a handle to a bitmap version of the image which the given DC can use
return image.MakeBitmap(hDC);
}
return NULL;
}
void ResizeImage(CxImage& image, long lMaxHeight, long lMaxWidth)
{
const DWORD ulImgHeight = image.GetHeight();
const DWORD ulImgWidth = image.GetWidth();
long nHeightDif = ulImgHeight - lMaxHeight;
long nWidthDif = ulImgWidth - lMaxWidth;
if(nHeightDif > 0 || nWidthDif > 0)
{
double rScaleFactor = 1.0;
if(nHeightDif > nWidthDif)
rScaleFactor = (double)lMaxHeight / (double)ulImgHeight;
else
rScaleFactor = (double)lMaxWidth / (double)ulImgWidth;
long nNewHeight = (long)(ulImgHeight * rScaleFactor);
long nNewWidth = (long)(ulImgWidth * rScaleFactor);
image.Resample2(nNewWidth, nNewHeight);
}
}
Add方法似乎添加了图像,但会重复为新图像返回相同的索引值。当图像列表显示在列表框中时,对于添加并返回相同索引的所有图像,将显示相同的图像,但列表中图像下方显示的文件名是正确的。
我已经尝试了几件事来确定wnat正在发生但都无济于事。此问题似乎与图像大小或形状(h。与w。)无关。我已经通过在图片控件上显示图像来验证正确的图像是否附加到HBITMAP上以加载图像,但列表控件上的图像不正确。
如果我使用以下方式添加图像:
int nImage = ImageList.Add(CBitmap::FromHandle(hBitmap), (CBitmap *)NULL);
没有加载任何重复的图像。而不是重复索引Add返回-1。我找不到CImageList上的错误的文档。有谁知道可能会发生什么?