Late binding of image - I_IMAGECALLBACK


When inserting an item in the list view control, you dont have to specify the image just then. You can delay a decision on the image by using a callback item. A callback item is an item that causes the list view control to send the LVN_GETDISPINFO notification to its parent whenever it needs information to display the item. The LVN_GETDISPINFO notification can be handled by the parent window or it can be reflected back to the list view control, the latter solution being the more preferred. Here are the steps to use message reflection.
 
  • Add a message handler for =LVN_GETDISPINFO in your CListCtrl derived class. The = sign indicates that it is a reflected message. This adds a line in the message map section which looks like
    ON_NOTIFY_REFLECT(LVN_GETDISPINFO, OnGetDispInfo)
  • Implement the message handler. The message handler shown below does not show any logic to decide on the image index. The image index would most likely be a function of the item index and/or the items data (pItem->lParam).
    void CMyListCtrl::OnGetDispInfo(NMHDR* pNMHDR, LRESULT* pResult) 
    { 
            LV_ITEM *pItem = &((LV_DISPINFO*)pNMHDR)->item; 
    
            if( pItem->mask & LVIF_IMAGE ) 
            { 
                    // GetImageFor() needs to be defined and should return image number 
                    pItem->iImage = GetImageFor( pItem-> iItem); 
            } 
           
            *pResult = 0; 
    }
     
  • Insert items using the I_IMAGECALLBACK value for image index. E.g.
  • m_listctrl.InsertItem( nRow, sItemText, I_IMAGECALLBACK );
 



Comments

  • Image Icons Does not appear if I close a dialog

    Posted by Legacy on 01/09/2002 12:00am

    Originally posted by: Shiv

    Hi,

    I have a Listcontrol in a dialog. I initialize the CImageList with Icons on the Dialog's OnInitDialog(). The icons appears the first I operate on the Dialog however when I close the dialog and start it again the Icons disappear. Can anybody tell me why? What exactly is going wrong?

    Thanx
    Shiv

    Reply
  • ... and with the overlay image as well.

    Posted by Legacy on 02/02/2000 12:00am

    Originally posted by: Kurt P. Gulden

    In Oleg Pilipenkos' earlier example, just replace the line:

    SetCallbackMask(GetCallbackMask() | LVIS_STATEIMAGEMASK);

    with...

    SetCallbackMask(GetCallbackMask() | LVIS_OVERLAYMASK);

    then, in the CFancyListCtrl::OnGetDispInfo() function, replace ( or modify ) the lines:

    if ((pDispInfo->item.mask & LVIF_STATE) != 0)
    pDispInfo->item.state = INDEXTOSTATEIMAGEMASK(nState);

    with...

    if ((pDispInfo->item.mask & LVIF_STATE) != 0)
    pDispInfo->item.state = INDEXTOOVERLAYMASK(nOverlayIndex);

    Yes, I know it looks weird, but it does work. Thanks Oleg for your keen example.

    Reply
  • It is also possible to "late bind" with the state image.

    Posted by Legacy on 02/25/1999 12:00am

    Originally posted by: Oleg Pilipenko

    If you modify the CListCtrl callback mask:
    
    

    SetCallbackMask(GetCallbackMask() | LVIS_STATEIMAGEMASK);

    in the GetDispInfo you can set state image index:

    void CFancyListCtrl::OnGetDispInfo(NMHDR* pNMHDR, LRESULT* pResult)
    {
    LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
    ASSERT_POINTER(pDispInfo, LV_DISPINFO);

    static CString str;
    int nImage = 0;
    int nState = 0;
    DoGetDispInfo(GetItemData(pDispInfo->item.iItem),
    pDispInfo->item.iSubItem,
    str,
    nImage,
    nState);

    if ((pDispInfo->item.mask & LVIF_TEXT) != 0)
    pDispInfo->item.pszText = (LPTSTR)LPCTSTR(str);

    if ((pDispInfo->item.mask & LVIF_IMAGE) != 0)
    pDispInfo->item.iImage = nImage;

    if ((pDispInfo->item.mask & LVIF_STATE) != 0)
    pDispInfo->item.state = INDEXTOSTATEIMAGEMASK(nState);

    ASSERT_POINTER(pResult, LRESULT);
    *pResult = 0;
    }

    where DoGetDispInfo() does the dirty work of actually retriving item\subitem text, item image and state.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds