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

jag1976 2004-05-07 12:54:41
如何在CListCtrl显示文件中的文件,并根据文件本身的类型来显示图标(在系统中显示)
...全文
170 7 点赞 打赏 收藏 举报
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
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的图标是由程序控制的
  • 打赏
  • 举报
回复
相关推荐
发帖
界面
加入

1.5w+

社区成员

VC/MFC 界面
申请成为版主
帖子事件
创建了帖子
2004-05-07 12:54
社区公告
暂无公告