使用C#编写的SN输入工具的方法
按照网上的一篇文章改写的代码,主要应用了WinAPI。
获取剪切板上的数据然后将序列号中“-”转化成TAB键(进入下一个序列号输入框)
具体代码如下(测试环境VS.NET2005+WINXPSP2)
红色的字体是本功能的核心代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[Flags()]
public enum KeyModifiers
{
None=0,Alt=1,Control=2,Shift=4,Windows=8
}
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, KeyModifiers fsModifiers, Keys vk);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
string strKeys;
private void ProcessHotKey()
{
strKeys = Clipboard.GetText();
strKeys = strKeys.Replace("-", "{TAB}");
SendKeys.Send(strKeys);
}
private void Form1_Load(object sender, EventArgs e)
{
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)
{
const int WM_HOTKEY = 0x0312;
switch (m.Msg)
{
case WM_HOTKEY:
ProcessHotKey();
break;
}
base.WndProc(ref m);
}
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true;//点击“X”不能关闭
base.OnClosing(e);
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
this.Dispose();
}
private void button1_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.Append("--使用帮助--/r/n");
sb.Append("1.复制序列号到剪切板;/r/n");
sb.Append("2.将鼠标定位到需要填写序列号的文本框;/r/n");
sb.Append("3.按下“F10”;/r/n");
sb.Append("4.OK!祝你使用愉快!/r/n");
MessageBox.Show(sb.ToString(),"使用说明");
}
private void button3_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
this.notifyIcon1.Visible = false;
}
FormWindowState preState;
private void Form1_Resize(object sender, EventArgs e)//窗体大小改变前保存窗体的状态
{
if (this.WindowState != FormWindowState.Minimized)
{
preState = this.WindowState;
}
}
private void Form1_Deactivate(object sender, EventArgs e)//窗体不活动时的事件处理
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Visible = false;
}
}
private void notifyIcon1_Click(object sender, EventArgs e)//notifyIcon的点击事件
{
if (this.Visible)
this.Visible = false;
else
this.Visible = true;
if (this.WindowState == FormWindowState.Minimized)
this.WindowState = preState;
else
this.WindowState = FormWindowState.Minimized;
}
}
}
本文地址:http://www.45fan.com/dnjc/69336.html