HOWTO: Change Icon or Bitmap of CListCtrl Item When Selected

Last reviewed: July 7, 1997
Article ID: Q141834
The information in this article applies to:
  • The Microsoft Foundation Classes (MFC) included with: - Microsoft Visual C++, 32-bit Edition, versions 4.0, 5.0

SUMMARY

This article shows how to change the icon or bitmap of a CListCtrl item when it is selected.

MORE INFORMATION

When you initialize the CListCtrl by calling CListCtrl::InsertItem(), you can pass in a value of I_IMAGECALLBACK for the index of the image. This means that the system expects you to fill in the image index when you get an LVN_GETDISPINFO notification. Inside of the handler for LVN_GETDISPINFO, you can check if the item is selected and set the appropriate image index.

Sample Code

   BEGIN_MESSAGE_MAP(CTestView, CView)
      //{{AFX_MSG_MAP(CTestView)
      ON_WM_CREATE()
      //}}AFX_MSG_MAP
      ON_NOTIFY (LVN_GETDISPINFO, IDI_LIST, OnGetDispInfo)
   END_MESSAGE_MAP()

   int CTestView::OnCreate(LPCREATESTRUCT lpCreateStruct)
   {
      if (CView::OnCreate(lpCreateStruct) == -1)
         return -1;

      // m_pImage is a CTestView's member variable of type CImageList*
      // create the CImageList with 16x15 images
      m_pImage = new CImageList();
      VERIFY (m_pImage->Create (16, 15, TRUE, 0, 1));
      CBitmap bm;
      // IDR_MAINFRAME is the toolbar bitmap in a default AppWizard
      // project.
      bm.LoadBitmap (IDR_MAINFRAME);
      // This will automatically parse the bitmap into nine images.
      m_pImage->Add (&bm, RGB (192, 192, 192));

      // m_pList is  CTestView's member variable of type CListCtrl*
      // create the CListCtrl.
      m_pList = new CListCtrl();
      VERIFY (m_pList->Create (WS_VISIBLE | WS_CHILD | LVS_REPORT |
          LVS_EDITLABELS, CRect (0, 0, 400, 400), this, IDI_LIST));
      // Create column.
      m_pList->InsertColumn (0, "Button Number", LVCFMT_LEFT, 100);
      // Associate CImageList with CListCtrl.
      m_pList->SetImageList (m_pImage, LVSIL_SMALL);

      char szTemp[10];
      for (int iCntr = 0; iCntr < 9; iCntr++)
      {
         wsprintf (szTemp, "%d", iCntr);
         m_pList->InsertItem (LVIF_IMAGE | LVIF_TEXT,
            iCntr, szTemp, 0, 0, I_IMAGECALLBACK, 0L);
      }
      return 0;
   }

   void CTestView::OnGetDispInfo (NMHDR* pnmhdr, LRESULT* pResult)
   {
      LV_DISPINFO* pdi = (LV_DISPINFO *) pnmhdr;

      // Fill in the LV_ITEM structure with the image info.
      // When an item is selected, the image is set to the first
      // image (the new bitmap on the toolbar).
      // When it is not selected, the image index is equal to the
      // item number (that is, 0=new, 1=open, 2=save, and so on.)
      if (LVIS_SELECTED == m_pList->GetItemState (pdi->item.iItem,
                         LVIS_SELECTED))
         pdi->item.iImage = 0;
      else
         pdi->item.iImage = pdi->item.iItem;
   }

   CTestView::~CTestView()
   {
      // Clean up.
      delete m_pimagelist;
      delete m_pimagelistSmall;
   }
 

	
	


Keywords : kbcode kbprg MfcUI
Technology : kbMfc
Version : 4.0 5.0
Platform : NT WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 7, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.