[置顶] 获取CListCtrl选中行行号的多种方法

来源:互联网 发布:华纳史诗冒险 知乎 编辑:程序博客网 时间:2024/05/22 08:20


转自:http://blog.csdn.net/u012372584/article/details/56830958


获取选中行的行号分为获取选中多行的行号和获取单行选中的行号。以下代码都经过测试没问题,涉及到一些函数请自行查询MSDN.

获取多行选中的行号:

方法一:

[cpp] view plain copy
 print?
  1. CString str;         
  2.     for(int i=0; i<m_List.GetItemCount(); i++)         
  3.     {              
  4.         if( m_List.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED )              
  5.         {                   
  6.             str.Format(_T("选中了第%d行"), i);                  
  7.             AfxMessageBox(str);             
  8.         }        
  9.     }  

方法二:

[cpp] view plain copy
 print?
  1. CString str;   
  2.     POSITION pos = m_List.GetFirstSelectedItemPosition();         
  3.     if (pos == NULL)              
  4.         TRACE0("No items were selected!/n");         
  5.     else         
  6.     {              
  7.         while (pos)              
  8.         {                   
  9.             int nItem = m_List.GetNextSelectedItem(pos);                   
  10.             TRACE1("Item %d was selected!/n", nItem);        
  11.             str.Format(_T("选中了第%d行"), nItem);                  
  12.             AfxMessageBox(str);         
  13.         }         
  14.     }  

获取单行选中行号的:

方法一:

[cpp] view plain copy
 print?
  1. CString str;   
  2.     int nIdx=m_List.GetSelectionMark();  
  3.     if (nIdx<0)  
  4.     {  
  5.         AfxMessageBox(_T("没有选中任何行"));    
  6.     }   
  7.     else  
  8.     {  
  9.         str.Format(_T("选中了第%d行"), nIdx);                  
  10.         AfxMessageBox(str);  
  11.     }  

获取选中的行号和列号:

方法一:需要添加NM_CLICK消息的响应函数,也就是单击消息响应:

[cpp] view plain copy
 print?
  1. DWORD dwPos = GetMessagePos();              
  2.     CPoint point( LOWORD(dwPos), HIWORD(dwPos) );                
  3.     m_List.ScreenToClient(&point);                
  4.     LVHITTESTINFO lvinfo;              
  5.     lvinfo.pt = point;             
  6.     lvinfo.flags = LVHT_ABOVE;                   
  7.     int nItem = m_List.SubItemHitTest(&lvinfo);              
  8.     if(nItem != -1)              
  9.     {                   
  10.         CString strtemp;                   
  11.         strtemp.Format(_T("单击的是第%d行第%d列"), lvinfo.iItem, lvinfo.iSubItem);                  
  12.         AfxMessageBox(strtemp);             
  13.     }   

方法二:

需要添加NM_CLICK消息的响应函数,也就是单击消息响应:

[cpp] view plain copy
 print?
  1. void Ctest111Dlg::OnNMClickList1(NMHDR *pNMHDR, LRESULT *pResult)  
  2. {  
  3.     LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);  
  4.     // TODO: 在此添加控件通知处理程序代码  
  5.   
  6.     NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;              
  7.     if(pNMListView->iItem != -1)             
  8.     {                   
  9.         CString strtemp;                   
  10.         strtemp.Format(_T("单击的是第%d行第%d列"),                                   
  11.         pNMListView->iItem, pNMListView->iSubItem);                   
  12.         AfxMessageBox(strtemp);             
  13.     }     
  14.   
  15.     *pResult = 0;  
  16. }  

原创粉丝点击