在CListCtrl显示文件,并根据文件类型来显示图标技术转载 2009-04-20 15:28:53 阅读31 评论0 字号:大中小 订阅 网上找到的代码,share BOOL CTest6Dlg::OnInitDialog() { CDialog::OnInitDialog();
HIMAGELIST himlSmall; HIMAGELIST himlLarge; SHFILEINFO sfi; char cSysDir[MAX_PATH]; CString strBuf; memset(cSysDir, 0, MAX_PATH);
GetWindowsDirectory(cSysDir, MAX_PATH); strBuf = cSysDir; sprintf(cSysDir, "%s", strBuf.Left(strBuf.Find("\\")+1)); himlSmall = (HIMAGELIST)SHGetFileInfo ((LPCSTR)cSysDir, 0, &sfi, sizeof(SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_SMALLICON );
himlLarge = (HIMAGELIST)SHGetFileInfo((LPCSTR)cSysDir, 0, &sfi, sizeof(SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_LARGEICON);
if (himlSmall && himlLarge) { ::SendMessage(m_list.m_hWnd, LVM_SETIMAGELIST, (WPARAM)LVSIL_SMALL, (LPARAM)himlSmall); ::SendMessage(m_list.m_hWnd, LVM_SETIMAGELIST, (WPARAM)LVSIL_NORMAL, (LPARAM)himlLarge); } return TRUE; // return TRUE unless you set the focus to a control } void CTest6Dlg::AddFiles(LPCTSTR lpszFileName, BOOL bAddToDocument) { int nIcon = GetIconIndex(lpszFileName, FALSE, FALSE); CString strSize; CFileFind filefind; // get file size if (filefind.FindFile(lpszFileName)) { filefind.FindNextFile(); strSize.Format("%d", filefind.GetLength()); } else strSize = "0";
// split path and filename CString strFileName = lpszFileName; CString strPath; int nPos = strFileName.ReverseFind('\\'); if (nPos != -1) { strPath = strFileName.Left(nPos); strFileName = strFileName.Mid(nPos + 1); }
// insert to list int nItem = m_list.GetItemCount(); m_list.InsertItem(nItem, strFileName, nIcon); m_list.SetItemText(nItem, 1, strSize); m_list.SetItemText(nItem, 2, strFileName.Right(3)); m_list.SetItemText(nItem, 3, strPath); } int CTest6Dlg::GetIconIndex(LPCTSTR lpszPath, BOOL bIsDir, BOOL bSelected) { SHFILEINFO sfi; memset(&sfi, 0, sizeof(sfi));
if (bIsDir) { SHGetFileInfo(lpszPath, FILE_ATTRIBUTE_DIRECTORY, &sfi, sizeof(sfi), SHGFI_SMALLICON | SHGFI_SYSICONINDEX | SHGFI_USEFILEATTRIBUTES |(bSelected ? SHGFI_OPENICON : 0)); return sfi.iIcon; } else { SHGetFileInfo (lpszPath, FILE_ATTRIBUTE_NORMAL, &sfi, sizeof(sfi), SHGFI_SMALLICON | SHGFI_SYSICONINDEX | SHGFI_USEFILEATTRIBUTES | (bSelected ? SHGFI_OPENICON : 0)); return sfi.iIcon; } return -1; http://hi.baidu.com/2708010203/blog/item/eb00ca3e9fadc3c47d1e71ac.html |
|