1、列表样式设置
1 LONG lStyle; 2 lStyle = GetWindowLong(m_configList.m_hWnd, GWL_STYLE); 3 lStyle &= ~LVS_TYPEMASK; 4 lStyle |= LVS_REPORT; 5 SetWindowLong(m_configList.m_hWnd, GWL_STYLE, lStyle); 6 7 DWORD dwStyle = m_configList.GetExtendedStyle(); 8 dwStyle |= LVS_EX_FULLROWSELECT;//整行选中 9 dwStyle |= LVS_EX_GRIDLINES;//网格线 10 dwStyle |= LVS_EX_DOUBLEBUFFER; 11 //dwStyle |= LVS_EX_CHECKBOXES;//每一行最前面带个复选框 12 m_configList.SetExtendedStyle(dwStyle);
2、设置列表的列名
1 m_configList.InsertColumn(0, "列1", LVCFMT_CENTER, 100, 0); 2 m_configList.InsertColumn(1, "列2", LVCFMT_CENTER, 100, 1); 3 m_configList.InsertColumn(2, "列3", LVCFMT_CENTER, 120, 2); 4 m_configList.InsertColumn(3, "列4", LVCFMT_CENTER, 100, 3);
3、插入数据
m_configList.InsertItem(0,"");//插入行 m_configList.SetItemText(0,1,"name");//设置第2列
4、得到单击的listCtrl的行列号
1 void CTestDlg::OnClickList(NMHDR* pNMHDR,LRESULT* pResult) 2 { 3 //方法1 4 DWORD dwPos = GetMessagePos(); 5 CPoint point(LOWORD(dwPos),HIWORD(dwPos)); 6 m_configList.ScreenToClient(&point); 7 LVHITTESTINFO lvinfo; 8 lvinfo.pt = point; 9 lvinfo.flags = LVHT_ABOVE; 10 11 int nItem = m_list.SubItemHitTest(&lvinfo); 12 if(nItem != -1) 13 { 14 lvinfo.iItem //行 15 lvinfo.iSubItem //列 16 } 17 //方法2 18 NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR; 19 if(pNMListView->iItem != -1) 20 { 21 pNMListView->iItem //行 22 pNMListView->iSubItem //列 23 } 24 }
5、选中和取消选中某一行
1 int nIndex = 0; 2 m_configList.SetItemState(nIndex,LVIS_SELECTED|LVIS_FOCUSED,LVIS_SELECTED|LVIS_FOCUSED);//选中 3 m_configList.SetItemState(nIndex,0,LVIS_SELECTED|LVIS_FOCUSED);
6、得到listCtrl中所有选中行的序号
1 ① 2 CString str; 3 for(int i = 0;i < m_configList.GetITemCount();++i) 4 { 5 if(m_configList.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED) 6 {} 7 } 8 ② 9 POSITION pos = m_configList.GetFirstSelectedItemPosition(); 10 if(pos == NULL) 11 {} 12 else 13 { 14 while(pos) 15 { 16 int nItem = m_configList.GetNextSelectedItem(pos); 17 } 18 }
参考
https://blog.csdn.net/zq4132/article/details/54971717