原文:C# 获取系统Icon、获取文件相关的Icon

1、获取系统Icon工具下载SystemIcon.exe

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);
}
}
}

  

C# 获取系统Icon、获取文件相关的Icon的更多相关文章

  1. delphi 获取系统注册的文件图标

    var Icon:TICON; Key : string; App : string; Index : Integer; begin FileName:=Edit6.Text; then begin ...

  2. Qt5获取系统文件图标,文件路径

    获取系统图标: QFileIconProvider icon_provider; QIcon icon = icon_provider.icon(QFileIconProvider::Folder); ...

  3. [Cocos2d-x for WP8学习笔记] 获取系统字体

    在Cocos2d-x for WP8较新的版本中,获取字体这一块,在wp8下默认返回了null,只能内嵌字体文件解决. 其实可以通过下面的方法获取系统的字体文件 CCFreeTypeFont::loa ...

  4. 如何在jsp页面获取系统时间

    <%@ page import="java.util.*"%> //获取系统时间必须导入的 <%@ page import="java.text.*&q ...

  5. System.getProperty()获取系统的相关属性

    我们在编程的过程中有时候需要获取系统的相关属性,今天就让我们一起来学习学习如何获取系统的相关属性 至于System.getProperty(param)中的各个参数的概念请看下表. java.vers ...

  6. 获取系统相关信息 (CPU使用率 内存使用率 系统磁盘大小)

    引言 在软件开个过程中,对于软件的稳定性和使用率也是我们需要关注的 .  使用sigar来监控,简单方便!  使用说明:下载sigar jar及配合sigar的dll文件来用,需要将dll文件放到JD ...

  7. Linux sysinfo获取系统相关信息

    Linux中,可以用sysinfo来获取系统相关信息. #include <stdio.h> #include <stdlib.h> #include <errno.h& ...

  8. c# 日常记录,(获取系统时间、return),一些文件隐藏无法引用,c#多个窗体之间传值

    1.获取系统时间 DateTime.Now.ToString(); DateTime dt =DateTime.Now; dt.AddDays(1); //增加一天 dt.AddDays(-1);// ...

  9. 牛客网Java刷题知识点之File对象常用功能:获取文件名称、获取文件路径、获取文件大小、获取文件修改时间、创建与删除、判断、重命名、查看系统根目录、容量获取、获取某个目录下内容、过滤器

    不多说,直接上干货! 获取文件名称.获取文件路径.获取文件大小.获取文件修改时间 FileMethodDemo.java package zhouls.bigdata.DataFeatureSelec ...

随机推荐

  1. 【9204】第k小整数

    Time Limit: 10 second Memory Limit: 2 MB 问题描述 现有n个整数,n≤10000,要求出这n个正整数中的第k个最小整数(相同大小的整数只计算一次),k≤1000 ...

  2. perl对比两个文件的行

    perl对比两个文件的行 对比两个文件的各行,得到A与B相同的行/A与B不相同的行 主要功能 得到相同行 得到A中包含,B不包含的行 得到B中包含,A中不包含的行 具体执行情况 Perl代码 #!/u ...

  3. php array数组的相关处理函数and str字符串处理与正则表达式

    下面给各位同学整理了一些关于php array数组的相关处理函数and str字符串处理与正则表达式,希望文章对你会有所帮助.   数组的相关处理函数: 1)数组的键值操作函数 array_value ...

  4. Android onKeyDown监听返回键无效

    当我们的Activity继承了TabActivity,在该类中重写onKeyDown是监听不到返回键的, 具体解决方法如下: 重写dispatchKeyEvent /** * 退出 */ @Overr ...

  5. Spring MVC学习:配置简解

    http://blog.csdn.net/heirenheiren/article/details/41485485

  6. Android Material Design 5.0 PickerDialog

    5.0系统下的时间选择器效果图: 该项目兼容到3.0下面所以用第三方开源项目:actionbarsherlock,动画效果兼容:nineoldandroids-2.4.0.jar,格式转换器:joda ...

  7. 【u209】轰炸

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 小y是苏联的总书记. 苏联有n个城市,某些城市之间修筑了公路.任意两个城市都可以通过公路直接或者间接到 ...

  8. Android菜鸟的成长笔记(25)——可爱的小闹钟

    摘要: 这一篇主要使用系统为我们提供的一个服务AlarmManager来制作一个Android小闹钟,同时还涉及到了自定义主题.判断第一次启动应用.自定义动画.对话框.制作指导滑动页面等方面.最后形成 ...

  9. Windows下启动ActiveMq端口被占用的解决办法

    cd /D E:\RuntimeSoft\apache-activemq-5.11.0\binactivemq.bat start结果提示:端口号被占用. Windows下查看端口号被占用开始--运行 ...

  10. Python 数组[],元组(),字典{}的异同

    序列 Python有6中内建的序列,在这里我们重点讨论两种,数组和元组.所有序列都可以做某些特定的操作,大致上常用的是:加,乘,索引,分片以及检查某个元素是否属于序列的成员. Python还提供一些内 ...