C# 获取系统图标

本文介绍了一个使用C#从shell32.dll中提取图标的示例程序,并展示了如何将这些图标加载到Windows窗体应用的PictureBox控件中。通过DllImport导入了两个来自DLL文件的函数:MessageBeep用于播放系统声音,ExtractIconEx用于提取图标资源。

1 代码

[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Runtime.InteropServices;  
  10.   
  11.   
  12. namespace TestControls  
  13. {  
  14.     public partial class Form2 : Form  
  15.     {  
  16.         public Form2()  
  17.         {  
  18.             InitializeComponent();  
  19.         }  
  20.   
  21.     
  22.         [DllImport("user32.dll", CharSet = CharSet.Auto)]  
  23.         private static extern bool MessageBeep(uint type);  
  24.   
  25.         [DllImport("Shell32.dll")]  
  26.         public extern static int ExtractIconEx(string libName, int iconIndex, IntPtr[] largeIcon, IntPtr[] smallIcon, int nIcons);  
  27.   
  28.         public static IntPtr[] largeIcon;  
  29.         public static IntPtr[] smallIcon;  
  30.   
  31.         private void Form1_Load(object sender, EventArgs e)  
  32.         {  
  33.             largeIcon = new IntPtr[250];  
  34.             smallIcon = new IntPtr[250];  
  35.   
  36.             ExtractIconEx("shell32.dll", 0, largeIcon, smallIcon, 250);  
  37.   
  38.             for (int i = 1; i < 200; ++i)  
  39.             {  
  40.                 Icon ic = Icon.FromHandle(largeIcon[i]);  
  41.                 ((PictureBox)this.Controls["pictureBox" + i.ToString()]).Image = ic.ToBitmap();  
  42.             }  
  43.         }  
  44.     }  
  45. }  

2 效果图






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值