Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
971 views
in Technique[技术] by (71.8m points)

visual c++ - How to add Images to CListCtrl in MFC

How do you add Images to a ClistCtrl in MFC? I have tried and found that it's quite difficult.

I used CImageList to add images and then passed it to the CListCtrl. Can you provide some samples?

m_sentToCListCtrl.InsertColumn(0, _T("Item Name"), LVCFMT_LEFT,nColInterval*3);     
m_sentToCListCtrl.InsertColumn(1, _T("Value"),LVCFMT_LEFT, nColInterval);
m_sentToCListCtrl.InsertColumn(2, _T("Time"), LVCFMT_LEFT, rect.Width()-4*nColInterval);
ListView_SetExtendedListViewStyle(m_sentToCListCtrl.m_hWnd,LVS_EX_CHECKBOXES );

// Create 256 color image lists     
HIMAGELIST hSentToList =ImageList_Create(84,71, ILC_COLOR8 |ILC_MASK , 8, 1);
m_sentToImageList.Attach(hSentToList);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to add some bitmaps to your CImageList after you have created it. Something like this:

m_myImageList.Create(84,71, ILC_COLOR8 |ILC_MASK , 8, 1);

CBitmap bm;
bm.LoadBitmap(IDB_BITMAP1);
m_myImageList.Add(&bm, RGB(0, 0, 0));
bm.LoadBitmap(IDB_BITMAP2);
m_myImageList.Add(&bm, RGB(0, 0, 0));

Then, attach it to the CListCtrl:

m_sentToCListCtrl.SetImageList(&m_imageList, LVSIL_SMALL);

Finally, you add items to your CListCtrl by using the InsertItem method:

LVITEM lvItem;
lvItem.iItem = 0;
lvItem.iImage = 0;    // image index that refers to your image list
lvItem.pszText = L"Item 1";
lvItem.mask = LVIF_TEXT;

m_sentToCListCtrl.InsertItem(&lvItem);

For more info refer to CListCtrl documentation. There are examples too.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...