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
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
#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 SHFILEINFO76
{ 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_SMALLICON90
try91
{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
else103
{//得到小图标平铺104
int iTotal=(int)SHGetFileInfo(FileName,0,ref fi,100, 257);105
try106
{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
try124
{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
#endregion139

140
}141
}142




浙公网安备 33010602011771号