【问题标题】:How to Add Icon in cells of a column CListCtrl如何在列 CListCtrl 的单元格中添加图标
【发布时间】:2016-05-29 11:20:04
【问题描述】:

我有一个 CListCtrl 可以按行显示我的数据。它有两列。现在我需要添加另一个实际显示图标的列。

// set look and feel
listCtrl.SetExtendedStyle(listCtrl.GetExtendedStyle() | columnStyles);

添加行项目如下:

for (const auto dataValue : dataTable)
{
    int rowIndex = listCtrl.GetItemCount();
    listCtrl.InsertItem(rowIndex, dataValue.at(0).c_str());
    for (int colIndex = 1; colIndex < listCtrl.GetHeaderCtrl()->GetItemCount(); ++colIndex)
    {
        listCtrl.SetItemText(rowIndex, colIndex, dataValue.at(colIndex).c_str());
    }
}

我添加了一个包含行图标的新列。

我不知道如何在添加列的单元格中添加图标。考虑它是在第一列中添加的。

请提出建议。

【问题讨论】:

标签: mfc


【解决方案1】:

您不需要新列,因为图像显示在第一列的左侧(鉴于您的文本,我假设您使用的是 LVS_REPORT 样式)。

您需要一个成员图像列表,其图像数量与列表中的项目相同。所以在你列表的派生类上,添加一个成员:

CImageList m_ImageList;

然后在您列表的OnCreate 函数上:

m_ImageList.Create(32, 32, ILC_COLOR24, numberOfEnableParts, 1);
m_ImageList.SetImageCount(n);

for (int i = 0; i< n; i++)
{

    if(InsertItem(n, sText) != -1)
    {
         //set text of columns with SetItemText
         //...
         // don't know if you use a icon or a bitmap; next line I did it for the second case
         m_ImageList->Replace(n, CBitmap::FromHandle(hBmp), (CBitmap*)NULL);

        //then, associate the item with its own image of the image list

        LVITEM lvi;
        lvi.iItem= i;
        lvi.iSubItem= 0;
        lvi.mask =  LVIF_IMAGE;
        lvi.iImage= i;
        SetItem(&lvi);

    }
}


SetImageList(m_ImageList, LVSIL_SMALL);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-04
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    • 2012-08-31
    相关资源
    最近更新 更多