选择显示字体大小

vc编写简单的序列号(sn)填写器


  前几天在网上看到有个软件叫sncopy,用来辅助填写系列号(sn)的。创意觉得还是挺好的。装软件的时候经常要填写系列号,而很多系列号都要分节来填写,没法用ctrl+c和ctrl+v(复制和粘贴)来一次性搞定,只能分节的复制和粘贴,很是麻烦。sncopy就是来帮我们解决这个问题的。感觉这个不是很难做,就是从剪贴板上获取整个系列号,然后进行分解,依次填入即可。于是动手自己也做一个!

  一、建立一个基于对话框的应用程序 snpaste(过程略)

  二、编写代码

  我们使用shift+v作为快捷键,以此来快速地一次性地填写整个系列号。先进行热键的注册。在initdialog()中添加如下代码:

if(!::registerhotkey(this->getsafehwnd(),0x3333,mod_shift,0x56))
{
 ::afxmessagebox("热键注册失败!");
 this->closewindow();
}

  在程序退出前必须注销热键。在onclose()中:

::unregisterhotkey(this->getsafehwnd(),0x3333);

  响应热键:

lresult csnpastedlg::onhotkey(wparam wparam,lparam lparam)
{
 if(!openclipboard())
 {
  ::afxmessagebox("无法打开粘贴板!");
  return -1;
 }
 cstring str=cstring((char*)::getclipboarddata(cf_text));
 cstring oldstr=str;//保存原来的字串
 closeclipboard();
 str.trim();
 cstring strtemp;
 int find_i=str.find(''-'');
 if(find_i!=-1)//系列号中有“-”的,以此来划分系列号字串
 {
  while(find_i!=-1)
  {
   strtemp=str.left(find_i);
   str=str.mid(find_i+1);
   find_i=str.find(''-'');
   sleep(100);//由于剪贴板操作比较慢,必须加一定的延时,否则数据会出错。
   this->sendstrtoclipboard(strtemp);//将分解得到的一小节字串复制到剪贴板
   this->performctrlv();//模仿键盘击键ctrl+v
   this->performclicktab();//模仿键盘击键tab
  }
  if(!str.isempty())
  {
   this->sendstrtoclipboard(str);
   this->performctrlv();
   this->performclicktab();
  }
 }
 else//系列号字串中没有“-”,有预先设定的长度来划分。
 { 
  while(!str.isempty())
  {
   strtemp=str.left(this->m_spinctrl.getpos());
   str=str.mid(this->m_spinctrl.getpos());
   sleep(100);
   this->sendstrtoclipboard(strtemp);
   this->performctrlv();
   this->performclicktab();
  }
 }
 sleep(100);
 this->sendstrtoclipboard(oldstr);//恢复原来剪贴板上的数据
 return 1;
}

  以下是键盘击键动作的模仿

void csnpastedlg::performctrlv(void)
{
 ::keybd_event(vk_control,0,0,0);//按ctrl,不放开
 ::keybd_event(0x56,0,0,0);//v key;再按v键不放开
 ::keybd_event(0x56,0,keyeventf_keyup,0);//放开v键
 ::keybd_event(vk_control,0,keyeventf_keyup,0);//放开ctrl键
}
void csnpastedlg::performclicktab(void)
{
 ::keybd_event(vk_tab,0,0,0);//按tab键不放
 ::keybd_event(vk_tab,0,keyeventf_keyup,0);//放开tab键
}

  以下是把字串送到剪贴板

void csnpastedlg::sendstrtoclipboard(cstring str)
{
 if(!openclipboard())
 {
  ::afxmessagebox("无法打开粘贴板!");
  return ;
 }
 emptyclipboard();//清空
 hglobal hglo;
 hglo=globalalloc(gptr,str.getlength()+1);//申请全局空间
 if(hglo==null)
 {
  ::afxmessagebox("申请内存失败!");
  return ;
 }
 lpbyte pbyte=(lpbyte)globallock(hglo);
 memcpy(pbyte,str.getbuffer(),str.getlength());
 str.releasebuffer();
 globalunlock(hglo);
 setclipboarddata(cf_text,hglo);//将数据送到剪贴板
 closeclipboard();
}

  三、程序运行

  程序在visual c++7.1上编译通过。界面如下:


图一 程序运行的界面


 


关键字 本文所属关键字

  • net  

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范