VC控件ListCtrl的使用方法總彙

如下未經說明,listctrl默認view 風格爲report 
------------------------------------------------------------------------------- 
1. CListCtrl 風格 
       LVS_ICON: 爲每一個item顯示大圖標 
       LVS_SMALLICON: 爲每一個item顯示小圖標 
       LVS_LIST: 顯示一列帶有小圖標的item 
       LVS_REPORT: 顯示item詳細資料 
直觀的理解:windows資源管理器,"查看"標籤下的"大圖標,小圖標,列表,詳細資料 
-------------------------------------------------------------------------------- 
2. 設置listctrl 風格及擴展風格 
       LONG lStyle; 
       lStyle = GetWindowLong(m_list.m_hWnd, GWL_STYLE);//獲取當前窗口style 
       lStyle &= ~LVS_TYPEMASK; //清除顯示方式位 
       lStyle |= LVS_REPORT; //設置style 
       SetWindowLong(m_list.m_hWnd, GWL_STYLE, lStyle);//設置style 
       DWORD dwStyle = m_list.GetExtendedStyle(); 
       dwStyle |= LVS_EX_FULLROWSELECT;//選中某行使整行高亮(只適用與report風格的listctrl) 
dwStyle |= LVS_EX_GRIDLINES;//網格線(只適用與report風格的listctrl) 
dwStyle |= LVS_EX_CHECKBOXES;//item前生成checkbox控件 
m_list.SetExtendedStyle(dwStyle); //設置擴展風格 
注:listviewstyle請查閱msdn       
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wceshellui5/html/wce50lrflistviewstyles.asp 
-------------------------------------------------------------------------------- 
3. 插入數據 
       m_list.InsertColumn( 0, "ID", LVCFMT_LEFT, 40 );//插入列 
       m_list.InsertColumn( 1, "NAME", LVCFMT_LEFT, 50 ); php

      //新插入的在上面
       int nRow = m_list.InsertItem(0, "11");// 插入行
       m_list.SetItemText(nRow, 1, "jacky");//設置其它列數據 html

    //新插入的數據在下面 shell

   int nIndex = m_list.GetItemCount();
    LV_ITEM lvItem;
    lvItem.mask = LVIF_TEXT ; 
    lvItem.iItem = nIndex;     //行數
    lvItem.iSubItem = 0;
    lvItem.pszText = (char*)(LPCTSTR)strCount;   //第一列
    //在最後一行插入記錄值.
         m_list.InsertItem(&lvItem); 
    //插入其它列
    m_list.SetItemText(nIndex,1,strLat);
-------------------------------------------------------------------------------- 
4. 一直選中item 
選中style中的Show selection always,或者在上面第2點中設置LVS_SHOWSELALWAYS 
-------------------------------------------------------------------------------- 
5. 選中和取消選中一行 
int nIndex = 0; 
//選中 
m_list.SetItemState(nIndex, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED); 
//取消選中 
     m_list.SetItemState(nIndex, 0, LVIS_SELECTED|LVIS_FOCUSED); 
-------------------------------------------------------------------------------- 
6. 獲得listctrl中全部行的checkbox的狀態 
m_list.SetExtendedStyle(LVS_EX_CHECKBOXES); 
CString str; 
for(int i=0; i<m_list.GetItemCount(); i++) 
{ 
if( m_list.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED || m_list.GetCheck(i)) 
{ 
str.Format(_T("%d行的checkbox爲選中狀態"), i); 
AfxMessageBox(str); 
} 
} 
-------------------------------------------------------------------------------- 
7. 獲得listctrl中全部選中行的序號 
方法一: 
       CString str; 
       for(int i=0; i<m_list.GetItemCount(); i++) 
       { 
           if( m_list.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED ) 
           { 
                 str.Format(_T("選中了第%d"), i); 
                 AfxMessageBox(str); 
           } 
       } 
方法二: 
       POSITION pos = m_list.GetFirstSelectedItemPosition(); 
       if (pos == NULL) 
           TRACE0("No items were selected!\n"); 
       Else 
       { 
           while (pos) 
           { 
                 int nItem = m_list.GetNextSelectedItem(pos); 
                 TRACE1("Item %d was selected!\n", nItem); 
                 // you could do your own processing on nItem here 
           } 
       } 
-------------------------------------------------------------------------------- 
8. 獲得item的信息 
       TCHAR szBuf[1024]; 
       LVITEM lvi; 
       lvi.iItem = nItemIndex; 
       lvi.iSubItem = 0; 
       lvi.mask = LVIF_TEXT; 
       lvi.pszText = szBuf; 
       lvi.cchTextMax = 1024; 
       m_list.GetItem(&lvi); 
關於獲得設置item的狀態,還能夠參考msdn文章 
       Q173242: Use Masks to Set/Get Item States in ClistCtrl 
http://support.microsoft.com/kb/173242/en-us 
-------------------------------------------------------------------------------- 
9. 獲得listctrl的全部列的header字符串內容 
       LVCOLUMN lvcol; 
       char   str[256]; 
       int   nColNum; 
       CString   strColumnName[4];//假若有4列 
       nColNum = 0; 
       lvcol.mask = LVCF_TEXT; 
       lvcol.pszText = str; 
       lvcol.cchTextMax = 256; 
       while(m_list.GetColumn(nColNum, &lvcol)) 
       { 
           strColumnName[nColNum] = lvcol.pszText; 
           nColNum++; 
       } 
-------------------------------------------------------------------------------- 
10. 使listctrl中一項可見,即滾動滾動條 
m_list.EnsureVisible(i, FALSE); 
-------------------------------------------------------------------------------- 
11. 獲得listctrl列數 
int nHeadNum = m_list.GetHeaderCtrl()->GetItemCount(); 
-------------------------------------------------------------------------------- 
12. 刪除全部列 
方法一: 
         while ( m_list.DeleteColumn (0)) 
由於你刪除了第一列後,後面的列會依次向上移動。 
方法二: 
       int nColumns = 4; 
       for (int i=nColumns-1; i>=0; i--) 
           m_list.DeleteColumn (i); 
-------------------------------------------------------------------------------- 
13. 獲得單擊的listctrl的行列號 
添加listctrl控件的NM_CLICK消息相應函數 
       void CTest6Dlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult) 
       { 
           // 方法一: 
           /* 
           DWORD dwPos = GetMessagePos(); 
           CPoint point( LOWORD(dwPos), HIWORD(dwPos) ); 
           m_list.ScreenToClient(&point); 
           LVHITTESTINFO lvinfo; 
           lvinfo.pt = point; 
           lvinfo.flags = LVHT_ABOVE; 
           int nItem = m_list.SubItemHitTest(&lvinfo); 
           if(nItem != -1) 
           { 
                 CString strtemp; 
                 strtemp.Format("單擊的是第%d行第%d", lvinfo.iItem, lvinfo.iSubItem); 
                 AfxMessageBox(strtemp); 
           } 
           */ 
         // 方法二: 
           /* 
           NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR; 
           if(pNMListView->iItem != -1) 
           { 
                 CString strtemp; 
                 strtemp.Format("單擊的是第%d行第%d", 
                                 pNMListView->iItem, pNMListView->iSubItem); 
                 AfxMessageBox(strtemp); 
           } 
           */ 
           *pResult = 0; 
       } 
-------------------------------------------------------------------------------- 
14. 判斷是否點擊在listctrlcheckbox上 
添加listctrl控件的NM_CLICK消息相應函數 
       void CTest6Dlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)       
{           
DWORD dwPos = GetMessagePos(); 
           CPoint point( LOWORD(dwPos), HIWORD(dwPos) );   
           m_list.ScreenToClient(&point); 
           LVHITTESTINFO lvinfo; 
           lvinfo.pt = point; 
           lvinfo.flags = LVHT_ABOVE; 
           UINT nFlag; 
           int nItem = m_list.HitTest(point, &nFlag); 
           //判斷是否點在checkbox上 
           if(nFlag == LVHT_ONITEMSTATEICON) 
           { 
                 AfxMessageBox("點在listctrlcheckbox"); 
           } 
           *pResult = 0; 
       } 
-------------------------------------------------------------------------------- 
15. 右鍵點擊listctrlitem彈出菜單 
添加listctrl控件的NM_RCLICK消息相應函數 
       void CTest6Dlg::OnRclickList1(NMHDR* pNMHDR, LRESULT* pResult) 
       { 
           NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR; 
           if(pNMListView->iItem != -1) 
           { 
                 DWORD dwPos = GetMessagePos(); 
                 CPoint point( LOWORD(dwPos), HIWORD(dwPos) ); 
                 CMenu menu; 
                 VERIFY( menu.LoadMenu( IDR_MENU1 ) ); 
                 CMenu* popup = menu.GetSubMenu(0); 
                 ASSERT( popup != NULL ); 
               popup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this ); 
           } 
           *pResult = 0; 
   } windows

16. item切換焦點時 (包括用鍵盤和鼠標切換 item),狀態的一些變化順序 
添加 listctrl控件的 LVN_ITEMCHANGED消息相應函數 
       void CTest6Dlg::OnItemchangedList1(NMHDR* pNMHDR, LRESULT* pResult) 
       { 
           NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR; 
           // TODO: Add your control notification handler code here 
           CString sTemp; 
           if((pNMListView->uOldState & LVIS_FOCUSED) == LVIS_FOCUSED && (pNMListView->uNewState & LVIS_FOCUSED) == 0) 
           { 
                 sTemp.Format("%d losted focus",pNMListView->iItem); 
           } 
           else if((pNMListView->uOldState & LVIS_FOCUSED) == 0 && 
               (pNMListView->uNewState & LVIS_FOCUSED) == LVIS_FOCUSED) 
           { 
                 sTemp.Format("%d got focus",pNMListView->iItem); 
           } 
           if((pNMListView->uOldState & LVIS_SELECTED) == LVIS_SELECTED && 
             (pNMListView->uNewState & LVIS_SELECTED) == 0) 
           { 
                 sTemp.Format("%d losted selected",pNMListView->iItem); 
           } 
else if((pNMListView->uOldState & LVIS_SELECTED) == 0 && (pNMListView->uNewState & LVIS_SELECTED) == LVIS_SELECTED) 
           { 
sTemp.Format("%d got selected",pNMListView->iItem); 
} 
*pResult = 0; 
-------------------------------------------------------------------------------- 
17. 獲得另外一個進程裏的 listctrl控件的 item內容 
http://www.codeproject.com/threads/int64_memsteal.asp 

-------------------------------------------------------------------------------- 
18. 選中 listview中的 item 
Q131284: How To Select a Listview Item Programmatically 
http://support.microsoft.com/kb/131284/en-us 

-------------------------------------------------------------------------------- 
19. 如何在 CListView中使用 CListCtrl的派生類 
http://www.codeguru.com/cpp/controls/listview/introduction/article.php/c919/ 

-------------------------------------------------------------------------------- 
20. listctrlsubitem添加圖標 
       m_list.SetExtendedStyle(LVS_EX_SUBITEMIMAGES); 
       m_list.SetItem(..); //具體參數請參考 msdn 
相關文章
相關標籤/搜索