Vc++ 控件List Control用法总结
时间:2021-03-09 10:10:25|栏目:C代码|点击: 次
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
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);
以上所述就是本文的全部内容了,希望大家能够喜欢。