如何在CListCtrl显示文件中的文件,并根据文件本身的类型来显示图标

jag1976 2004-05-07 12:54:41
如何在CListCtrl显示文件中的文件,并根据文件本身的类型来显示图标(在系统中显示)
...全文
200 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangnanonnet 2004-05-08
  • 打赏
  • 举报
回复
我那个同事是个荷兰人,看来我们不能完全轻信鬼老啊
zhangnanonnet 2004-05-08
  • 打赏
  • 举报
回复
此段程序是我从我们的项目中摘抄下来的,这段我还没有仔细分析过,是一个同事写的,还有点缺憾,他把系统盘指定在C:了,我把初始化那部分代码改成这样了。
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_FileList.m_hWnd, LVM_SETIMAGELIST, (WPARAM)LVSIL_SMALL, (LPARAM)himlSmall);
::SendMessage(m_FileList.m_hWnd, LVM_SETIMAGELIST, (WPARAM)LVSIL_NORMAL, (LPARAM)himlLarge);
return TRUE;
}
return FALSE;
jag1976 2004-05-07
  • 打赏
  • 举报
回复
多谢鱼欢老大
wbusy 2004-05-07
  • 打赏
  • 举报
回复
爽啊,正是我想要的哦。
多谢了,呵呵
zhangnanonnet 2004-05-07
  • 打赏
  • 举报
回复
对了,在对话框初始化的时候还要将系统的ICO列表读取出来
HIMAGELIST himlSmall;
HIMAGELIST himlLarge;
SHFILEINFO sfi;

himlSmall = (HIMAGELIST) SHGetFileInfo ((LPCSTR) "C:\\",
0, &sfi, sizeof (SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_SMALLICON);

himlLarge = (HIMAGELIST) SHGetFileInfo ((LPCSTR) "C:\\",
0, &sfi, sizeof (SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_LARGEICON);

if (himlSmall && himlLarge)
{
::SendMessage(m_FileList.m_hWnd, LVM_SETIMAGELIST, (WPARAM)LVSIL_SMALL, (LPARAM)himlSmall);
::SendMessage(m_FileList.m_hWnd, LVM_SETIMAGELIST, (WPARAM)LVSIL_NORMAL, (LPARAM)himlLarge);
return TRUE;
}
zhangnanonnet 2004-05-07
  • 打赏
  • 举报
回复
比如:
// ADDFILE按钮的事件
void CFilePage::OnBtnadd()
{
// TODO: Add your control notification handler code here
char szFilters[] = "All Files (*.*)|*.*||";

CFileDialog dlg(FALSE, "*", "*.*" , OFN_NOVALIDATE| OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT, szFilters, this);

dlg.m_ofn.lpstrTitle = "Select Files";

if (dlg.DoModal() == IDOK)
{
POSITION pos = dlg.GetStartPosition();
while (pos != NULL)
{
CString strPath = dlg.GetNextPathName(pos);
if (strPath.Find(":\\\\") == 1 && strPath.GetLength() > 4)
{
// this means we have an invalid path that looks like this:
// C:\\foo.bmp
// We need to cut out the extra slash
CString temp;
temp = strPath.Left(3);
temp += strPath.Mid(4);
strPath = temp;
}
AddFiles(strPath, TRUE);
}
}
}

// AddFiles
void CFilePage::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 = FormatSize(filefind.GetLength());
}
else
strSize = FormatSize(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_FileList.GetItemCount();
m_FileList.InsertItem(nItem, strFileName, nIcon);
m_FileList.SetItemText(nItem, 1, strSize);
m_FileList.SetItemText(nItem, 2, GetTypeName(lpszFileName));
m_FileList.SetItemText(nItem, 3, strPath);
}
// 获得文件图标
int 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;
}
zhaolaoxin 2004-05-07
  • 打赏
  • 举报
回复
CListCtrl的图标是由程序控制的

15,980

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧