宁可辛苦一阵子,不要辛苦一辈子

VocanoLee学习手札

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

  1 using System;

    2 using System.Collections.Generic;

    3 using System.ComponentModel;

    4 using System.Data;

    5 using System.Drawing;

    6 using System.Text;

    7 using System.Windows.Forms;

    8 using System.Runtime.InteropServices;  //名字空间

    9 

   10 namespace CS_ListViewAddFileIco

   11 {

   12     public partial class Form1 : Form

   13     {

   14         private int i;

   15         public Form1()

   16         {

   17             InitializeComponent();

   18             i = 0;

   19         }

   20 

   21         [DllImport("Shell32.dll")]

   22         private static extern int SHGetFileInfo

   23         (

   24         string pszPath,

   25         uint dwFileAttributes,

   26         out   SHFILEINFO psfi,

   27         uint cbfileInfo,

   28         SHGFI uFlags

   29         );

   30         [StructLayout(LayoutKind.Sequential)]

   31         private struct SHFILEINFO

   32         {

   33             public SHFILEINFO(bool b)

   34             {

   35                 hIcon = IntPtr.Zero; iIcon = 0; dwAttributes = 0; szDisplayName = ""; szTypeName = "";

   36             }

   37             public IntPtr hIcon;

   38             public int iIcon;

   39             public uint dwAttributes;

   40             [MarshalAs(UnmanagedType.LPStr, SizeConst = 260)]

   41             public string szDisplayName;

   42             [MarshalAs(UnmanagedType.LPStr, SizeConst = 80)]

   43             public string szTypeName;

   44         };

   45         private enum SHGFI

   46         {

   47             SmallIcon = 0x00000001,

   48             LargeIcon = 0x00000000,

   49             Icon = 0x00000100,

   50             DisplayName = 0x00000200,

   51             Typename = 0x00000400,

   52             SysIconIndex = 0x00004000,

   53             UseFileAttributes = 0x00000010

   54         }

   55         public static Icon GetFileIcon(string fileName, bool largeIcon)

   56         {

   57             SHFILEINFO info = new SHFILEINFO(true);

   58             int cbFileInfo = Marshal.SizeOf(info);

   59             SHGFI flags;

   60             if (largeIcon)

   61                 flags = SHGFI.Icon | SHGFI.LargeIcon | SHGFI.UseFileAttributes;

   62             else

   63                 flags = SHGFI.Icon | SHGFI.SmallIcon | SHGFI.UseFileAttributes;

   64             SHGetFileInfo(fileName, 256, out info, (uint)cbFileInfo, flags);

   65             return Icon.FromHandle(info.hIcon);

   66         }

   67         private void btnAdd_Click(object sender, EventArgs e)

   68         {

   69             Icon ico = GetFileIcon(tbFileName.Text, true);

   70             imageList1.Images.Add(ico);

   71             listView1.LargeImageList = imageList1;

   72             listView1.Items.Add(tbFileName.Text, i++);

   73 

   74         }

   75     }

   76 }

posted on 2009-05-06 12:58  VocanoLee  阅读(448)  评论(0)    收藏  举报