C#把应用程序的图标读取出来添加到imagelist里去`

如题... 如题 展开
 我来答
百度网友7522791a6
2008-12-10 · TA获得超过1174个赞
知道小有建树答主
回答量:1372
采纳率:50%
帮助的人:984万
展开全部
可以用api,下面的例子可以参考。
using System;
using System.Runtime.InteropServices;

namespace MediaClient
{
public class Win32
{
[DllImport("shell32.dll", EntryPoint = "ExtractIcon")]
public static extern int ExtractIcon(IntPtr hInst,string lpFileName,int nIndex);

[DllImport("shell32.dll", EntryPoint = "SHGetFileInfo")]
public static extern IntPtr SHGetFileInfo(string pszPath,uint dwFileAttribute,ref SHFILEINFO psfi,uint cbSizeFileInfo,uint Flags);

[DllImport("User32.dll", EntryPoint = "DestroyIcon")]
public static extern int DestroyIcon(IntPtr hIcon);

[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
public IntPtr hIcon;
public IntPtr iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
}
}

调用方法:

/**//// <summary>
/// 获得指定路径下面的所有文件及文件夹
/// </summary>
/// <param name="path">路径</param>
/// <returns></returns>
public ArrayList GetListViewItem(string path,ImageList imglist)
{
Win32.SHFILEINFO shfi = new Win32.SHFILEINFO();
try
{
string[] dirs = Directory.GetDirectories(path);
string[] files = Directory.GetFiles(path);
ArrayList itemarr = new ArrayList();

for (int i = 0; i < dirs.Length; i++)
{
string[] info = new string[4];
DirectoryInfo dir = new DirectoryInfo(dirs[i]);
//获得图标
Win32.SHGetFileInfo(dirs[i],
(uint)0x80,
ref shfi,
(uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi),
(uint)(0x100 | 0x400)); //取得Icon和TypeName
//添加图标
imglist.Images.Add(dir.Name, (Icon)Icon.FromHandle(shfi.hIcon).Clone());
info[0] = dir.Name;
info[1] = "";
info[2] = "文件夹";
info[3] = dir.LastWriteTime.ToString();
ListViewItem item = new ListViewItem(info,dir.Name);
itemarr.Add(item);
//销毁图标
Win32.DestroyIcon(shfi.hIcon);
}
for (int i = 0; i < files.Length; i++)
{
string[] info = new string[4];
FileInfo fi = new FileInfo(files[i]);
//获得图标
Win32.SHGetFileInfo(files[i],
(uint)0x80,
ref shfi,
(uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi),
(uint)(0x100 | 0x400)); //取得Icon和TypeName
//添加图标
imglist.Images.Add(fi.Name,(Icon)Icon.FromHandle(shfi.hIcon).Clone());
info[0] = fi.Name;
info[1] = fi.Length.ToString();
info[2] = fi.Extension.ToString();
info[3] = fi.LastWriteTime.ToString();
ListViewItem item = new ListViewItem(info,fi.Name);
itemarr.Add(item);
//销毁图标
Win32.DestroyIcon(shfi.hIcon);
}
return itemarr;

}
catch
{
return null;
}
}

uFlags常数:
SHGFI_ICON = 0x100
SHGFI_DISPLAYNAME = 0x200,
SHGFI_TYPENAME = 0x400,
SHGFI_ATTRIBUTES = 0x800,
SHGFI_ICONLOCATION = 0x1000,
SHGFI_EXETYPE = 0x2000,
SHGFI_SYSICONINDEX = 0x4000,
SHGFI_LINKOVERLAY = 0x8000,
SHGFI_SELECTED = 0x10000,
SHGFI_ATTR_SPECIFIED = 0x20000,
SHGFI_LARGEICON = 0x0,
SHGFI_SMALLICON = 0x1,
SHGFI_OPENICON = 0x2,
SHGFI_SHELLICONSIZE = 0x4,
SHGFI_PIDL = 0x8,
SHGFI_USEFILEATTRIBUTES = 0x10
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式