一般軟件都要輸入序列號(SN)
而軟件輸入序列號的地方通常都是幾個文本框(TextBox)組成
當然這些都和我的編寫這個程序的原因無關
要做這個程序
既然是要處理復制的序列號
於是就用到了string Clipboard
第一步
接著
再查詢Tab鍵的寫法就是{Tab}
那麼我只要將原來文本strKeys中的strKeys
SendKeys
這兩行代碼
這樣就有了我的程序的主過程private void ProcessHotkey()//主處理程序
{
strKeys = Clipboard
strKeys
SendKeys
}
但是我們怎麼通過快捷鍵來觸發
於是我開始在百度和MSDN查找相關處理全局快捷鍵的windows api的資料
要設置快捷鍵必須使用userBOOL RegisterHotKey(
HWND hWnd
int id
UINT fsModifiers
UINT vk
);
和BOOL UnregisterHotKey(
HWND hWnd
int id
);
轉換成C#代碼[DllImport(
public static extern bool RegisterHotKey(
IntPtr hWnd
int id
KeyModifiers fsModifiers
Keys vk // virtual
);
[DllImport(
public static extern bool UnregisterHotKey(
IntPtr hWnd
int id // hot key identifier
);
[Flags()]
public enum KeyModifiers
{
None =
Alt =
Control =
Shift =
Windows =
}
這是注冊和卸載全局快捷鍵的方法
於是有了private void Form
{
label
Clipboard
RegisterHotKey(Handle
}
private void Form
{
UnregisterHotKey(Handle
}
那麼我們在別的窗口
那麼我們就必須重寫WndProc()方法protected override void WndProc(ref Message m)//監視Windows消息
{
const int WM_HOTKEY =
switch (m
{
case WM_HOTKEY:
ProcessHotkey();//調用主處理程序
break;
}
base
}
這樣我的程序就完成了
using System;
using System Drawing;
using System Collections;
using System ComponentModel;
using System Windows Forms;
using System Data;
using System Runtime InteropServices;
namespace WindowsApplication
{
///
/// Form 的摘要說明
///
public class Form : System Windows Forms Form
{
///
/// 必需的設計器變量
///
private System ComponentModel Container components = null;
public Form ()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
//
}
///
/// 清理所有正在使用的資源
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components Dispose();
}
}
base Dispose( disposing );
}
#region Windows 窗體設計器生成的代碼
///
/// 設計器支持所需的方法 不要使用代碼編輯器修改
/// 此方法的內容
///
private void InitializeComponent()
{
this label = new System Windows Forms Label();
this label = new System Windows Forms Label();
this label = new System Windows Forms Label();
this label = new System Windows Forms Label();
this label = new System Windows Forms Label();
this SuspendLayout();
//
// label
//
this label AutoSize = true;
this label Location = new System Drawing Point( );
this label Name = label ;
this label Size = new System Drawing Size( );
this label TabIndex = ;
this label Text = EoS tion制作 ;
//
// label
//
this label AutoSize = true;
this label Location = new System Drawing Point( );
this label Name = label ;
this label Size = new System Drawing Size( );
this label TabIndex = ;
this label Text = 使用方法 ;
//
// label
//
this label AutoSize = true;
this label Location = new System Drawing Point( );
this label Name = label ;
this label Size = new System Drawing Size( );
this label TabIndex = ;
this label Text = 將序列號拷貝到剪切板 ;
//
// label
//
this label AutoSize = true;
this label Location = new System Drawing Point( );
this label Name = label ;
this label Size = new System Drawing Size( );
this label TabIndex = ;
this label Text = 將光標定位到序列號輸入處 ;
//
// label
//
this label AutoSize = true;
this label Location = new System Drawing Point( );
this label Name = label ;
this label Size = new System Drawing Size( );
this label TabIndex = ;
this label Text = 按F 鍵 ;
//
// Form
//
this AutoScaleBaseSize = new System Drawing Size( );
this ClientSize = new System Drawing Size( );
this Controls Add(this label );
this Controls Add(this label );
this Controls Add(this label );
this Controls Add(this label );
this Controls Add(this label );
this Name = Form ;
this Text = SN輸入工具(C#版Version ) ;
this FormClosing += new System Windows Forms FormClosingEventHandler(this Form _FormClosing);
this Load += new System EventHandler(this Form _Load);
this ResumeLayout(false);
this PerformLayout();
}
#endregion
///
/// 應用程序的主入口點
///
[STAThread]
static void Main()
{
Application Run(new Form ());
}
[DllImport( user 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( user dll SetLastError=true)]
public static extern bool UnregisterHotKey( IntPtr hWnd
// handle to window
int id // hot key identifier
);
[Flags()]
public enum KeyModifiers
{
None =
Alt =
Control =
Shift =
Windows =
}
private void ProcessHotkey()//主處理程序
{
strKeys = Clipboard GetText();
strKeys Replace( {TAB} );
SendKeys Send(strKeys);
}
private Label label ;
private Label label ;
private Label label ;
private Label label ;
private Label label ;
string strKeys;
private void Form _Load(object sender System EventArgs e)
{
label AutoSize = true;
Clipboard Clear();//先清空剪貼板防止剪貼板裡面先復制了其他內容
RegisterHotKey(Handle Keys F );
}
private void Form _FormClosing(object sender FormClosingEventArgs e)
{
UnregisterHotKey(Handle );//卸載快捷鍵
}
protected override void WndProc(ref Message m)//循環監視Windows消息
{
const int WM_HOTKEY = x ;//按快捷鍵
switch (m Msg)
{
case WM_HOTKEY:
ProcessHotkey();//調用主處理程序
break;
}
base WndProc(ref m);
}
}
}
From:http://tw.wingwit.com/Article/program/net/201311/11597.html