zoukankan      html  css  js  c++  java
  • 获取文件的图标

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace 获取文件的图标
    {
    
        public partial class Form1 : Form
        {
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
            struct SHFILEINFO
            {
                public IntPtr hIcon;
                public int iIcon;
                public uint dwAttributes;
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
                public string szDisplayName;
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
                public string szTypeName;
            }
    
            public Form1()
            {
                InitializeComponent();
            }
    
    
            private void button1_Click(object sender, EventArgs e)
            {
                using (Graphics g = this.pictureBox1.CreateGraphics())
                {
                    g.Clear(this.pictureBox1.BackColor); //清除picturebox的背景色,为了画透明图标
    
                    g.DrawIcon(GetIcon(this.textBox1.Text, false), 1, 1); //绘制小图标
                }
    
                using (Graphics g = this.pictureBox2.CreateGraphics())
                {
                    g.Clear(this.pictureBox2.BackColor); //清除picturebox的背景色,为了画透明图标
    
                    g.DrawIcon(GetIcon(textBox1.Text, true), 1, 1); //绘制小图标
                }
            }
            /// 
            /// 获取文件类型的关联图标
            /// 
            /// 文件类型的扩展名或文件的绝对路径
            /// 是否返回大图标
            /// 获取到的图标
            static Icon GetIcon(string fileName, bool isLargeIcon)
            {
                SHFILEINFO shfi = new SHFILEINFO();
                IntPtr hI;
    
                if (isLargeIcon)
                    hI = NativeMethods.SHGetFileInfo(fileName, 0, ref shfi, (uint)Marshal.SizeOf(shfi), NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_USEFILEATTRIBUTES | NativeMethods.SHGFI_LARGEICON);
                else
                    hI = NativeMethods.SHGetFileInfo(fileName, 0, ref shfi, (uint)Marshal.SizeOf(shfi), NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_USEFILEATTRIBUTES | NativeMethods.SHGFI_SMALLICON);
    
                Icon icon = Icon.FromHandle(shfi.hIcon).Clone() as Icon;
    
                NativeMethods.DestroyIcon(shfi.hIcon); //释放资源
                return icon;
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                if (openFileDialog1 .ShowDialog ()==DialogResult .OK )
                {
                    textBox1.Text = openFileDialog1.FileName;
                }
            }
    
            class NativeMethods
            {
    
                [DllImport("Shell32.dll", EntryPoint = "SHGetFileInfo", SetLastError = true, CharSet = CharSet.Auto)]
                public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
    
                [DllImport("User32.dll", EntryPoint = "DestroyIcon")]
                public static extern int DestroyIcon(IntPtr hIcon);
    
                #region API 参数的常量定义
    
                public const uint SHGFI_ICON = 0x100;
                public const uint SHGFI_LARGEICON = 0x0; //大图标 32×32
                public const uint SHGFI_SMALLICON = 0x1; //小图标 16×16
                public const uint SHGFI_USEFILEATTRIBUTES = 0x10;
    
                #endregion
    
            }
        }
    
    }
  • 相关阅读:
    多一盎司定律
    工作职场中,需要养成并实践的思维模型
    中国易经大师排名,易学十大泰斗人物
    大易人生 --- 曾老
    中道管理 --- 曾老
    大数据分析的道与术
    人应该服从自己的规划
    练习不是让你一次就把事情做好,而是帮助你做的越来越好
    让理想转个弯
    在酒桌上就能搞定生意
  • 原文地址:https://www.cnblogs.com/3Tai/p/2878614.html
Copyright © 2011-2022 走看看