在winForm下编程时,做资源管理器浏览本地文件的时候,用ListView控件加载文件,
可以根据文件类型来判断item的图标,具体代码如下:
可以根据文件类型来判断item的图标,具体代码如下:
1
using System;
2
using System.Drawing;
3
using System.Windows.Forms;
4![]()
5
namespace YJDSoft.Control.Data.LocalDataExplorer
6
{
7
/// <summary>
8
/// ImageIconManage 的摘要说明。
9
/// </summary>
10
public class ImageIconManage
11
{
12
private System.Windows.Forms.ImageList m_SmallImageList;
13
private System.Windows.Forms.ImageList m_LargeImageList;
14
//private System.IntPtr m_FormHandle;
15
private int IconIndex = 0 ;
16
/// <summary>
17
/// 图表管理类
18
/// </summary>
19
/// <param name="SmallImageList">小图标集合的list</param>
20
/// <param name="LargeImageList">大图标集合的list</param>
21
public ImageIconManage(System.Windows.Forms.ImageList SmallImageList,System.Windows.Forms.ImageList LargeImageList)
22
{
23
//
24
// TODO: 在此处添加构造函数逻辑
25
//
26
m_SmallImageList = SmallImageList;
27
m_LargeImageList = LargeImageList;
28
//m_FormHandle = FormHandle;
29
m_LargeImageList.ImageSize = new Size(32,32);
30![]()
31
this.m_LargeImageList.Images.Clear();
32
this.m_SmallImageList.Images.Clear();
33
Icon ic0=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",3);
34
m_LargeImageList.Images.Add(ic0);
35
m_SmallImageList.Images.Add(ic0);
36
}
37
/// <summary>
38
/// 得到该文件对应的图标(包括大图标,小图标)
39
/// </summary>
40
/// <param name="FileNameOrExtension">文件的全路径或者文件的带点扩展名(如: .Dll)</param>
41
/// <returns>返回该文件或扩展名的图标在SmallImageList或LargeImageList里的索引值</returns>
42
public int FileBindingIcon(string FileNameOrExtension)
43
{
44
try
45
{
46
SetIcon(m_SmallImageList,FileNameOrExtension,false);//得到小图标平铺
47
SetIcon(m_LargeImageList,FileNameOrExtension,true);//得到大图标平铺
48
return IconIndex = IconIndex + 1;
49
}
50
catch(Exception ex)
51
{
52
MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);
53
return -1;
54
}
55
}
56
/// <summary>
57
/// 得的目录的图标的索引值
58
/// </summary>
59
public int GetFolderIcon
60
{
61
get
62
{
63
return 0; //目录图标的索引值默认为0
64
}
65
}
66![]()
67
用API函数调用取的文件对应的图标的所有方法
139![]()
140
}
141
}
142![]()
using System;2
using System.Drawing;3
using System.Windows.Forms;4

5
namespace YJDSoft.Control.Data.LocalDataExplorer6
{7
/// <summary>8
/// ImageIconManage 的摘要说明。9
/// </summary>10
public class ImageIconManage11
{12
private System.Windows.Forms.ImageList m_SmallImageList;13
private System.Windows.Forms.ImageList m_LargeImageList;14
//private System.IntPtr m_FormHandle;15
private int IconIndex = 0 ;16
/// <summary>17
/// 图表管理类18
/// </summary>19
/// <param name="SmallImageList">小图标集合的list</param>20
/// <param name="LargeImageList">大图标集合的list</param>21
public ImageIconManage(System.Windows.Forms.ImageList SmallImageList,System.Windows.Forms.ImageList LargeImageList)22
{23
//24
// TODO: 在此处添加构造函数逻辑25
//26
m_SmallImageList = SmallImageList;27
m_LargeImageList = LargeImageList;28
//m_FormHandle = FormHandle;29
m_LargeImageList.ImageSize = new Size(32,32);30

31
this.m_LargeImageList.Images.Clear();32
this.m_SmallImageList.Images.Clear();33
Icon ic0=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",3);34
m_LargeImageList.Images.Add(ic0);35
m_SmallImageList.Images.Add(ic0);36
}37
/// <summary>38
/// 得到该文件对应的图标(包括大图标,小图标)39
/// </summary>40
/// <param name="FileNameOrExtension">文件的全路径或者文件的带点扩展名(如: .Dll)</param>41
/// <returns>返回该文件或扩展名的图标在SmallImageList或LargeImageList里的索引值</returns>42
public int FileBindingIcon(string FileNameOrExtension)43
{44
try45
{46
SetIcon(m_SmallImageList,FileNameOrExtension,false);//得到小图标平铺47
SetIcon(m_LargeImageList,FileNameOrExtension,true);//得到大图标平铺48
return IconIndex = IconIndex + 1;49
}50
catch(Exception ex)51
{52
MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);53
return -1;54
}55
}56
/// <summary>57
/// 得的目录的图标的索引值58
/// </summary>59
public int GetFolderIcon60
{61
get62
{63
return 0; //目录图标的索引值默认为064
}65
}66

67
用API函数调用取的文件对应的图标的所有方法139

140
}141
}142



浙公网安备 33010602011771号