分类: C/C++
2008-03-17 16:54:19
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();
}
三、程序运行