C#获取文件(磁盘驱动器)的关联图标、获取目录文件和图标

我们知道在VC++环境下利用Extended Combobox很容易实现下图的样子,但是在Vc#条件下没有可扩展的Combobox控件,所以在实现起来比较麻烦。这里笔者向大家推荐两个类,第一个类用来获取本地的文件、文件夹、磁盘图标,第二类用来在Combobox中显示图标。

虽然这两个用法都比较简单,但是在实现起来还是破费周折的。

C#获取文件(磁盘驱动器)的关联图标、获取目录文件和图标(原创)


一、获取文件、文件夹、本地磁盘的类GetIcon

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;

namespace Gateway
{
    public class GetIcon
    {
        [DllImport("Shell32.dll")]
        private static extern IntPtr SHGetFileInfo
        (
            string pszPath,
            uint dwFileAttributes,
            out   SHFILEINFO psfi,
            uint cbfileInfo,
            SHGFI uFlags
        );
  

        [StructLayout(LayoutKind.Sequential)]
        private struct SHFILEINFO
        {
            public SHFILEINFO(bool b)
            {
                hIcon = IntPtr.Zero; iIcon = 0; dwAttributes = 0; szDisplayName = ""; szTypeName = "";
            }
            public IntPtr hIcon;
            public int iIcon;
            public uint dwAttributes;
            [MarshalAs(UnmanagedType.LPStr, SizeConst = 260)]
            public string szDisplayName;
            [MarshalAs(UnmanagedType.LPStr, SizeConst = 80)]
            public string szTypeName;
        };

     

        private enum SHGFI
        {
            SmallIcon = 0x00000001,
            LargeIcon = 0x00000000,
            Icon = 0x00000100,
            DisplayName = 0x00000200,
            Typename = 0x00000400,
            SysIconIndex = 0x00004000,
            UseFileAttributes = 0x00000010
        }

        /// <summary>
        /// 根据文件扩展名得到系统扩展名的图标
        /// </summary>
        /// <param name="fileName">文件名(如:win.rar;setup.exe;temp.txt)</param>
        /// <param name="largeIcon">图标的大小</param>
        /// <returns></returns>
        public static Icon GetFileIcon(string fileName, bool largeIcon)
        {
            SHFILEINFO info = new SHFILEINFO(true);
            int cbFileInfo = Marshal.SizeOf(info);
            SHGFI flags;
            if (largeIcon)
                flags = SHGFI.Icon | SHGFI.LargeIcon | SHGFI.UseFileAttributes;
            else
                flags = SHGFI.Icon | SHGFI.SmallIcon | SHGFI.UseFileAttributes;
            IntPtr IconIntPtr=SHGetFileInfo(fileName, 256, out info, (uint)cbFileInfo, flags);
            if (IconIntPtr.Equals(IntPtr.Zero))
                return null;
            return Icon.FromHandle(info.hIcon);
        }

        /// <summary>  
        /// 获取文件夹图标
        /// </summary>  
        /// <returns>图标</returns>  
        public static Icon GetDirectoryIcon(bool largeIcon)
        {
            SHFILEINFO _SHFILEINFO = new SHFILEINFO();
            int cbFileInfo = Marshal.SizeOf(_SHFILEINFO);
            SHGFI flags;
            if (largeIcon)
                flags = SHGFI.Icon | SHGFI.LargeIcon;
            else
                flags = SHGFI.Icon | SHGFI.SmallIcon;

            IntPtr IconIntPtr = SHGetFileInfo(@"", 0, out _SHFILEINFO, (uint)cbFileInfo, flags);
            if (IconIntPtr.Equals(IntPtr.Zero))
                return null;
            Icon _Icon = System.Drawing.Icon.FromHandle(_SHFILEINFO.hIcon);
            return _Icon;
        } 

    }
}



 

二、扩展Combobox控件的类ComboBoxEx

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Gateway
{
        class ComboBoxEx : ComboBox
        {
            private ImageList imageList;
            public ImageList ImageList
            {
                get { return imageList; }
                set { imageList = value; }
            }

            public ComboBoxEx()
            {
                DrawMode = DrawMode.OwnerDrawFixed;
            }

            protected override void OnDrawItem(DrawItemEventArgs ea)
            {
                ea.DrawBackground();
                ea.DrawFocusRectangle();

                ComboBoxExItem item;
                Size imageSize = imageList.ImageSize;
                Rectangle bounds = ea.Bounds;

                try
                {
                    item = (ComboBoxExItem)Items[ea.Index];

                    if (item.ImageIndex != -1)
                    {
                        imageList.Draw(ea.Graphics, bounds.Left, bounds.Top,
                        item.ImageIndex);
                        ea.Graphics.DrawString(item.Text, ea.Font, new
                        SolidBrush(ea.ForeColor), bounds.Left + imageSize.Width, bounds.Top);
                    }
                    else
                    {
                        ea.Graphics.DrawString(item.Text, ea.Font, new
                        SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
                    }
                }
                catch
                {
                    if (ea.Index != -1)
                    {
                        ea.Graphics.DrawString(Items[ea.Index].ToString(), ea.Font, new
                        SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
                    }
                    else
                    {
                        ea.Graphics.DrawString(Text, ea.Font, new
                        SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
                    }
                }

                base.OnDrawItem(ea);
            }
        }

        class ComboBoxExItem
        {
            private string _text;
            public string Text
            {
                get { return _text; }
                set { _text = value; }
            }

            private int _imageIndex;
            public int ImageIndex
            {
                get { return _imageIndex; }
                set { _imageIndex = value; }
            }

            public ComboBoxExItem()
                : this("")
            {
            }

            public ComboBoxExItem(string text)
                : this(text, -1)
            {
            }

            public ComboBoxExItem(string text, int imageIndex)
            {
                _text = text;
                _imageIndex = imageIndex;
            }

            public override string ToString()
            {
                return _text;
            }
        }
}


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值