表徵圖拾取器(C#)版

來源:互聯網
上載者:User

完全是利用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 );
  }
 }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.