先注明一下,这里,我们用m_listctrl来表示一个CListCtrl的类对象,然后这里我们的ListCtrl都是report形式,至于其他的如什么大图标,小图标的暂时不讲,毕竟report是大众话的使用。其次,我们这里用条款一,条款二来描述第一点,第二点,这个是参照《Effective C++》的叫法,俺觉得这么叫比较COOL :)
条款一:设置ListCtrl的风格
在CSDN上常常看到有人问怎么设置风格的,他们ListCtrl的样子是一个列表,有横条和竖条分界线,然后选中一行,要整一行都选中,而不是只有某一列被选中,等等,这里给一个比较全面的设置方法。
//获得原有风格 DWORD dwStyle = ::GetWindowLong(m_listctrl.m_hWnd, GWL_STYLE); dwStyle &= ~(LVS_TYPEMASK); dwStyle &= ~(LVS_EDITLABELS); //设置新风格 SetWindowLong(m_listctrl.m_hWnd, GWL_STYLE,dwStyle, |LVS_REPORT | LVS_NOLABELWRAP | LVS_SHOWSELALWAYS); //设置扩展风格 DWORD styles = LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_CHECKBOXES; ListView_SetExtendedListViewStyleEx(m_listctrl.m_hWnd, styles, styles );
TCHAR rgtsz[2][10] = {_T("列头1"), _T("列头2")}; LV_COLUMN lvcolumn; CRect rect; m_listctrl.GetWindowRect(&rect); for(int i=0;i<2;i++) { lvcolumn.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH | LVCF_ORDER; lvcolumn.fmt = LVCFMT_LEFT; lvcolumn.pszText = rgtsz[i]; lvcolumn.iSubItem = i; lvcolumn.iOrder = i; if(i==0) { lvcolumn.cx = rect.Width()*3/5 ; } else lvcolumn.cx = rect.Width()*2/5; m_listctrl.InsertColumn(i, &lvcolumn); }
int nIndex = m_listctrl.GetItemCount(); LV_ITEM lvitemAdd = {0}; lvitemAdd.mask = LVIF_TEXT; lvitemAdd.iItem = nIndex ; lvitemAdd.iSubItem = 0; lvitemAdd.pszText =_T("毛毛1");; if (m_listctrl.InsertItem(&lvitemAdd) != -1) { LV_ITEM lvitem = {0}; lvitem.mask = LVIF_TEXT; lvitem.iItem = nIndex ; lvitem.iSubItem = 1; lvitem.pszText =_T("毛毛2"); m_listctrl.SetItem(&lvitem); }
m_image.Create(16,16, TRUE|ILC_COLOR24, 3, 1); m_listctrl.SetImageList(&m_image,LVSIL_SMALL);
lvitemAdd.mask = LVIF_TEXT;
lvitemAdd.mask = LVIF_TEXT|LVIF_IMAGE
int nIndex = m_listctrl.GetItemCount(); LV_ITEM lvitemAdd = {0}; lvitemAdd.mask = LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM; lvitemAdd.iItem = nIndex ; lvitemAdd.iSubItem = 0; lvitemAdd.pszText =_T("毛毛1");; lvitemAdd.iImage = n; lvitemAdd.lParam = (LPARAM)hwnd;(某个窗口的窗口句柄) if (m_listctrl.InsertItem(&lvitemAdd) != -1) { LV_ITEM lvitem = {0}; lvitem.mask = LVIF_TEXT; lvitem.iItem = nIndex ; lvitem.iSubItem = 1; lvitem.pszText =_T("毛毛2"); m_listctrl.SetItem(&lvitem); }
void CMyDlg::OnItemClick(NMHDR* pNMHDR, LRESULT* pResult) { // TODO: Add your control notification handler code here int nItem = -1; LPNMITEMACTIVATE lpNMItemActivate = (LPNMITEMACTIVATE)pNMHDR; if(lpNMItemActivate != NULL) { nItem = lpNMItemActivate->iItem; } }
LV_ITEM lvitem = {0}; lvitem.iItem = nIndex; lvitem.iSubItem = 0; lvitem.mask = LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM; m_listctrl.GetItem(&lvitem)
m_listctrl.SetItemState(nIndex,LVIS_SELECTED|LVIS_FOCUSED,LVIS_SELECTED|LVIS_FOCUSED);
// CListCtrl* pListCtrl = (CListCtrl*) GetDlgItem (IDC_YOURLISTCONTROL); ASSERT(pListCtrl != NULL); POSITION pos = pList->GetFirstSelectedItemPosition(); if (pos == NULL) TRACE0("No items were selected!\n"); else { while (pos) { int nItem = pList->GetNextSelectedItem(pos); TRACE1("Item %d was selected!\n", nItem); // you could do your own processing on nItem here } }
m_listctrl.DeleteItem(0) m_listctrl.DeleteItem(1)
m_listctrl.DeleteItem(1) m_listctrl.DeleteItem(0)
POSITION pos = m_listctrl.GetFirstSelectedItemPosition(); if (pos == NULL) TRACE0("No items were selected!\n"); else { while (pos) { int nItem = m_listctrl.GetNextSelectedItem(pos); m_listctrl.DeleteItem(nItem ); } }
POSITION sSelPos = NULL; while(sSelPos = m_listctrl.GetFirstSelectedItemPosition()) { int nSelItem = -1; nSelItem = m_listctrl.GetNextSelectedItem(sSelPos); if(nSelItem >= 0 && nSelItem<m_listctrl.GetItemCount()) { //好了,这个nSelItem 就是我们要的DD } }