Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CListCtrl::CImageList Icons Disappearing when clicked

Status
Not open for further replies.

ronnyjljr

IS-IT--Management
Nov 23, 2003
249
US
Hello!

I have a list control that I load icons in to and it looks fantastic. The problem is when you click on the item, the text stays but the icon disappears. I fear that it is the way I am drawing the icons but I do not know of any other way.

Code:
[blue]
        CImageList myList;
	myList.Create(16,16,ILC_COLOR32,1,0);
	myList.Add(AfxGetApp()->LoadIcon(ICON_MED));
	myList.Add(AfxGetApp()->LoadIcon(ICON_RX));
	
        TheList.SetImageList(&myList,LVSIL_SMALL);
	CDC *pDC = TheList.GetWindowDC();
	POINT pt = {0,0};
	myList.Draw(pDC,0,pt,ILD_NORMAL);

[/blue]

Thoughts?

Thanks,
Ron

cout << "If you don't know where you want to go, we'll make sure you get taken";
 
N/M, I got it.

cout << "If you don't know where you want to go, we'll make sure you get taken";
 
hey ronnyjljr, ive got a simillar problem. how did you resolve it?

 
Outcast,

In order for it to redraw the icons you have to place an event handler for the List Ctrl's NM_CUSTOMDRAW event. Use the class wizard, right click on the listctrl and select add event handler and scroll the list until you find the NM_CUSTOMDRAW, select it, then select add and edit. The code will look something like this:

Code:
[blue]
void YourDlg::OnNMCustomdrawList(NMHDR *pNMHDR, LRESULT *pResult)
{
	NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );

    *pResult = 0;
[/blue]

Replace all that code with this code:

Code:
[blue]
	NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );

    *pResult = 0;

    // If this is the beginning of the control's paint cycle, request
    // notifications for each item.

    if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
        {
        *pResult = CDRF_NOTIFYITEMDRAW;
        }
    else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
        {
        // This is the pre-paint stage for an item.  We need to make another
        // request to be notified during the post-paint stage.

        *pResult = CDRF_NOTIFYPOSTPAINT;
        }
    else if ( CDDS_ITEMPOSTPAINT == pLVCD->nmcd.dwDrawStage )
        {
        // If this item is selected, re-draw the icon in its normal
        // color (not blended with the highlight color).

        LVITEM rItem;
        int    nItem = static_cast<int>( pLVCD->nmcd.dwItemSpec );

        // Get the image index and state of this item.  Note that we need to
        // check the selected state manually.  The docs _say_ that the
        // item's state is in pLVCD->nmcd.uItemState, but during my testing
        // it was always equal to 0x0201, which doesn't make sense, since
        // the max CDIS_* constant in commctrl.h is 0x0100.

        ZeroMemory ( &rItem, sizeof(LVITEM) );
        rItem.mask  = LVIF_IMAGE | LVIF_STATE;
        rItem.iItem = nItem;
        rItem.stateMask = LVIS_SELECTED;
            [green]//Replace PatientView with the name of your ListCtrl[/green]
        [red]PatientView.GetItem ( &rItem );[/red]

        // If this item is selected, redraw the icon with its normal colors.

        if ( rItem.state & LVIS_SELECTED )
            {
            CDC*  pDC = CDC::FromHandle ( pLVCD->nmcd.hdc );
            CRect rcIcon;

            [green]//Replace PatientView with the name of your ListCtrl[/green]
            [red]PatientView.GetItemRect ( nItem, &rcIcon, LVIR_ICON );[/red]

            [green]//Replace myList with the name of your imagelist [/green]
           [red]myList.Draw ( pDC, rItem.iImage, rcIcon.TopLeft(),
                             ILD_TRANSPARENT );[/red]

            *pResult = CDRF_SKIPDEFAULT;
            }
        }

[/blue]


And presto! All you have to do is edit three lines and it will work for every list control you have!

Hope this helps!
-Ron

cout << "If you don't know where you want to go, we'll make sure you get taken";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top