用C#代碼編寫的SN快速輸入工具

  一般軟件都要輸入序列號(SN),而大家平時用的最多的恐怕是盜版軟件,通常盜版軟件的序列號(SN)都保存成:XXXXX-XXXXX-XXXX-XXXX的形式。

  而軟件輸入序列號的地方通常都是幾個文本框(TextBox)組成。一個個的將XXXXX複製到文本框將非常麻煩。於是SN快速輸入工具便由此產生了。

  當然這些都和我的編寫這個程序的原因無關。我編寫這個程序的原因純粹是因爲有個網友和他舅舅打賭說要編寫個程序,而他舅舅就是要他編寫這個程序,但可惜我的這位網友纔是個編程初學者(比我更菜的菜鳥),當然完成不了這個看似簡單,實際要用到許多編程知識的程序咯。

  要做這個程序,首先當然是要了解程序的功能了。它的功能就是要讓你複製完了形式如“XXXXX-XXXXX-XXXX-XXXX”的序列號之後,當你把鼠標指向文本框,程序能自動將XXXXX添加到相應的文本框中。

  既然是要處理複製的序列號,那麼我們肯定要用到和剪貼板相關的東西了。剪貼板,還好這個我以前在C#中用過N次了,不用再查windows api了。C#裏面本來就提供了Clipboard這個類。

  於是就用到了string Clipboard.GetText()這個靜態方法,將剛纔複製的帶-的序列號取出來,然後用個string類型的變量strKeys保存在我的程序中,以便使用。

  第一步,從剪貼板裏面取數據,我們就完成了。

  接着,我們該考慮怎麼處理我們的數據了,我們的數據最後是要寫到幾個連續的文本框中的,那麼我們可以考慮通過String.Split(char[],string splitoption)這個方法將序列號分割成幾個子字符串,然後再通過windows api講文本輸出到相應的textbox句柄上。但是這樣做無疑增加了程序的難度,幾個連續的文本框的切換,使用Tab鍵就能做到了,然後將文本輸出到文本框中,直接讓鍵盤打出來就ok了。那麼很明顯,我們只需要將我們要按的鍵模擬出來就行了,這個時候我首先想到的是windows api中鍵盤模擬事件keybd_event,於是我開始在MSDN中查詢keybd_event方法,方法中有個KEYEVENTF_KEYUP這個參數,但是我不知道他相應的值,於是我開始查找這個長整形的值。但是始終都找不到,就在我在MSDN中查找KEYUP相關的東西的時候,我突然發現了System.Windows.Form.SendKeys這個類。原來.net framework已經將keybd_event這個非託管對象的方法封裝到SendKeys這個類中了,直接使用SendKeys這個類就可以模擬鍵盤操作了。

  再查詢Tab鍵的寫法就是{Tab}。

  那麼我只要將原來文本strKeys中的'-'全部轉換成{Tab}然後再交給SendKeys這個類來處理,這個程序就基本完成了。

  於是有了

strKeys.Replace("-", "{TAB}");
SendKeys.Send(strKeys);

  這兩行代碼。

  這樣就有了我的程序的主過程:

private void ProcessHotkey()//主處理程序
{
 strKeys = Clipboard.GetText();
 strKeys.Replace("-", "{TAB}");
 SendKeys.Send(strKeys);
}

  但是我們怎麼通過快捷鍵來觸發,來完成這個過程了。

  於是我開始在百度和MSDN查找相關處理全局快捷鍵的windows api的資料。

  要設置快捷鍵必須使用user32.dll下面的兩個方法。

BOOL RegisterHotKey(
 HWND hWnd,
 int id,
 UINT fsModifiers,
 UINT vk
);

  和

BOOL UnregisterHotKey(
 HWND hWnd,
 int id
);

  轉換成C#代碼,那麼首先就要引用命名空間System.Runtime.InteropServices;來加載非託管類user32.dll。於是有了:

[DllImport("user32.dll", SetLastError=true)]
public static extern bool RegisterHotKey(
 IntPtr hWnd, // handle to window
 int id, // hot key identifier
 KeyModifiers fsModifiers, // key-modifier options
 Keys vk // virtual-key code
);

[DllImport("user32.dll", SetLastError=true)]
public static extern bool UnregisterHotKey(
 IntPtr hWnd, // handle to window
 int id // hot key identifier
);


[Flags()]
public enum KeyModifiers
{
 None = 0,
 Alt = 1,
 Control = 2,
 Shift = 4,
 Windows = 8
}

  這是註冊和卸載全局快捷鍵的方法,那麼我們只需要在Form_Load的時候加上註冊快捷鍵的語句,在FormClosing的時候卸載全局快捷鍵。同時,爲了保證剪貼板的內容不受到其他程序調用剪貼板的干擾,在Form_Load的時候,我先將剪貼板裏面的內容清空。

  於是有了:

private void Form1_Load(object sender, System.EventArgs e)
{
 label2.AutoSize = true;

 Clipboard.Clear();//先清空剪貼板防止剪貼板裏面先複製了其他內容
 RegisterHotKey(Handle, 100, 0, Keys.F10);
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
 UnregisterHotKey(Handle, 100);//卸載快捷鍵
}

  那麼我們在別的窗口,怎麼讓按了快捷鍵以後調用我的主過程ProcessHotkey()呢?

  那麼我們就必須重寫WndProc()方法,通過監視系統消息,來調用過程:

protected override void WndProc(ref Message m)//監視Windows消息
{
 const int WM_HOTKEY = 0x0312;//按快捷鍵
 switch (m.Msg)
 {
  case WM_HOTKEY:
   ProcessHotkey();//調用主處理程序
   break;
 }
 base.WndProc(ref m);
}

  這樣我的程序就完成了。

  全部代碼:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace WindowsApplication2
{
 /// <summary>
 /// Form1 的摘要說明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  /// <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.label1 = new System.Windows.Forms.Label();
   this.label2 = new System.Windows.Forms.Label();
   this.label3 = new System.Windows.Forms.Label();
   this.label4 = new System.Windows.Forms.Label();
   this.label5 = new System.Windows.Forms.Label();
   this.SuspendLayout();
   //
   // label1
   //
   this.label1.AutoSize = true;
   this.label1.Location = new System.Drawing.Point(49, 37);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(83, 12);
   this.label1.TabIndex = 0;
   this.label1.Text = "EoS.3tion製作";
   //
   // label2
   //
   this.label2.AutoSize = true;
   this.label2.Location = new System.Drawing.Point(49, 64);
   this.label2.Name = "label2";
   this.label2.Size = new System.Drawing.Size(65, 12);
   this.label2.TabIndex = 1;
   this.label2.Text = "使用方法:";
   //
   // label3
   //
   this.label3.AutoSize = true;
   this.label3.Location = new System.Drawing.Point(65, 85);
   this.label3.Name = "label3";
   this.label3.Size = new System.Drawing.Size(155, 12);
   this.label3.TabIndex = 2;
   this.label3.Text = "1、將序列號拷貝到剪切板。";
   //
   // label4
   //
   this.label4.AutoSize = true;
   this.label4.Location = new System.Drawing.Point(65, 107);
   this.label4.Name = "label4";
   this.label4.Size = new System.Drawing.Size(179, 12);
   this.label4.TabIndex = 3;
   this.label4.Text = "2、將光標定位到序列號輸入處。";
   //
   // label5
   //
   this.label5.AutoSize = true;
   this.label5.Location = new System.Drawing.Point(65, 128);
   this.label5.Name = "label5";
   this.label5.Size = new System.Drawing.Size(77, 12);
   this.label5.TabIndex = 4;
   this.label5.Text = "3、按F10鍵。";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(292, 266);
   this.Controls.Add(this.label5);
   this.Controls.Add(this.label4);
   this.Controls.Add(this.label3);
   this.Controls.Add(this.label2);
   this.Controls.Add(this.label1);
   this.Name = "Form1";
   this.Text = "SN輸入工具(C#版Version0.1)";
   this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
   this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);
   this.PerformLayout();
  }
  #endregion

  /// <summary>
  /// 應用程序的主入口點。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  [DllImport("user32.dll", SetLastError=true)]
  public static extern bool RegisterHotKey( IntPtr hWnd,
   // handle to window
   int id, // hot key identifier
   KeyModifiers fsModifiers, // key-modifier options
   Keys vk // virtual-key code
  );

  [DllImport("user32.dll", SetLastError=true)]
  public static extern bool UnregisterHotKey( IntPtr hWnd,
   // handle to window
   int id // hot key identifier
  );

  [Flags()]
  public enum KeyModifiers
  {
   None = 0,
   Alt = 1,
   Control = 2,
   Shift = 4,
   Windows = 8
  }

  private void ProcessHotkey()//主處理程序
  {
   strKeys = Clipboard.GetText();
   strKeys.Replace("-", "{TAB}");
   SendKeys.Send(strKeys);
  }

  private Label label1;
  private Label label2;
  private Label label3;
  private Label label4;
  private Label label5;

  string strKeys;

  private void Form1_Load(object sender, System.EventArgs e)
  {
   label2.AutoSize = true;

   Clipboard.Clear();//先清空剪貼板防止剪貼板裏面先複製了其他內容
   RegisterHotKey(Handle, 100, 0, Keys.F10);
  }

  private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  {
   UnregisterHotKey(Handle, 100);//卸載快捷鍵
  }

  protected override void WndProc(ref Message m)//循環監視Windows消息
  {
   const int WM_HOTKEY = 0x0312;//按快捷鍵
   switch (m.Msg)
   {
    case WM_HOTKEY:
     ProcessHotkey();//調用主處理程序
     break;
   }
   base.WndProc(ref m);
  }
 }

<script type="text/javascript"> var arrBaiduCproConfig=new Array(); arrBaiduCproConfig['uid'] =2214; arrBaiduCproConfig['n'] ='sayyescpr'; arrBaiduCproConfig['tm'] =30; arrBaiduCproConfig['cm'] =68; arrBaiduCproConfig['um'] =34; arrBaiduCproConfig['w'] =468; arrBaiduCproConfig['h'] =60; arrBaiduCproConfig['wn'] =2; arrBaiduCproConfig['hn'] =1; arrBaiduCproConfig['ta'] ='right'; arrBaiduCproConfig['tl'] ='bottom'; arrBaiduCproConfig['bu'] =0; arrBaiduCproConfig['bd'] ='#ffffff'; arrBaiduCproConfig['bg'] ='#ffffff'; arrBaiduCproConfig['tt'] ='#0000ff'; arrBaiduCproConfig['ct'] ='#000000'; arrBaiduCproConfig['url'] ='#666666'; arrBaiduCproConfig['bdl'] ='#ffffff'; arrBaiduCproConfig['rad'] =1; </script> <script src="http://cpro.baidu.com/cpro/ui/ui.js" type="text/javascript"> </script> <script type="text/javascript"> </script>
發佈了5 篇原創文章 · 獲贊 0 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章