it编程 > 编程语言 > Asp.net

C#调用系统图标的几种实现方案

16人参与 2026-09-16 Asp.net

c# winform 把系统图标放到 button(两种方案)

说明:windows系统自带dll(imageres.dll / shell32.dll)里没有动物头像,只有文件夹、警告、信息、磁盘等系统图标;动物头像需要自己准备图片资源。下面先讲系统内置图标调用。

前置引用

using system;
using system.drawing;
using system.runtime.interopservices;
using system.windows.forms;

方案1:直接用 systemicons(最简单,内置系统图标)

systemicons 是 .net 自带,不用p/invoke,直接拿到警告、信息、问号、错误等系统图标,赋值给按钮 image 属性。

// 窗体构造函数
public form1()
{
    initializecomponent();

    // 取系统【信息】图标,转成位图放到按钮
    icon sysicon = systemicons.information;
    button1.image = sysicon.tobitmap();
    button1.imagealign = contentalignment.middleleft;
    button1.textimagerelation = textimagerelation.imagebeforetext;
    button1.text = "信息按钮";
}

可用枚举:
systemicons.applicationerrorwarninginformationquestionshieldwinlogo

方案2:从 imageres.dll / shell32.dll 提取任意系统图标(pinvoke extracticonex)

windows现代图标大多在 c:\windows\system32\imageres.dll,旧版在 shell32.dll,通过索引提取图标。

[dllimport("shell32.dll", charset = charset.unicode)]
private static extern int extracticonex(string lpszfile, int niconindex, out intptr phiconlarge, out intptr phiconsmall, int nicons);

[dllimport("user32.dll")]
private static extern bool destroyicon(intptr hicon);

/// <summary>从dll提取图标</summary>
/// <param name="dllpath">imageres.dll</param>
/// <param name="index">图标索引</param>
/// <returns>icon对象</returns>
private icon extractdllicon(string dllpath, int index)
{
    intptr hlarge, hsmall;
    int ret = extracticonex(dllpath, index, out hlarge, out hsmall, 1);
    if (ret <= 0) return null;

    icon icon = icon.fromhandle(hlarge);
    destroyicon(hlarge);
    destroyicon(hsmall);
    return icon;
}

// 使用示例
private void form1_load(object sender, eventargs e)
{
    string dll = path.combine(environment.getfolderpath(environment.specialfolder.windows), @"system32\imageres.dll");
    icon foldericon = extractdllicon(dll, 3); // index=3 文件夹图标,索引可查imageres图标表
    button2.image = foldericon.tobitmap();
    button2.imagealign = contentalignment.middleleft;
    button2.textimagerelation = textimagerelation.imagebeforetext;
    button2.text = "文件夹";
}

工具推荐:resource hacker 打开 imageres.dll,浏览所有图标,查看每个图标的索引编号

动物头像怎么放到按钮

系统dll没有动物头像,两种办法:

  1. 下载动物png/ico图片,加入项目资源(项目属性→资源,添加图片)
// 资源里的动物图
button3.image = properties.resources.cat;
button3.textimagerelation = textimagerelation.imagebeforetext;
  1. 代码加载本地图片文件
button3.image = image.fromfile(@"c:\cat.png");

wpf版本(如果你是wpf)

wpf按钮不用bitmap,用bitmapimage

<button width="120" height="40">
    <stackpanel orientation="horizontal">
        <image width="20" height="20">
            <image.source>
                <bitmapimage urisource="pack://application:,,,/resources/cat.png"/>
            </image.source>
        </image>
        <textblock margin="5,0,0,0">动物按钮</textblock>
    </stackpanel>
</button>

常见坑

  1. 图标用完记得释放gdi句柄,否则内存泄漏;
  2. .net6/.net7+ winform 要手动安装 system.drawing.common nuget包;
  3. 64位系统,读取system32下dll,32位程序会被重定向到syswow64,提取图标会不对,项目平台目标设为x64。

如果你需要,我可以给你一份imageres.dll常用图标索引清单,或者写一个遍历imageres所有图标预览窗体。

到此这篇关于c#调用系统图标的几种实现方案的文章就介绍到这了,更多相关c#调用系统图标内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

您想发表意见!!点此发布评论

推荐阅读

使用C# WinForm实现一个TCP服务端与客户端

09-16

C#中SerialPort 串口通信发送与接收完整指南

09-15

C#代码轻松实现锁定Excel中的特定单元格

09-15

C#基于Spire.XLS实现Excel与Markdown双向转换教程

09-15

C# 锁机制原理深入解析

09-10

C#代码如何设置限制PDF复制和打印权限

09-10

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论