一、建立一个基于对话框的应用程序 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上编译通过。界面如下:
![]() 图一 程序运行的界面 |
只要将所要填写的系列号复制到剪贴板上,然后将光标放置在所要填写的位置,作用Shift+V就可以轻松一次性把系列号填写完。好的创意往往是成功的一半!
文章来源于软件测试时代 http://www.testage.net/
最新专题
最新新闻
排行榜
- 编辑推荐
- 周排行
- 月排行
分类最新内容










