【chatgpt】写一个文件CListctrl
class CFolderListCtrl : public CListCtrl { public: CImageList m_imageList; CFolderListCtrl() { // 创建图像列表,包含文件夹和文件的图标 m_imageList.Create(16, 16, ILC_COLOR32, 0, 2); HICON hIconFolder = (HICON)LoadImage(NULL, _T("folder.ico"), IMAGE_ICON, 16, 16, LR_LOADFROMFILE); HICON hIconFile = (HICON)LoadImage(NULL, _T("file.ico"), IMAGE_ICON, 16, 16, LR_LOADFROMFILE); m_imageList.Add(hIconFolder); m_imageList.Add(hIconFile); // 设置图像列表给列表控件 SetImageList(&m_imageList, LVSIL_SMALL); // 设置列标题 InsertColumn(0, _T("文件名"), LVCFMT_LEFT, 200); InsertColumn(1, _T("类型"), LVCFMT_LEFT, 100); InsertColumn(2, _T("大小"), LVCFMT_LEFT, 100); InsertColumn(3, _T("修改日期"), LVCFMT_LEFT, 150); } void AddFolder(const CString& folderPath) { // 添加文件夹及其属性到列表控件 SHFILEINFO fileInfo; SHGetFileInfo(folderPath, 0, &fileInfo, sizeof(fileInfo), SHGFI_DISPLAYNAME | SHGFI_ICON | SHGFI_SMALLICON); int index = InsertItem(GetItemCount(), fileInfo.szDisplayName, fileInfo.iIcon); SetItemText(index, 1, L"文件夹"); FILETIME fileTime = { 0 }; SHGetFileInfo(folderPath, 0, &fileInfo, sizeof(fileInfo), SHGFI_TYPENAME | SHGFI_SMALLICON | SHGFI_ATTRIBUTES); SetItemText(index, 2, L"-"); SetItemText(index, 3, FormatFileTime(fileTime)); SetItemData(index, FILE_ATTRIBUTE_DIRECTORY); } void AddFile(const CString& filePath) { // 添加文件及其属性到列表控件 SHFILEINFO fileInfo; SHGetFileInfo(filePath, 0, &fileInfo, sizeof(fileInfo), SHGFI_DISPLAYNAME | SHGFI_ICON | SHGFI_SMALLICON); int index = InsertItem(GetItemCount(), fileInfo.szDisplayName, fileInfo.iIcon); SetItemText(index, 1, L"文件"); WIN32_FILE_ATTRIBUTE_DATA fileAttrData; GetFileAttributesEx(filePath, GetFileExInfoStandard, &fileAttrData); ULONGLONG fileSize = fileAttrData.nFileSizeLow | (fileAttrData.nFileSizeHigh << 32); SetItemText(index, 2, FormatFileSize(fileSize)); SetItemText(index, 3, FormatFileTime(fileAttrData.ftLastWriteTime)); SetItemData(index, FILE_ATTRIBUTE_NORMAL); } CString FormatFileSize(ULONGLONG fileSize) { CString strSize; if (fileSize >= 1024 * 1024 * 1024) { strSize.Format(_T("%.2f GB"), fileSize / (1024.0 * 1024.0 * 1024.0)); } else if (fileSize >= 1024 * 1024) { strSize.Format(_T("%.2f MB"), fileSize / (1024.0 * 1024.0)); } else if (fileSize >= 1024) { strSize.Format(_T("%.2f KB"), fileSize / 1024.0); } else { strSize.Format(_T("%.2f Bytes"), (double)fileSize); } return strSize; } CString FormatFileTime(const FILETIME& fileTime) { SYSTEMTIME sysTime; FileTimeToSystemTime(&fileTime, &sysTime); CString strTime; strTime.Format(_T("%04d-%02d-%02d %02d:%02d:%02d"), sysTime.wYear, sysTime.wMonth, sysTime.wDay, sysTime.wHour, sysTime.wMinute, sysTime.wSecond); return strTime; } }; 以上是一个继承自CListCtrl的CFolderListCtrl类,用于显示文件夹和文件的列表,并支持显示文件的图标。在构造函数中,创建了一个图像列表,其中包含文件夹和文件的图标,并设置给列表控件。然后设置了列标题。AddFolder和AddFile方法用于向列表控件中添加文件夹和文件,并设置相应的属性,比如文件名、类型、大小和修改日期等。FormatFileSize和FormatFileTime方法用于格式化文件大小和文件修改日期的显示。