Creating a column with bitmaps on CListCtrl residing on a dialog
-
Sorry to trouble you guys, but I think I'm missing something in my understanding of the List Control boxes. I'm creating a CListCtrl box with 3 columns. The first 2 are text and the third will show bitmaps. My code goes like this:
void PopulateList ()
{
LVCOLUMN lvColumn1,lvColumn2,lvColumn3;
COLORREF rgbMask = RGB(0,0,0); //(rgb[0].rgbtRed, rgb[0].rgbtGreen, rgb[0].rgbtBlue);
// I believe I need to create a CImageList for the bitmap column, right?
CImageList ThumbList;
ThumbList.Create(MAXWIDTH, MAXHEIGHT, ILC_COLOR, 1, 1);//Create Column 1 lvColumn1.mask = LVCF\_FMT | LVCF\_TEXT | LVCF\_WIDTH; lvColumn1.fmt = Alignment; lvColumn1.cx = 120; lvColumn1.pszText = ColumnHeading\[0\]; nCol = m\_MainList.InsertColumn(0, &lvColumn1); int NextIndex = nCol; //Populate Column 1 while (!EOF) { //Read the Data from File and store in "OutputTitle" . . LVITEM lvItem; lvItem.mask = LVIF\_TEXT; lvItem.iItem = NextIndex; lvItem.iSubItem = 0; lvItem.pszText = OutputTitle; NextIndex = m\_MainList.InsertItem(&lvItem); . } ListView\_SortItemsEx( m\_MainList.m\_hWnd, CompareProc, (LPARAM)&m\_MainList ); //Create Column 2 lvColumn2.mask = LVCF\_FMT | LVCF\_TEXT | LVCF\_WIDTH; lvColumn2.fmt = Alignment; lvColumn2.cx = 120; lvColumn2.pszText = ColumnHeading\[1\]; nCol = m\_MainList.InsertColumn(1, &lvColumn2); //Populate Column 2 int NumOfEntries = m\_MainList.GetItemCount(); for(int i=0; i>NumOfEntries; i++) { //Get data based on First Column entries . . . m\_MainList.SetItemText(i,1, ); } //Create Column 3 lvColumn3.mask = LVCF\_FMT | LVCF\_TEXT | LVCF\_WIDTH; lvColumn3.fmt = Alignment; lvColumn3.cx = 240; lvColumn3.pszText = ColumnHeading\[2\]; nCol = m\_MainList.InsertColumn(2, &lvColumn3); NextIndex = nCol; //Populate Column 3 BYTE PreviewInfo\[MAXHEIGHT\]\[MAXWIDTH\]\[3\]={0}; int ImageIndex = 0; for(int i=0; i CBitmap \*ThumbNail = new CBitmap; ThumbNail->Attach(Picture ); ImageIndex = ThumbList.Add(ThumbNail,rgbMask); m\_MainList.SetImageList(&ThumbList, LVSIL\_SMALL); LVITEM lvItem; lvItem.mask = LVIF\_IMAGE; lvItem.iItem = i; // I tried lvItem.iSubItem = 0; lvItem.iImage = ImageIndex; NextIndex =
-
Sorry to trouble you guys, but I think I'm missing something in my understanding of the List Control boxes. I'm creating a CListCtrl box with 3 columns. The first 2 are text and the third will show bitmaps. My code goes like this:
void PopulateList ()
{
LVCOLUMN lvColumn1,lvColumn2,lvColumn3;
COLORREF rgbMask = RGB(0,0,0); //(rgb[0].rgbtRed, rgb[0].rgbtGreen, rgb[0].rgbtBlue);
// I believe I need to create a CImageList for the bitmap column, right?
CImageList ThumbList;
ThumbList.Create(MAXWIDTH, MAXHEIGHT, ILC_COLOR, 1, 1);//Create Column 1 lvColumn1.mask = LVCF\_FMT | LVCF\_TEXT | LVCF\_WIDTH; lvColumn1.fmt = Alignment; lvColumn1.cx = 120; lvColumn1.pszText = ColumnHeading\[0\]; nCol = m\_MainList.InsertColumn(0, &lvColumn1); int NextIndex = nCol; //Populate Column 1 while (!EOF) { //Read the Data from File and store in "OutputTitle" . . LVITEM lvItem; lvItem.mask = LVIF\_TEXT; lvItem.iItem = NextIndex; lvItem.iSubItem = 0; lvItem.pszText = OutputTitle; NextIndex = m\_MainList.InsertItem(&lvItem); . } ListView\_SortItemsEx( m\_MainList.m\_hWnd, CompareProc, (LPARAM)&m\_MainList ); //Create Column 2 lvColumn2.mask = LVCF\_FMT | LVCF\_TEXT | LVCF\_WIDTH; lvColumn2.fmt = Alignment; lvColumn2.cx = 120; lvColumn2.pszText = ColumnHeading\[1\]; nCol = m\_MainList.InsertColumn(1, &lvColumn2); //Populate Column 2 int NumOfEntries = m\_MainList.GetItemCount(); for(int i=0; i>NumOfEntries; i++) { //Get data based on First Column entries . . . m\_MainList.SetItemText(i,1, ); } //Create Column 3 lvColumn3.mask = LVCF\_FMT | LVCF\_TEXT | LVCF\_WIDTH; lvColumn3.fmt = Alignment; lvColumn3.cx = 240; lvColumn3.pszText = ColumnHeading\[2\]; nCol = m\_MainList.InsertColumn(2, &lvColumn3); NextIndex = nCol; //Populate Column 3 BYTE PreviewInfo\[MAXHEIGHT\]\[MAXWIDTH\]\[3\]={0}; int ImageIndex = 0; for(int i=0; i CBitmap \*ThumbNail = new CBitmap; ThumbNail->Attach(Picture ); ImageIndex = ThumbList.Add(ThumbNail,rgbMask); m\_MainList.SetImageList(&ThumbList, LVSIL\_SMALL); LVITEM lvItem; lvItem.mask = LVIF\_IMAGE; lvItem.iItem = i; // I tried lvItem.iSubItem = 0; lvItem.iImage = ImageIndex; NextIndex =
JJeffrey wrote:
lvItem.iSubItem = 0;
Try changing that line to
lvItem.iSubItem = nCol; // where nCol is the result of the line 'nCol = m_MainList.InsertColumn(2, &lvColumn3);'
?
-
JJeffrey wrote:
lvItem.iSubItem = 0;
Try changing that line to
lvItem.iSubItem = nCol; // where nCol is the result of the line 'nCol = m_MainList.InsertColumn(2, &lvColumn3);'
?
-
I'm an f-ing idiot at times.... First problem - you've made your image-list local to the PopulateList function - it needs to be alive as long as the list control is alive (makes sense?), so declare it at class scope with the list control variable. Second problem - the image index refers to the optional icon that can be to the left of the text in the first column. You can only put text in the rest of the columns - that's the bit I should have remembered :doh: To get images in an arbitrary column, you need to use custom-draw. Or a ready-written list control sub-class, like the ones here on CodeProject[^]. This one[^], for example, allows images in any column.
-
I'm an f-ing idiot at times.... First problem - you've made your image-list local to the PopulateList function - it needs to be alive as long as the list control is alive (makes sense?), so declare it at class scope with the list control variable. Second problem - the image index refers to the optional icon that can be to the left of the text in the first column. You can only put text in the rest of the columns - that's the bit I should have remembered :doh: To get images in an arbitrary column, you need to use custom-draw. Or a ready-written list control sub-class, like the ones here on CodeProject[^]. This one[^], for example, allows images in any column.