0
votes

I'm trying to create a dialog in MFC that contains a CListCtrl to display a list of items with associated images. However, the images are being displayed as blank white squares. They are there, or at least, there is a space where they should be.

I am trying to load the bitmap from a file (although I have also tried loading from a resource ID which has the same effect) and I am storing it in a CImageList. This image list is then given to the CListCtrl. I'm fairly certain that the bitmap is being correctly loaded as I have managed to load the same bitmap successfully elsewhere in my project using the same code.

I'm not sure whether this is important, but this is part of a context menu shell extension and the dialog is raised when the user clicks on one of the items in the explorer context menu. Also, I'm relatively new to MFC so apologies if I've just missed something really obvious.

The following is my code for initialising the CListCtrl in report view with two columns and one item which should have the image in the first column and some text in the second:

// Get reference to list control
CListCtrl m_list_control = (CListCtrl*)GetDlgItem(IDC_LISTCONTROL);

// Create image list
CImageList image_list;
image_list.Create(32, 32, ILC_COLOR4, 0, 3);

HANDLE hBitMap = ::LoadImage(0, L"E:\pathtomybitmap\bitmap1.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); 
CBitmap bitmap; 
bitmap.Attach((HBITMAP)hBitMap); 

image_list.Add(&bitmap, RGB(255, 0, 255));

// Add the image list to the list control 
// (LVSIL_NORMAL didn't seem to show anything at all)
m_list_control->SetImageList(&image_list, LVSIL_SMALL);


// Add columns
LVCOLUMN column;
column.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_IMAGE;
column.fmt = LVCFMT_LEFT | LVCFMT_IMAGE;
column.cx = 100;
column.pszText = (LPWSTR)&L"Image";
column.iImage = 0;

m_list_control->InsertColumn(0, &column);

m_list_control->InsertColumn(1, _T("Text"), LVCFMT_LEFT, 100);


int index = m_list_control->InsertItem(0, _T(""), 0);
m_list_control->SetItemText(0, 1, _T("My text"));

Any idea what I'm doing wrong?

1
Can't tell from the code you've posted, but what's the scope of the variable image_list? - Paul Mitchell
Ah sorry, I added the declarations here but image_list and m_list_control are member variables of the class extending CDialog - Greg
Is your bitmap 4bit? (16 colours). Try ILC_COLOR32 - GazTheDestroyer
Yeah I'm pretty sure it's 4 bit. I tried the other options anyway though to no avail - Greg
In the fourth parameter image_list.Create(32, 32, ILC_COLOR4, 0, 3); try to pass the real number of elements instead of zero. - sergiol

1 Answers

0
votes

Try changing create statement so that the size you put is smaller than the image size

image_list.Create(31, 31, ILC_COLOR4, 0, 3);

Try setting the mask argument of insert item to:

InsertItem(LVIF_TEXT | LVIF_IMAGE,...