日期:2014-05-19  浏览次数:20918 次

如何在LISTVIEW显示文件的时候显示图标
listView1.SmallImageList   =   this.imageList2;
                                                        listView1.Items[0].ImageIndex   =   0;

这个只是自定义的imageList2;的图标
如何让他显示系统的图标啊

------解决方案--------------------
参考如下连接:
http://www.cnblogs.com/wdxinren/archive/2005/01/03/85655.html
------解决方案--------------------
using System.Runtime.InteropServices;

private static uint SHGFI_ICON = 0x100;
private static uint SHGFI_DISPLAYNAME = 0x200;
private static uint SHGFI_TYPENAME = 0x400;
private static uint SHGFI_ATTRIBUTES = 0x800;
private static uint SHGFI_ICONLOCATION = 0x1000;
private static uint SHGFI_EXETYPE = 0x2000;
private static uint SHGFI_SYSICONINDEX = 0x4000;
private static uint SHGFI_LINKOVERLAY = 0x8000;
private static uint SHGFI_SELECTED = 0x10000;
private static uint SHGFI_LARGEICON = 0x0;
private static uint SHGFI_SMALLICON = 0x1;
private static uint SHGFI_OPENICON = 0x2;
private static uint SHGFI_SHELLICONSIZE = 0x4;
private static uint SHGFI_PIDL = 0x8;
private static uint SHGFI_USEFILEATTRIBUTES = 0x10;

private static uint FILE_ATTRIBUTE_NORMAL = 0x80;
private static uint LVM_FIRST = 0x1000;
private static uint LVM_SETIMAGELIST = LVM_FIRST + 3;
private static uint LVSIL_NORMAL = 0;
private static uint LVSIL_SMALL = 1;

[DllImport( "Shell32.dll ")]
private static extern IntPtr SHGetFileInfo(string pszPath,
uint dwFileAttributes, ref SHFILEINFO psfi,
int cbfileInfo, uint uFlags);

private struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public int dwAttributes;
public string szDisplayName;
public string szTypeName;
}

[DllImport( "User32.DLL ")]
public static extern int SendMessage(IntPtr hWnd,
uint Msg, IntPtr wParam, IntPtr lParam);

private void button1_Click(object sender, EventArgs e)
{
SHFILEINFO vFileInfo = new SHFILEINFO();
IntPtr vImageList = SHGetFileInfo( " ", 0, ref vFileInfo,
Marshal.SizeOf(vFileInfo), SHGFI_SHELLICONSIZE |
SHGFI_SYSICONINDEX | SHGFI_LARGEICON);

SendMessage(listView1.Handle, LVM_SETIMAGELIST, (IntPtr)LVSIL_NORMAL,
vImageList);

SHGetFileInfo(@ "c:\temp\temp.txt ", 0, ref vFileInfo,
Marshal.SizeOf(vFileInfo), SHGFI_SYSICONINDEX);

listView1.Items.Add( "temp.txt ", vFileInfo.iIcon);
}