winForm下,做类似资源管理器一样的工具,怎么取到文件类型的相应图标
在winForm下编程时,做资源管理器浏览本地文件的时候,用ListView控件加载文件,
可以根据文件类型来判断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
#region 用API函数调用取的文件对应的图标的所有方法
68
//*********************************************************************************************************//
69
[System.Runtime.InteropServices.DllImport("Shell32.dll")]
70
public static extern int ExtractIcon(IntPtr h,string strx,int ii);
71
72
[System.Runtime.InteropServices.DllImport("Shell32.dll")]
73
public static extern int SHGetFileInfo(string pszPath,uint dwFileAttributes,ref SHFILEINFO psfi,uint cbFileInfo, uint uFlags);
74
75
public struct SHFILEINFO
76
{
77
public IntPtr hIcon;
78
public int iIcon;
79
public uint dwAttributes;
80
public char szDisplayName;
81
public char szTypeName;
82
}
83
84
protected virtual void SetIcon(ImageList imageList,string FileName,bool tf)
85
{
86
SHFILEINFO fi=new SHFILEINFO();
87
if(tf==true)
88
{//得到大图标平铺
89
int iTotal=(int)SHGetFileInfo(FileName,0,ref fi,100, 16640);//SHGFI_ICON|SHGFI_SMALLICON
90
try
91
{
92
if(iTotal >0)
93
{
94
Icon ic=Icon.FromHandle(fi.hIcon);
95
imageList.Images.Add(ic);
96
//return ic;
97
}
98
}
99
catch(Exception ex)
100
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
101
}
102
else
103
{//得到小图标平铺
104
int iTotal=(int)SHGetFileInfo(FileName,0,ref fi,100, 257);
105
try
106
{
107
if(iTotal >0)
108
{
109
Icon ic=Icon.FromHandle(fi.hIcon);
110
imageList.Images.Add(ic);
111
//return ic;
112
}
113
}
114
catch(Exception ex)
115
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
116
}
117
}
118
//*********************************************************************************************************//
119
//*************************************************************************************
120
121
protected virtual Icon myExtractIcon(string FileName,int iIndex)
122
{
123
try
124
{
125
//IntPtr hIcon=(IntPtr)ExtractIcon(this.m_FormHandle,FileName,iIndex);
126
IntPtr hIcon=(IntPtr)ExtractIcon(this.m_SmallImageList.Handle,FileName,iIndex);
127
if(! hIcon.Equals(null))
128
{
129
Icon icon=Icon.FromHandle(hIcon);
130
return icon;
131
}
132
}
133
catch(Exception ex)
134
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
135
return null;
136
}
137
//*************************************************************************************
138
#endregion
139
140
}
141
}
142

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142
