用
VC
编写的
SN
快速输入工具
大家在安装软件时,一般都会遇到让输入序列号
SN
的情况。序列号
(SN)
通常是由“
-
”分隔开的几部分组成,相应地,安装软件输入序列号的地方通常也是由几个文本框
(TextBox)
组成的
,这时,为了输入序列号,我们就要在安装程序和记录有
SN
的文件之间粘贴、复制多次,非常麻烦。从玩电脑到现在,安装了不少软件,只记得有一次,那个安装程序作的比较体贴人意,
可以把这种
SN
一次性粘贴到
SN
的编辑框中。刚才在网上看到了一篇名为“
用C#代码编写的SN快速输入工具
”的文章,和我那次用的情况是一样的,感觉很有意思。就用
VC
也简单实现了一下。
原理很简单,我参考了
[1]
中说的方法,在这里就不赘述了,那位仁兄写的相当好,你可以打开这个链接
http://blog.csdn.net/EoS_3tion/archive/2006/01/23/586527.aspx
看一下。
大致可以把这个程序分为
3
个步骤:
第一步,从剪贴板里面取数据。
第二步,处理取来的数据,根据分隔符把整个
SN
长串分割成几个短字符串备用。
第三步,获得相应文本框的句柄,将处理好的数据添到相应得文本框中。
下面就慢慢介绍用
VC
开发这个程序的过程:
首先,打开
Notepad
,自己随便写一个
SN String
,一会我们要把它粘贴到序列号文本框中。假设这个
SN String
是这样的
ABCDE-FGHKI-SGDJG-IEURR-LPGWQ
。该
SN
分
5
部分,每个部分
5
个字符,部分之间用“-”分割,所以共有
29
个字符。
接着打开
VC6.0
,
创建一个基于
Dialog
的
MFC
程序。我把工程的名字写成了“
AutoSN
”。
然后,打开
Resource View
。见
Figure 1
。编辑
IDD_AUTOSN_DIALOG
,使其最终的界面如
Figure 2
所示。
![Resource View](http://blog.csdn.net/images/blog_csdn_net/hope2jiang/resourceview.jpg)
Figure 1 Resource View
![](http://blog.csdn.net/images/blog_csdn_net/hope2jiang/AutoSN.jpg)
Figure 2 Interface
给按钮
”Get SN”
添加一个
click
事件处理函数,如果你没有修改其他设置的话,该函数的名字会为
OnOK
。下面就是该函数的代码。
void CAutoSNDlg::OnOK()
{
LPTSTR lptstr;
// Open the Clipboard
if ( !OpenClipboard() )
{
AfxMessageBox( "Cannot open the Clipboard" );
return;
}
// ...
// Get the currently selected data
HANDLE hData = GetClipboardData(CF_TEXT);
if(hData != NULL)
{
lptstr = (char *)GlobalLock(hData);
if (lptstr != NULL)
{
// The SN Code string
// "ABCDE-FGHKI-SGDJG-IEURR-LPGWQ"
CString strSN(lptstr);
// ...
// Some code or a function should be here to preprocess
// the sn string to ensure the valid format. Use the
// GetLength() function is far from enough...
if(strSN.GetLength() != 29)
{
AfxMessageBox( "The SN code invalid. it should be in /"XXXXX-XXXXX-XXXXX-XXXXX-XXXXX/" format." );
return;
}
// Extract the five parts
CString s1 = strSN.Mid(0, 5); // Get "ABCDE"
CString s2 = strSN.Mid(6, 5); // Get "FGHKI"
CString s3 = strSN.Mid(12, 5); // Get "SGDJG"
CString s4 = strSN.Mid(18, 5); // Get "IEURR"
CString s5 = strSN.Mid(24, 5); // Get "LPGWQ"
CEdit* pBox;
// Get the first Edit Box
pBox = (CEdit*) GetDlgItem(IDC_EDIT_SN1);
pBox->SetFocus();
pBox->SetSel(0, -1);
// "ABCDE" goes here
pBox->ReplaceSel(s1);
// Get the second Edit Box
pBox = (CEdit*) GetDlgItem(IDC_EDIT_SN2);
pBox->SetFocus();
pBox->SetSel(0, -1);
// "FGHKI" goes here
pBox->ReplaceSel(s2);
// Get the third Edit Box
pBox = (CEdit*) GetDlgItem(IDC_EDIT_SN3);
pBox->SetFocus();
pBox->SetSel(0, -1);
// "SGDJG" goes here
pBox->ReplaceSel(s3);
// Get the fourth Edit Box
pBox = (CEdit*) GetDlgItem(IDC_EDIT_SN4);
pBox->SetFocus();
pBox->SetSel(0, -1);
// "IEURR" goes here
pBox->ReplaceSel(s4);
// SimulateKey tab
// SimulateKey( VK_TAB );
// Get the fifth Edit Box
pBox = (CEdit*) GetDlgItem(IDC_EDIT_SN5);
pBox->SetFocus();
pBox->SetSel(0, -1);
// "LPGWQ" goes here
pBox->ReplaceSel(s5);
GlobalUnlock(hData);
}
else
{
AfxMessageBox("No Data");
}
}
if(!EmptyClipboard())
{
AfxMessageBox("Empty Clipboard Failed.");
}
// ...
CloseClipboard();
}
编译,运行程序。打开记载有
SN
的记事本,将复制其中的
SN
,按下“
Get SN
”按钮。
Figure 3
是运行结果截图。
![](http://blog.csdn.net/images/blog_csdn_net/hope2jiang/AutoSN1.jpg)
Figure 3 Result
在作者原来的文章中也提到了键盘模拟事件
keybd_event
。在
VC
中有一个
keybd_event
的函数。
VOID keybd_event(
BYTE bVk, // virtual-key code
BYTE bScan, // hardware scan code
DWORD dwFlags, // function options
ULONG_PTR dwExtraInfo // additional keystroke data
);
我也写了一个函数,以BYTE类型的virtual-key code变量为参数,也可以达到模拟键盘的效果,也把这个函数贴出来。
void SimulateKey(BYTE key )
{
// Simulate a key press
keybd_event( key,
0x45,
KEYEVENTF_EXTENDEDKEY | 0,
0 );
// Simulate a key release
keybd_event( key,
0x45,
KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
0);
}
参考:
[1] EoS_3tion,用C#代码编写的SN快速输入工具, 2006-02-01,http://blog.csdn.net/EoS_3tion/archive/2006/01/23/586527.aspx