m_imagelist.Create(16, 16, TRUE, 2, 2); //创建16*16的图标
m_imagelist.Add(AfxGetApp()->LoadIcon(IDR_DBTYPE)); //向imagelist中加入图标
m_imagelist.Add(AfxGetApp()->LoadIcon(IDI_ICON1));
CImageList控件一般不能依靠本身使用,和其它控件一起配合使用。
imagelist还可以根据一幅大的位置,按相应的大小自动截取成小图填充在imagelist中。下面一个例子是将toolbar(资源号:uToolBar)上的图片截取后放在imagelist中
CBitmap bmp;
BITMAP bmpInfo;
bmp.LoadBitmap(uToolBar);
bmp.GetBitmap(&bmpInfo);
int nBtCountSum = pToolBar->GetToolBarCtrl().GetButtonCount();
int i, nBtCount = 0;
for(i=0; i<nBtCountSum; i++)
{
//计算button的个数
//GetItemID: The command ID of the button or separator specified by nIndex.
//Separators return ID_SEPARATOR
if(ID_SEPARATOR != pToolBar->GetItemID(i))
nBtCount++;
}
m_nToolImageHeight = bmpInfo.bmHeight;
m_ImageList.Create(uToolBar, bmpInfo.bmWidth/nBtCount, bmpInfo.bmHeight, crMask);
ImageList还可以自己绘制:
CPoint pt(rect.left, rect.top+(nItemHeight-m_nToolImageHeight)/2);
m_ImageList.Draw(pDC, lpItem->nImgIndex, pt, ILD_TRANSPARENT);
CBitmap btmap;
btmap.LoadBitmapW(IDR_MAINFRAME);
CImageList a;
//a.Create(32,64,ILC_COLOR32 | ILC_MASK, 4, 1);
a.Create(IDR_MAINFRAME,16,16,RGB(0,255,0));
// a.Add(&btmap,(CBitmap *)NULL);
IMAGEINFO imageinfo;
a.GetImageInfo(1,&imageinfo);
CDC memdc;
CDC * dc=this->GetDC();
memdc.CreateCompatibleDC(dc);
memdc.SelectGdiObject(
//memdc.SelectObject(imageinfo.hbmImage);
dc->BitBlt(0,0,16,16,&memdc,0,0,SRCCOPY);
//a.Draw(dc,0,CPoint(0,0),ILD_TRANSPARENT);
//memdc.Detach();