怎么样根据后缀显示系统图标?
在listveiw里,怎么样根据文件的后缀来显示系统的图标呢? 问题点数:20、回复次数:3Top
1 楼fengjun19912(学于睿智)回复于 2006-03-03 15:25:01 得分 20
正好我做过,给一段代码你参考一下:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace YJDSoft.Control.Data.LocalDataExplorer
{
/// <summary>
/// ImageIconManage 的摘要说明。
/// </summary>
public class ImageIconManage
{
private System.Windows.Forms.ImageList m_SmallImageList;
private System.Windows.Forms.ImageList m_LargeImageList;
//private System.IntPtr m_FormHandle;
private int IconIndex = 0 ;
/// <summary>
/// 图表管理类
/// </summary>
/// <param name="SmallImageList">小图标集合的list</param>
/// <param name="LargeImageList">大图标集合的list</param>
public ImageIconManage(System.Windows.Forms.ImageList SmallImageList,System.Windows.Forms.ImageList LargeImageList)
{
//
// TODO: 在此处添加构造函数逻辑
//
m_SmallImageList = SmallImageList;
m_LargeImageList = LargeImageList;
//m_FormHandle = FormHandle;
m_LargeImageList.ImageSize = new Size(32,32);
this.m_LargeImageList.Images.Clear();
this.m_SmallImageList.Images.Clear();
Icon ic0=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",3);
m_LargeImageList.Images.Add(ic0);
m_SmallImageList.Images.Add(ic0);
}
/// <summary>
/// 得到该文件对应的图标(包括大图标,小图标)
/// </summary>
/// <param name="FileNameOrExtension">文件的全路径或者文件的带点扩展名(如: .Dll)</param>
/// <returns>返回该文件或扩展名的图标在SmallImageList或LargeImageList里的索引值</returns>
public int FileBindingIcon(string FileNameOrExtension)
{
try
{
SetIcon(m_SmallImageList,FileNameOrExtension,false);//得到小图标平铺
SetIcon(m_LargeImageList,FileNameOrExtension,true);//得到大图标平铺
return IconIndex = IconIndex + 1;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);
return -1;
}
}
/// <summary>
/// 得的目录的图标的索引值
/// </summary>
public int GetFolderIcon
{
get
{
return 0; //目录图标的索引值默认为0
}
}
#region 用API函数调用取的文件对应的图标的所有方法
//*********************************************************************************************************//
[System.Runtime.InteropServices.DllImport("Shell32.dll")]
public static extern int ExtractIcon(IntPtr h,string strx,int ii);
[System.Runtime.InteropServices.DllImport("Shell32.dll")]
public static extern int SHGetFileInfo(string pszPath,uint dwFileAttributes,ref SHFILEINFO psfi,uint cbFileInfo, uint uFlags);
public struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
public char szDisplayName;
public char szTypeName;
}
protected virtual void SetIcon(ImageList imageList,string FileName,bool tf)
{
SHFILEINFO fi=new SHFILEINFO();
if(tf==true)
{//得到大图标平铺
int iTotal=(int)SHGetFileInfo(FileName,0,ref fi,100, 16640);//SHGFI_ICON|SHGFI_SMALLICON
try
{
if(iTotal >0)
{
Icon ic=Icon.FromHandle(fi.hIcon);
imageList.Images.Add(ic);
//return ic;
}
}
catch(Exception ex)
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
}
else
{//得到小图标平铺
int iTotal=(int)SHGetFileInfo(FileName,0,ref fi,100, 257);
try
{
if(iTotal >0)
{
Icon ic=Icon.FromHandle(fi.hIcon);
imageList.Images.Add(ic);
//return ic;
}
}
catch(Exception ex)
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
}
}
//*********************************************************************************************************//
//*************************************************************************************
protected virtual Icon myExtractIcon(string FileName,int iIndex)
{
try
{
//IntPtr hIcon=(IntPtr)ExtractIcon(this.m_FormHandle,FileName,iIndex);
IntPtr hIcon=(IntPtr)ExtractIcon(this.m_SmallImageList.Handle,FileName,iIndex);
if(! hIcon.Equals(null))
{
Icon icon=Icon.FromHandle(hIcon);
return icon;
}
}
catch(Exception ex)
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
return null;
}
//*************************************************************************************
#endregion
}
}
Top
2 楼Qqwwee_Com(http://qqwwee.com)回复于 2006-03-03 15:50:40 得分 0
收藏。
====CSDN 小助手 V2.5 ====
CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
速度快;使用方便;提供源代码。
界面:http://blog.csdn.net/Qqwwee_Com/category/146601.aspx
下载:http://szlawbook.com/csdnv2
Top
3 楼copico(北北)回复于 2006-03-05 00:27:12 得分 0
哇,你后面的数字给了我太大的帮助啦Top




