CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2005
    Posts
    445

    Adding columns to CListCtrl

    Hi all,

    I'm adding 12 columns to a list control as follows:

    Code:
                    LVCOLUMN lvColumn;
    	int i=0; 
    
                    lvColumn.iImage = 1; 
    	lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_IMAGE | LVCF_WIDTH;
    	lvColumn.fmt = LVCFMT_BITMAP_ON_RIGHT | LVCF_IMAGE;
    	lvColumn.cx = 100;
    	lvColumn.pszText = "Column 1";
    	if ( m_ListCtrlCallsHistory.InsertColumn( i++, &lvColumn ) == -1 )
    	    return FALSE;
    
    
    	lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_IMAGE | LVCF_WIDTH;
    	lvColumn.fmt = LVCFMT_BITMAP_ON_RIGHT | LVCF_IMAGE;
    	lvColumn.cx = 130;
    	lvColumn.pszText = "Column 2";
    	if ( m_ListCtrlCallsHistory.InsertColumn( i++, &lvColumn ) == -1 )
    	    return FALSE;
    
                    // Adding Column 3,4,5,6,7,.....
    I've specified that I would like the icon to be on the right hand side of the text and it seems to work fine for all the columns execept of the first 1.

    Can someone please explain how can I put my image on the right side of the text in the first column???

    Many thanks!!!!
    Last edited by Salvadoravi; August 10th, 2009 at 10:29 AM.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Adding columns to CListCtrl

    It looks like it's a problem with the control.

    Here's a workaround:

    Insert a dummy column at index 0 and set its width to 0.

    Add a HDN_ITEMCHANGING notification handler and if the changed item has an index of 0, set it's column width back to 0. This ensures that the user can't uncover the hidden column.

    Not necessarily pretty, but it seems to work.

    Code:
    void CColumnImageDlg::CreateColumns( )
    {
      m_ImageList.Create( ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), ILC_COLOR4 , 0, 0 );
     
      m_ImageList.Add( ::AfxGetApp( )->LoadIcon( IDI_ICON1 ) );
      m_ImageList.Add( ::AfxGetApp( )->LoadIcon( IDI_ICON1 ) );
     
      m_ListCtrl.SetImageList( &m_ImageList, LVSIL_SMALL );
     
      LVCOLUMN lvColumn;
     
      int nIndex = 0;
      lvColumn.iImage = 0; 
      lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_IMAGE | LVCF_WIDTH;
      lvColumn.fmt = LVCFMT_LEFT | LVCFMT_BITMAP_ON_RIGHT | LVCF_IMAGE;
      lvColumn.cx = 0;
      lvColumn.pszText = _T("Column 1");
     
      m_ListCtrl.InsertColumn( nIndex, &lvColumn );
     
      lvColumn.cx = 100;
      lvColumn.pszText = _T("Column n");
     
      for( nIndex = 1; nIndex < 5; nIndex++ ) 
      {
        m_ListCtrl.InsertColumn( nIndex, &lvColumn );
      }
    }
     
    void CColumnImageDlg::OnHdnItemchangingList1(NMHDR *pNMHDR, LRESULT *pResult)
    {
      LPNMHEADER phdr = reinterpret_cast<LPNMHEADER>(pNMHDR);
     
      if( phdr->iItem == 0 )
      {
        phdr->pitem->cxy = 0;
      }
     
     *pResult = 0;
    }


  3. #3
    Join Date
    Dec 2005
    Posts
    445

    Re: Adding columns to CListCtrl

    Thank you Arjay!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured