我已经写了下面的代码,它试图获取一个32x32位图(通过MFC的资源系统加载),并将其转换为16x16位图,这样它们就可以用作CListCtrl的大小CImageLists。然而,当我打开CListCtrl时,所有的图标都是黑色的(无论是小视图还是大视图)。在我开始调整大小之前,在Large View中一切都工作得很好。
我做错了什么?
// Create the CImageLists
if (!m_imageListL.Create(32,32,ILC_COLOR24, 1, 1))
{
throw std::exception("Failed to create CImageList");
}
if (!m_imageListS.Create(16,16,ILC_COLOR24, 1, 1))
{
throw std::exception("Failed to create CImageList");
}
// Fill the CImageLists with items loaded from ResourceIDs
int i = 0;
for (std::vector<UINT>::iterator it = vec.begin(); it != vec.end(); it++, i++)
{
CBitmap* bmpBig = new CBitmap();
bmpBig->LoadBitmap(*it);
CDC bigDC;
bigDC.CreateCompatibleDC(m_itemList.GetDC());
bigDC.SelectObject(bmpBig);
CBitmap* bmpSmall = new CBitmap();
bmpSmall->CreateBitmap(16, 16, 1, 24, 0);
CDC smallDC;
smallDC.CreateCompatibleDC(&bigDC);
smallDC.SelectObject(bmpSmall);
smallDC.StretchBlt(0, 0, 32, 32, &bigDC, 0, 0, 16, 16, SRCCOPY);
m_imageListL.Add(bmpBig, RGB(0,0,0));
m_imageListS.Add(bmpSmall, RGB(0,0,0));
}
m_itemList.SetImageList(&m_imageListS, LVSIL_SMALL);
m_itemList.SetImageList(&m_imageListL, LVSIL_NORMAL);发布于 2010-05-07 08:20:21
在使用CBitmaps之后,请确保取消选择它们:
// Select the objects
CBitmap* ret1 = bigDC.SelectObject(bmpBig);
CBitmap* ret2 = smallDC.SelectObject(bmpSmall);
...
// Do the painting
...
// Deselect
bigDC.SelectObject(ret1);
smallDC.SelectObject(ret2);发布于 2010-05-06 15:49:01
需要为bigDC创建一个compatibleDC。即首先获取当前窗口的DC,并如下所示
bigDC.CreateCompatibleDC(&myWindowHdc);发布于 2010-05-06 16:00:41
您正在向列表中添加对本地CBitmap对象的引用。一旦退出循环,引用将不再有效。尝试在堆上创建对象。
https://stackoverflow.com/questions/2779294
复制相似问题