if(!::RegisterHotKey(this->GetSafeHwnd(),0x3333,MOD_SHIFT,0x56)) { ::AfxMessageBox("熱鍵注冊失??!"); this->CloseWindow(); } |
::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(); } |
![]() 圖一 程序運行的界面 |