VC指定視窗模擬按鍵或滑鼠事件

2021-06-22 23:14:19 字數 1859 閱讀 5969

方法有兩種:

1.sendmessage or postmessage

2.keybd_event,mouse_event

前者更強大,指定hwnd後可以後台傳送,而後者只能夠傳送前台資訊..也就是必須視窗最前

簡單的舉例:

方法2setcursorpos(x,y);

mouse_event(mouseeventf_leftdown,0,0,0,0);

mouse_event(mouseeventf_leftup,0,0,0,0);

keybd_event(k,0,0,0);

keybd_event(k,0,keyeventf_keyup,0);

方法1以當前程式的某個輸入框為例:

hwnd tw2 = getdlgitem(idc_edit1)->m_hwnd;

::sendmessage(tw2,wm_char,s[i],0);

是個按鈕:

hwnd tw3 = getdlgitem(idok)->m_hwnd;

::sendmessage(tw3,wm_lbuttondown,0,0);

::sendmessage(tw3,wm_lbuttonup,0,0);

以記事本為例

wnd = ::findwindow("notepad",   null);  

wnd   =   findwindowex(wnd,0,"edit",null);  

::sendmessage(tw2,wm_char,s[i],0);

有人就說了,我不知道名字叫edit啊,有下面的方法:

cwnd * pwnd = findwindow("notepad",   null);

cwnd * p2 = pwnd->gettopwindow();

wnd = p2->m_hwnd;

::sendmessage(tw2,wm_char,s[i],0);

又有人說了,如果連notepad也不知道呢...

我說,程序id總知道了吧...遍歷程序池得到想要的程序id,然後找到指定id的hwnd

struct   enumparam  

;  bool   callback   enumwinproc(hwnd   hwnd,   lparam   lparam)  

return   true;  

}enumparam   ep;  

startupinfo   si;  

process_information   pi;  

ep.hmainwnd   =   null;  

memset(&si,   0,   sizeof(si));  

si.cb   =   sizeof(startupinfo);  

if   (createprocess(null,"notepad.exe   c:.txt",  

null,null,false,0,null,null,&si,&pi))    

//enumwindows((wndenumproc)enumwinproc, (long)&ep);  

}  //此時的   ep.hmainwnd   就是你要的nodepad.exe視窗的控制代碼  

//接下來由hwnd得到cwnd *

wnd = ep.hmainwnd;

if(wnd == null) afxmessagebox("fasle");

cwnd * tc = fromhandle(wnd);

//獲取

wnd = tc->gettopwindow()->m_hwnd;

::sendmessage(tw2,wm_char,s[i],0);

//ok啦

遍歷程序的**下次再發了

VC 常用模擬鍵鼠偽程式碼

模擬鍵盤輸入例子 char userid 100 abc sendmessagea m this m hnamewnd,wm settext,wparam sizeof userid sizeof char lparam userid 模擬鍵盤操作例子 postmessage m this m hw...

C 視窗判斷,模擬按鍵,設定視窗最大化

dllimport user32.dll static extern void keybd event byte bvk,byte bscan,uint dwflags,uint dwextrainfo dllimport user32.dll static extern byte mapvirtu...

VC 啟動或停止指定的系統後台服務

write by jruinet ntservicecontroldlg.cpp 實現檔案 include stdafx.h include ntservicecontrol.h include ntservicecontroldlg.h include ntservicecontroldlg.h ...