LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

【C#】LISTVIEW控件:文件/目录增加图标显示实例

admin
2021年3月11日 1:55 本文热度 2353

说明:本例将目录中的文件显示在窗体的ListView控件中,并定义了多种视图浏览。通过调用Win32库函数实现图标数据的提取

主程序:


大图标:



列表:



详细信息:


Form1.cs:

1.  public partial class Form1 : Form

2.      {

3.          FileInfoList fileList;

4.   

5.   

6.          public Form1()

7.          {

8.              InitializeComponent();

9.          }

10.

11.

12.        private void 加载文件ToolStripMenuItem_Click(object sender, EventArgs e)

13.        {

14.            FolderBrowserDialog dlg = new FolderBrowserDialog();

15.            if (dlg.ShowDialog() == DialogResult.OK)

16.            {

17.                string[] filespath = Directory.GetFiles(dlg.SelectedPath);

18.                fileList = new FileInfoList(filespath);

19.                InitListView();

20.            }

21.        }

22.

23.

24.        private void InitListView()

25.        {

26.            listView1.Items.Clear();

27.            this.listView1.BeginUpdate();

28.            foreach (FileInfoWithIcon file in fileList.list)

29.            {

30.                ListViewItem item = new ListViewItem();

31.                item.Text = file.fileInfo.Name.Split('.')[0];

32.                item.ImageIndex = file.iconIndex;

33.                item.SubItems.Add(file.fileInfo.LastWriteTime.ToString());

34.                item.SubItems.Add(file.fileInfo.Extension.Replace(".",""));

35.                item.SubItems.Add(string.Format(("{0:N0}"), file.fileInfo.Length));

36.                listView1.Items.Add(item);

37.            }

38.            listView1.LargeImageList = fileList.imageListLargeIcon;

39.            listView1.SmallImageList = fileList.imageListSmallIcon;

40.            listView1.Show();

41.            this.listView1.EndUpdate();

42.        }

43.

44.

45.        private void 大图标ToolStripMenuItem_Click(object sender, EventArgs e)

46.        {

47.            listView1.View = View.LargeIcon;

48.        }

49.

50.

51.        private void 小图标ToolStripMenuItem_Click(object sender, EventArgs e)

52.        {

53.            listView1.View = View.SmallIcon;

54.        }

55.

56.

57.        private void 平铺ToolStripMenuItem_Click(object sender, EventArgs e)

58.        {

59.            listView1.View = View.Tile;

60.        }

61.

62.

63.        private void 列表ToolStripMenuItem_Click(object sender, EventArgs e)

64.        {

65.            listView1.View = View.List;

66.        }

67.

68.

69.        private void 详细信息ToolStripMenuItem_Click(object sender, EventArgs e)

70.        {

71.            listView1.View = View.Details;

72.        }

73.       }


FileInfoList.cs:

说明:主要用于后台数据的存储

1.  class FileInfoList

2.      {

3.          public List<FileInfoWithIcon> list;

4.          public ImageList imageListLargeIcon;

5.          public ImageList imageListSmallIcon;

6.   

7.   

8.          /// <summary>

9.          /// 根据文件路径获取生成文件信息,并提取文件的图标

10.        /// </summary>

11.        /// <param name="filespath"></param>

12.        public FileInfoList(string[] filespath)

13.        {

14.            list = new List<FileInfoWithIcon>();

15.            imageListLargeIcon = new ImageList();

16.            imageListLargeIcon.ImageSize = new Size(3232);

17.            imageListSmallIcon = new ImageList();

18.            imageListSmallIcon.ImageSize = new Size(1616);

19.            foreach (string path in filespath)

20.            {

21.                FileInfoWithIcon file = new FileInfoWithIcon(path);

22.                imageListLargeIcon.Images.Add(file.largeIcon);

23.                imageListSmallIcon.Images.Add(file.smallIcon);

24.                file.iconIndex = imageListLargeIcon.Images.Count - 1;

25.                list.Add(file);

26.            }

27.        }

28.    }

29.    class FileInfoWithIcon

30.    {

31.        public FileInfo fileInfo;

32.        public Icon largeIcon;

33.        public Icon smallIcon;

34.        public int iconIndex;

35.        public FileInfoWithIcon(string path)

36.        {

37.            fileInfo = new FileInfo(path);

38.            largeIcon = GetSystemIcon.GetIconByFileName(path, true);

39.            if (largeIcon == null)

40.                largeIcon = GetSystemIcon.GetIconByFileType(Path.GetExtension(path), true);

41.

42.

43.            smallIcon = GetSystemIcon.GetIconByFileName(path, false);

44.            if (smallIcon == null)

45.                smallIcon = GetSystemIcon.GetIconByFileType(Path.GetExtension(path), false);

46.        }

47.    }

 

GetSystemIcon:

说明:定义两种图标获取方式,从文件提取和从文件关联的系统资源中提取。

1.  public static class GetSystemIcon

2.      {

3.          /// <summary>

4.          /// 依据文件名读取图标,若指定文件不存在,则返回空值。 

5.          /// </summary>

6.          /// <param name="fileName">文件路径</param>

7.          /// <param name="isLarge">是否返回大图标</param>

8.          /// <returns></returns>

9.          public static Icon GetIconByFileName(string fileName, bool isLarge = true)

10.        {

11.            int[] phiconLarge = new int[1];

12.            int[] phiconSmall = new int[1];

13.            //文件名 图标索引

14.            Win32.ExtractIconEx(fileName, 0, phiconLarge, phiconSmall, 1);

15.            IntPtr IconHnd = new IntPtr(isLarge ? phiconLarge[0] : phiconSmall[0]);

16.           

17.            if (IconHnd.ToString() == "0")

18.                return null;

19.            return Icon.FromHandle(IconHnd);

20.        }

21.

22.

23.        /// <summary> 

24.        /// 根据文件扩展名(如:.*),返回与之关联的图标。

25.        /// 若不以"."开头则返回文件夹的图标。 

26.        /// </summary> 

27.        /// <param name="fileType">文件扩展名</param> 

28.        /// <param name="isLarge">是否返回大图标</param> 

29.        /// <returns></returns> 

30.        public static Icon GetIconByFileType(string fileType, bool isLarge)

31.        {

32.            if (fileType == null || fileType.Equals(string.Empty)) return null;

33.

34.

35.            RegistryKey regVersion = null;

36.            string regFileType = null;

37.            string regIconString = null;

38.            string systemDirectory = Environment.SystemDirectory + "\\";

39.

40.

41.            if (fileType[0] == '.')

42.            {

43.                //读系统注册表中文件类型信息 

44.                regVersion = Registry.ClassesRoot.OpenSubKey(fileType, false);

45.                if (regVersion != null)

46.                {

47.                    regFileType = regVersion.GetValue(""as string;

48.                    regVersion.Close();

49.                    regVersion = Registry.ClassesRoot.OpenSubKey(regFileType + @"\DefaultIcon"false);

50.                    if (regVersion != null)

51.                    {

52.                        regIconString = regVersion.GetValue(""as string;

53.                        regVersion.Close();

54.                    }

55.                }

56.                if (regIconString == null)

57.                {

58.                    //没有读取到文件类型注册信息,指定为未知文件类型的图标 

59.                    regIconString = systemDirectory + "shell32.dll,0";

60.                }

61.            }

62.            else

63.            {

64.                //直接指定为文件夹图标 

65.                regIconString = systemDirectory + "shell32.dll,3";

66.            }

67.            string[] fileIcon = regIconString.Split(new char[] { ',' });

68.            if (fileIcon.Length != 2)

69.            {

70.                //系统注册表中注册的标图不能直接提取,则返回可执行文件的通用图标 

71.                fileIcon = new string[] { systemDirectory + "shell32.dll""2" };

72.            }

73.            Icon resultIcon = null;

74.            try

75.            {

76.                //调用API方法读取图标 

77.                int[] phiconLarge = new int[1];

78.                int[] phiconSmall = new int[1];

79.                uint count = Win32.ExtractIconEx(fileIcon[0], Int32.Parse(fileIcon[1]), phiconLarge, phiconSmall, 1);

80.                IntPtr IconHnd = new IntPtr(isLarge ? phiconLarge[0] : phiconSmall[0]);

81.                resultIcon = Icon.FromHandle(IconHnd);

82.            }

83.            catch { }

84.            return resultIcon;

85.        }

86.    }

87.

88.

89.    /// <summary> 

90.    /// 定义调用的API方法 

91.    /// </summary> 

92.    class Win32

93.    {

94.        [DllImport("shell32.dll")]

95.        public static extern uint ExtractIconEx(string lpszFile, int nIconIndex, int[] phiconLarge, int[] phiconSmall, uint nIcons);

96.    } 


该文章在 2021/3/11 1:55:41 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved