1.新建对话框MFC,在对话框上放一个ListCtrl
// TODO: 在此添加额外的初始化代码 m_wndPath.DeleteAllItems();//清空 m_wndPath.InsertColumn(0,_T("项目"));//添加列 m_wndPath.InsertColumn(1,_T("所在路径")); m_wndPath.SetColumnWidth(0, 150);//设置列宽 m_wndPath.SetColumnWidth(1, 350); m_wndPath.SetRedraw(FALSE);//防止重绘 int nIndex; //char|TCHAR项目属性->字符集:使用多字节字符集 TCHAR Path[MAX_PATH+1];//TCHAR取代char MAX_PATH最长路径 nIndex=m_wndPath.InsertItem( 0,_T("Windows目录") ); if( nIndex < 0 ) return TRUE; GetWindowsDirectory(Path,MAX_PATH);//取得windows目录 m_wndPath.SetItemText( nIndex, 1, Path ); LPITEMIDLIST pidl;//桌面CSIDL_DESKTOPDIRECTORY //用来得到系统的某些特定文件夹的位置信息 if(SUCCEEDED(SHGetSpecialFolderLocation(NULL,CSIDL_DESKTOPDIRECTORY,&pidl))) { if(SHGetPathFromIDList(pidl, Path))//功能是把项目标志符列表转换为文档系统路径 { nIndex=m_wndPath.InsertItem( 0,_T("桌面"));//成功则返回0 if( nIndex < 0 ) { return TRUE; } m_wndPath.SetItemText( nIndex, 1, Path ); } } m_wndPath.SetRedraw(TRUE);//显示 return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
m_wndPath.DeleteAllItems(); m_wndPath.InsertColumn(0,"项目"); m_wndPath.InsertColumn(1,"所在路径"); m_wndPath.SetColumnWidth(0, 150); m_wndPath.SetColumnWidth(1, 350); m_wndPath.SetRedraw(FALSE); int nIndex; const int nFolder[]={ CSIDL_ALTSTARTUP,CSIDL_APPDATA,CSIDL_BITBUCKET,CSIDL_COMMON_ALTSTARTUP, CSIDL_COMMON_DESKTOPDIRECTORY,CSIDL_COMMON_FAVORITES,CSIDL_COMMON_PROGRAMS, CSIDL_COMMON_STARTMENU,CSIDL_COMMON_STARTUP,CSIDL_CONTROLS,CSIDL_COOKIES, CSIDL_DESKTOP,CSIDL_DESKTOPDIRECTORY,CSIDL_DRIVES,CSIDL_FAVORITES,CSIDL_FONTS, CSIDL_HISTORY,CSIDL_INTERNET,CSIDL_INTERNET_CACHE,CSIDL_NETHOOD,CSIDL_NETWORK, CSIDL_PERSONAL,CSIDL_PRINTERS,CSIDL_PRINTHOOD,CSIDL_PROGRAMS,CSIDL_RECENT, CSIDL_SENDTO,CSIDL_STARTMENU,CSIDL_STARTUP,CSIDL_TEMPLATES }; const CString strFolderName[]={ "CSIDL_ALTSTARTUP","CSIDL_APPDATA","回收站","CSIDL_COMMON_ALTSTARTUP", "CSIDL_COMMON_DESKTOPDIRECTORY","CSIDL_COMMON_FAVORITES","CSIDL_COMMON_PROGRAMS", "CSIDL_COMMON_STARTMENU","CSIDL_COMMON_STARTUP","控制面板","CSIDL_COOKIES", "CSIDL_DESKTOP","桌面","我的电脑","收藏夹","字体", "历史纪录","CSIDL_INTERNET","CSIDL_INTERNET_CACHE","网上邻居","CSIDL_NETWORK", "我的文档","打印机","CSIDL_PRINTHOOD","程序组","最近打开的文档", "发送","任务条启动菜单目录","启动目录","临时文档" }; char Path[MAX_PATH+1]; nIndex=m_wndPath.InsertItem( 0,"Windows目录" ); if( nIndex < 0 ) return TRUE; GetWindowsDirectory(Path,MAX_PATH); m_wndPath.SetItemText( nIndex, 1, Path ); nIndex=m_wndPath.InsertItem( 0,"System目录" ); if( nIndex < 0 ) return TRUE; GetSystemDirectory(Path,MAX_PATH); m_wndPath.SetItemText( nIndex, 1, Path ); int i,count=sizeof(nFolder)/sizeof(int); for(i=0;i<count;i++) { LPITEMIDLIST pidl; LPMALLOC pShellMalloc; if(SUCCEEDED(SHGetMalloc(&pShellMalloc))) { if(SUCCEEDED(SHGetSpecialFolderLocation(NULL, nFolder[i],&pidl))) { if(SHGetPathFromIDList(pidl, Path)) { nIndex=m_wndPath.InsertItem( 0,strFolderName[i] ); if( nIndex < 0 ) { pShellMalloc->Free(pidl); pShellMalloc->Release(); return TRUE; } m_wndPath.SetItemText( nIndex, 1, Path ); } pShellMalloc->Free(pidl); } pShellMalloc->Release(); } } m_wndPath.SetRedraw(TRUE);
//获得原有风格 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是个CImageList对象 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);
// 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 } }
这个相对前面九个条款是比较麻烦的,因为如果你要删除多行的话,往往要出错。比如,我现在要删除第0行和第1行(列表的行序列是从0开始的)
那么好啊。我来删了
m_listctrl.DeleteItem(0); m_listctrl.DeleteItem(1);
恭喜你,错了,我好开心啊 :)
因为你删除第0行以后,下面的行会往上移,那么原来的第1行就变成了第0行,那么你再 m_listctrl.DeleteItem(1),那么删除的是原来的第2行,真麻烦,
所以,只有从下往上删,才是安全的,先删的,不会影响后面的操作,
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 ); } }
你就等着收尸吧
这时候我们就要B4微软了,为虾米木有GetLastselectedItemPosition 和GetPrevSelectedItem,多写一对成员函数会死啊 :(
没办法,办法自己想,这里有个笨办法
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 } }
GetNextSelectedItem这个函数,看msdn的用法,其实是返回第一个的index,然后走到下一个选中的行去,所以这么做也是安全的,在实际中,俺也是这么做的,测试也通过,没问题的
当然,还有个办法,先通过GetFirstSelectedItemPosition和GetNextSelectedItem来获取所有的选中行的index,然后把这些index放到一个数组里,然后再从下往上删
唉真麻烦啊,还要不定数组,不说用new在堆上开吧,那么一个vector总是要的吧,麻烦啊,所以我暂时是用上述的办法来删除,也供大家参考,希望能找到更好的办法。