16人参与 • 2026-09-16 • Asp.net
说明:windows系统自带dll(imageres.dll / shell32.dll)里没有动物头像,只有文件夹、警告、信息、磁盘等系统图标;动物头像需要自己准备图片资源。下面先讲系统内置图标调用。
using system; using system.drawing; using system.runtime.interopservices; using system.windows.forms;
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.application、error、warning、information、question、shield、winlogo
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没有动物头像,两种办法:
// 资源里的动物图 button3.image = properties.resources.cat; button3.textimagerelation = textimagerelation.imagebeforetext;
button3.image = image.fromfile(@"c:\cat.png");
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>system.drawing.common nuget包;如果你需要,我可以给你一份imageres.dll常用图标索引清单,或者写一个遍历imageres所有图标预览窗体。
到此这篇关于c#调用系统图标的几种实现方案的文章就介绍到这了,更多相关c#调用系统图标内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论