简体   繁体   中英

Why are images not appearing on subitems in mfc CListView/CListCtrl?

I've created a very simple view derived from CListView , and I want to be able to show images on each column of my CListView .
To do that I know that I have to use LVS_EX_SUBITEMIMAGES and use SetItem to set the Image in the sub item, simple as that, but not working.
all the code is here

void MyListView::OnInitialUpdate()
{
    CListView::OnInitialUpdate();

    //create the list control
    GetListCtrl().ModifyStyle(0,LVS_REPORT | LVS_ALIGNLEFT | WS_BORDER | WS_TABSTOP);
    GetListCtrl().ModifyStyleEx(0,LVS_EX_SUBITEMIMAGES);
    GetListCtrl().InsertColumn(0, _T("Column 1"), LVCFMT_LEFT,60);
    GetListCtrl().InsertColumn(1, _T("Column 2"), LVCFMT_LEFT,60);

    //load the images
    CImageList *pImageList;
    pImageList = new CImageList();
    pImageList->Create( 26,26, ILC_MASK | ILC_COLOR16,2, 2);

    CBitmap bitmap;
    bitmap.LoadBitmap(  IDB_MAIN);
    pImageList->Add( &bitmap, (COLORREF)0xFFFFFF);
    bitmap.DeleteObject();

    bitmap.LoadBitmap( IDB_MAIN1);
    pImageList->Add( &bitmap, (COLORREF)0xFFFFFF);
    bitmap.DeleteObject();
    GetListCtrl().SetImageList( pImageList, LVSIL_SMALL);
    GetListCtrl().SetImageList( pImageList, LVSIL_NORMAL);
    GetListCtrl().SetImageList( pImageList, LVSIL_STATE);
    GetListCtrl().SetImageList( pImageList, LVSIL_GROUPHEADER);

    COLORREF col;
    col = RGB(240,240,240);
    GetListCtrl().SetBkColor(col);
    GetListCtrl().SetTextBkColor(col);
    GetListCtrl().SetRedraw(TRUE);

    //fill the view with 10 sample items
    for (int i=0;i<10;i++)
    {   
        CString csItem;
        csItem.Format(L"Item %d",i+1);

        GetListCtrl().InsertItem(LVIF_TEXT|LVIF_IMAGE,i,csItem,0,0,0,0);

        CString csItem2;
        csItem2.Format(L"Item2 %d",i+1);
        GetListCtrl().SetItem(i,1,LVIF_TEXT|LVIF_IMAGE,csItem2,1,0,0,0,0);
    }
}

It's really simple but I cannot get the result I want, and only the first column has images
错误的结果
I wanted that both columns had images, so the result should be like this
预期的结果,用mspaint编辑的图片=)

So what am I missing here? How can I correctly show the images also on the second column?
any help is apreciated, thanks in advance!

Do not use ModifyStyleEx(). It is for the CWnd's extendended styles. For the CListCtrl specific styles use SetExtendedStyle(). Check this discussion

Quote from the link above:

The list of extended styles for the ModifyStyleEx() all start with a WS_EX_... and presumably change a bit in the Window struct via the SetWindowLong(...). The SetExtendedStyle() method belongs to the CListCtrl in the List View and has style bits defined by LVS_EX_... Since the controls extended style is sent via a SendMessage(...), the number of extended style bits is not limited to a single word and so can presumably exceed 32.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM