Vc++ 控件List Control用法總結。本站提示廣大學習愛好者:(Vc++ 控件List Control用法總結)文章只能為提供參考,不一定能成為您想要的結果。以下是Vc++ 控件List Control用法總結正文
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);
以上所述就是本文的全體內容了,願望年夜家可以或許愛好。