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);
}
}
}
C# 获取系统Icon、获取文件相关的Icon的更多相关文章
- delphi 获取系统注册的文件图标
var Icon:TICON; Key : string; App : string; Index : Integer; begin FileName:=Edit6.Text; then begin ...
- Qt5获取系统文件图标,文件路径
获取系统图标: QFileIconProvider icon_provider; QIcon icon = icon_provider.icon(QFileIconProvider::Folder); ...
- [Cocos2d-x for WP8学习笔记] 获取系统字体
在Cocos2d-x for WP8较新的版本中,获取字体这一块,在wp8下默认返回了null,只能内嵌字体文件解决. 其实可以通过下面的方法获取系统的字体文件 CCFreeTypeFont::loa ...
- 如何在jsp页面获取系统时间
<%@ page import="java.util.*"%> //获取系统时间必须导入的 <%@ page import="java.text.*&q ...
- System.getProperty()获取系统的相关属性
我们在编程的过程中有时候需要获取系统的相关属性,今天就让我们一起来学习学习如何获取系统的相关属性 至于System.getProperty(param)中的各个参数的概念请看下表. java.vers ...
- 获取系统相关信息 (CPU使用率 内存使用率 系统磁盘大小)
引言 在软件开个过程中,对于软件的稳定性和使用率也是我们需要关注的 . 使用sigar来监控,简单方便! 使用说明:下载sigar jar及配合sigar的dll文件来用,需要将dll文件放到JD ...
- Linux sysinfo获取系统相关信息
Linux中,可以用sysinfo来获取系统相关信息. #include <stdio.h> #include <stdlib.h> #include <errno.h& ...
- c# 日常记录,(获取系统时间、return),一些文件隐藏无法引用,c#多个窗体之间传值
1.获取系统时间 DateTime.Now.ToString(); DateTime dt =DateTime.Now; dt.AddDays(1); //增加一天 dt.AddDays(-1);// ...
- 牛客网Java刷题知识点之File对象常用功能:获取文件名称、获取文件路径、获取文件大小、获取文件修改时间、创建与删除、判断、重命名、查看系统根目录、容量获取、获取某个目录下内容、过滤器
不多说,直接上干货! 获取文件名称.获取文件路径.获取文件大小.获取文件修改时间 FileMethodDemo.java package zhouls.bigdata.DataFeatureSelec ...
随机推荐
- 【33.10%】【codeforces 604C】Alternative Thinking
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- PHP文件处理--操作文件
除了能够对文件内容进行读写,对文件本身相同也能够进行操作,如拷贝文件.又一次命名.查看改动日期等. PHP内置了大量的文件操作函数,经常使用的文件函数例如以下表: 函数原型 函数说明 举例 bool ...
- kernel build & preempt-rt patch & xenomai
提前准备好 linux 内核源代码,假设是 x86 系统.能够去下载原生内核(Vanilla kernel): wget https://www.kernel.org/pub/linux/kernel ...
- CompletionService 和ExecutorService的区别和用法
JavaSE5的Java.util.concurrent包中的执行器(Executor)将为你管理Thread对象,从而简化了并发编程.Executor在客户端和执行任务之间提供了一个间接层,Exec ...
- [Angular Router] Lazy loading Module with Auxiliary router
Found the way to handle Auxiliary router for lazy loading moudle and erge load module are different. ...
- 解析C#内存管理
C#内存管理解析 前言:对于很多的C#程序员来说,经常会很少去关注其内存的释放,他们认为C#带有强大的垃圾回收机制,所有不愿意去考虑这方面的事情,其实不尽然,很多时候我们都需要考虑C#内存的管理问题, ...
- hdu5389 Zero Escape
Problem Description Zero Escape, is a visual novel adventure video game directed by Kotaro Uchikoshi ...
- 【37%】【poj1436】Horizontally Visible Segments
Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5200 Accepted: 1903 Description There ...
- 矩阵分解(matrix factorization)
1. 基本概念 针对高维空间中的数据集,矩阵分解通过寻找到一组基及每一个数据点在该基向量下的表示,可对原始高维空间中的数据集进行压缩表示. 令 X=[x1,⋯,xm]∈Rm×n 为数据矩阵,矩阵分解的 ...
- nuklear(A single-header ANSI C gui library,界面还不错)
Nuklear This is a minimal state immediate mode graphical user interface toolkit written in ANSI C an ...