码迷,mamicode.com
首页 > 其他好文 > 详细

MFC ListControl用法合集

时间:2014-09-20 03:31:56      阅读:374      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   os   使用   ar   for   数据   div   

以下未经说明,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); // 设置扩展风格 
注:listview 的style 请查阅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 );

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

    //新插入的数据在下面

   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. 判断是否点击在listctrl 的checkbox 上 
添加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(" 点在listctrl 的checkbox 上"); 
           } 
           *pResult = 0; 
       } 
-------------------------------------------------------------------------------- 
15. 右键点击listctrl 的item 弹出菜单 
添加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; 
   }

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. listctrl 的subitem 添加图标 
       m_list.SetExtendedStyle(LVS_EX_SUBITEMIMAGES); 
       m_list.SetItem(..); // 具体参数请参考msdn

MFC ListControl用法合集

标签:style   http   io   os   使用   ar   for   数据   div   

原文地址:http://www.cnblogs.com/trenail/p/3982802.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!