内容载入中...
一般软件都要输入序列号( 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( );
//
// TOD 在 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 );
}
}
}