完全是利用API实现的。
以前学习API的时候写的。现在整理一下,发给那些想知道如何实现的朋友。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.IO;
namespace GetFileIcon
{
/// <summary>
/// 图标拾取器
/// 程序:房客
/// 网址:http://www.cnblogs.com/sxlfybb/
/// 转载请保留,谢谢
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 32);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Start";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(112, 16);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(48, 40);
this.pictureBox1.TabIndex = 2;
this.pictureBox1.TabStop = false;
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(24, 8);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(64, 24);
this.checkBox1.TabIndex = 3;
this.checkBox1.Text = "小图标";
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2,
this.menuItem3,
this.menuItem4});
this.menuItem1.Text = "系统(&S)";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "关于(&A)";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Index = 1;
this.menuItem3.Text = "-";
//
// menuItem4
//
this.menuItem4.Index = 2;
this.menuItem4.Text = "退出(&X)";
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(178, 63);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "图标拾取C#版";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
[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;
};
public const uint SHGFI_ICON=0x100;
public const uint SHGFI_SMALLICON=0x1;
public const uint SHGFI_LARGEICON=0x0;
public const uint SHGFI_ATTRIBUTES=0x800;
public const uint FILE_ATTRIBUTE_NORMAL=0x80;
public const uint SHGFI_USEFILEATTRIBUTES=0x10;
[DllImport("shell32.dll")]
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
private void button1_Click(object sender, System.EventArgs e)
{
uint IconSize = SHGFI_LARGEICON;
if(checkBox1.Checked)
IconSize = SHGFI_SMALLICON;
if( openFileDialog1.ShowDialog(this) == DialogResult.OK )
{
try
{
SHFILEINFO shinfo = new SHFILEINFO();
SHGetFileInfo( openFileDialog1.FileName, 0, ref shinfo,(uint)Marshal.SizeOf(shinfo),SHGFI_ICON|IconSize);
Icon icon = Icon.FromHandle (shinfo.hIcon);
pictureBox1.Image = (Image)icon.ToBitmap();
string strSavePath = string.Empty;
if( MessageBox.Show(this,"是否保存图标?","询问",MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes )
{
if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
{
strSavePath = saveFileDialog1.FileName;
using (FileStream sw = new FileStream(strSavePath,FileMode.Create))
{
icon.Save(sw);
}
}
}
}
catch( Exception ex )
{
MessageBox.Show( ex.Message );
}
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
saveFileDialog1.Filter = "*.ico|*.ico";
openFileDialog1.Filter = "*.*|*.*";
}
private void menuItem4_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
string str = "图标拾取C#版 Ver1.0" ;
str += System.Environment.NewLine;
str += System.Environment.NewLine;
str += "程序:房客 " + "\t" + "QQ:83849123";
str += System.Environment.NewLine;
str += "网站:http://www.cnblogs.com/sxlfybb";
MessageBox.Show( this,str,"信息",MessageBoxButtons.OK,MessageBoxIcon.Information );
}
}
}