当前位置:首页 > 软件开发 > dev
firefox

用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);
  }
 }

 ↓相关文章:
© 2006-2008 All Rights Reserved