C# 获取系统Icon、获取文件相关的Icon
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace FileExplorer { /// <summary> /// 系统Icon /// 1、Get() 获取指定索引对应的系统icon /// 2、Save() 保存所有系统图像 /// 3、Show() 显示所有系统Icon图像 /// </summary> public partial class SystemIcon : Form { public SystemIcon() { InitializeComponent(); Show(this); Save(); } /// <summary> /// 在form上显示所有系统icon图像 /// </summary> public static void Show(Form form) { LoadSystemIcon(); FlowLayoutPanel flowLayout = new FlowLayoutPanel(); flowLayout.Dock = System.Windows.Forms.DockStyle.Fill; flowLayout.AutoScroll = true; for (int i = 0; i < SystemIconList.Count; i++) { PictureBox pic = new PictureBox(); pic.Size = new System.Drawing.Size(32, 32); flowLayout.Controls.Add(pic); Bitmap p = SystemIconList[i].ToBitmap(); pic.Image = p; } form.Controls.Add(flowLayout); } /// <summary> /// 保存所有系统图像 /// </summary> public static void Save() { LoadSystemIcon(); for (int i = 0; i < SystemIconList.Count; i++) { Bitmap p = SystemIconList[i].ToBitmap(); // 保存图像 string path = AppDomain.CurrentDomain.BaseDirectory + "系统图标\\"; string filepath = path + (i + ".png"); if (!Directory.Exists(path)) Directory.CreateDirectory(path); if (!File.Exists(filepath)) p.Save(filepath); } } /// <summary> /// 获取指定索引对应的系统icon /// </summary> public static Icon Get(int index) { LoadSystemIcon(); return index < SystemIconList.Count ? SystemIconList[index] : null; } private static List<Icon> SystemIconList = new List<Icon>(); // 记录系统图标 //[DllImport("user32.dll", CharSet = CharSet.Auto)] //private static extern bool MessageBeep(uint type); [DllImport("Shell32.dll")] public extern static int ExtractIconEx(string libName, int iconIndex, IntPtr[] largeIcon, IntPtr[] smallIcon, int nIcons); private static IntPtr[] largeIcon; private static IntPtr[] smallIcon; /// <summary> /// 获取所有系统icon图像 /// </summary> private static void LoadSystemIcon() { if (SystemIconList.Count > 0) return; largeIcon = new IntPtr[1000]; smallIcon = new IntPtr[1000]; ExtractIconEx("shell32.dll", 0, largeIcon, smallIcon, 1000); SystemIconList.Clear(); for (int i = 0; i < largeIcon.Length; i++) { try { Icon ic = Icon.FromHandle(largeIcon[i]); SystemIconList.Add(ic); } catch (Exception ex) { break; } } } //private void LoadSystemIcon() //{ // largeIcon = new IntPtr[1000]; // smallIcon = new IntPtr[1000]; // ExtractIconEx("shell32.dll", 0, largeIcon, smallIcon, 1000); // FlowLayoutPanel flowLayout = new FlowLayoutPanel(); // flowLayout.Dock = System.Windows.Forms.DockStyle.Fill; // flowLayout.AutoScroll = true; // for (int i = 0; i < largeIcon.Length; i++) // { // try // { // PictureBox pic = new PictureBox(); // pic.Size = new System.Drawing.Size(32, 32); // flowLayout.Controls.Add(pic); // Icon ic = Icon.FromHandle(largeIcon[i]); // SystemIcon.Add(ic); // Bitmap p = ic.ToBitmap(); // pic.Image = p; // // 保存图像 // string path = AppDomain.CurrentDomain.BaseDirectory + "系统图标\\"; // string filepath = path + (i + ".png"); // if (!Directory.Exists(path)) Directory.CreateDirectory(path); // if (!File.Exists(filepath)) p.Save(filepath); // } // catch (Exception ex) // { // break; // } // } // this.Controls.Add(flowLayout); //} } }
2、获取文件相关的Icon
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Windows.Forms; using System.IO; using System.Drawing; using Microsoft.Win32; namespace FileExplorer { /// <summary> /// 获取指定文件的Icon图像getIcon()、getIcon2() /// </summary> class FileIcon { private const uint SHGFI_ICON = 0x100; private const uint SHGFI_LARGEICON = 0x0; //大图标 private const uint SHGFI_SMALLICON = 0x1; //小图标 [StructLayout(LayoutKind.Sequential)] public struct SHFILEINFO { public IntPtr hIcon; //文件的图标句柄 public IntPtr iIcon; //图标的系统索引号 public uint dwAttributes; //文件的属性值 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string szDisplayName;//文件的显示名 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] public string szTypeName; //文件的类型名 }; [DllImport("shell32.dll")] private static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags); /// <summary> /// 获取文件FilePath对应的Icon /// </summary> public static Icon getIcon(string FilePath) { SHFILEINFO shinfo = new SHFILEINFO(); //FileInfo info = new FileInfo(FileName); //大图标 SHGetFileInfo(FilePath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_LARGEICON); Icon largeIcon = Icon.FromHandle(shinfo.hIcon); //Icon.ExtractAssociatedIcon(FileName); return largeIcon; } /// <summary> /// 获取文件FilePath对应的Icon /// </summary> public static Icon getIcon2(string FilePath) { return Icon.ExtractAssociatedIcon(FilePath); } } }
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
python click
from:https://www.cnblogs.com/dachenzi/p/7995539.html https://github.com/ageitgey/face_recognition/blob/master/face_recognition/face_recognition_cli.py Click模块 click模块是Flask的作者开发的一个第三方模块,用于快速创建命令行。它的作用与Python标准库的argparse相同,但是,使用起来更简单。 click是一个第三方库,因此使用起来需要先行安装 安装click模块 使用pip命令即可完成模块的安装 1 pip install click 基本使用 Click对argparse的主要改在在于易用性,使用click模块主要分为两个步骤: 使用@click.command() 装饰一个函数,使之成为命令行接口 使用@click.option() 等装饰函数,为其添加命令行选项 下列为click官方提供的例子: 1 2 3 4 5 6 7 8 9 10 11 12 13 import click @click .comma...
- 下一篇
区块链开发公司介绍区块链的基本原理
随着区块链的发展普及,区块链技术逐渐走入人们的视野,它无处不在,凡是了解它的人都称赞,那么区块链是什么样的原理呢? 我们知道区块链最大的特点是不可篡改,那么区块链如何实现不可篡改的呢?其实就是大家提到区块链最常用的一个词——去中心化。所以去中心不是区块链的目的,只是实现不可篡改的手段,而不可篡改也其实也不是最终的目的,真正的目的是创造信任,达成共识。所以都说,区块链是创造信任的机器。 区块链的基本原理理解起来并不复杂。 首先包括三个基本概念: 交易(Transaction):一次对账本的操作,导致账本状态的一次改变,如添加一条转账记录; 区块(Block):记录一段时间内发生的所有交易和状态结果,是对当前账本状态的一次共识; 链(Chain):由区块按照发生顺序串联而成,是整个账本状态变化的日志记录。 在实现上,首先假设存在一个分布式的数据记录账本,这个账本只允许添加、不允许删除。账本底层的基本结构是一个线性的链表,这也是其名字“区块链”的来源。链表由一个个“区块”串联组成,后继区块记录前导区块的哈希(Hash)值。新的数据要加入,必须放到一个新的区块中。而这个块(以及块里的交易)是否...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- MySQL8.0.19开启GTID主从同步CentOS8
- 设置Eclipse缩进为4个空格,增强代码规范
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- Hadoop3单机部署,实现最简伪集群
- CentOS8安装Docker,最新的服务器搭配容器使用
- Docker快速安装Oracle11G,搭建oracle11g学习环境