标签:
1.新建对话框MFC,在对话框上放一个ListCtrl
ID:IDC_PATH
View:Report
2.为ListCtrl添加变量
右击->添加变量m_wndPath
3.找到OnInitDialog()函数添加如下代码:
// 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
// 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
4.char被TCHAR取代,若要用则:
项目属性->字符集:使用多字节字符集
参考代码:
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); 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);
VC中ListCtrl经验总结
先注明一下,这里,我们用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); } 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); } 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); }
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); } 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); }
[cpp] view plaincopyprint?
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; } } 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) LV_ITEM lvitem = {0}; lvitem.iItem = nIndex; lvitem.iSubItem = 0; lvitem.mask = LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM; m_listctrl.GetItem(&lvitem)
[cpp] view plaincopyprint?
// 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 } }
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 ); // 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 } }
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 } }
标签:
原文地址:http://www.cnblogs.com/lujin49/p/4770284.html