两种方法:
1.SendMessage or PostMessage
2.keybd_event,mouse_event
前者更强大,指定hwnd后可以后台发送,而后者只能够发送前台信息..也就是必须窗口最前
简单的举例:
方法2
SetCursorPos(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
{
HWND hMainWnd;
DWORD dwProcessID;
};
BOOL CALLBACK EnumWinProc(HWND hwnd, LPARAM lParam)
{
DWORD dwID;
EnumParam* pep = (EnumParam*)lParam;
GetWindowThreadProcessId(hwnd,&dwID);
if (dwID == pep->dwProcessID) {
pep->hMainWnd = hwnd;
return 0;
}
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))
{
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
ep.dwProcessID = pi.dwProcessId;
while(!ep.hMainWnd) {
EnumWindows((WNDENUMPROC)EnumWinProc, (long)&ep);
//没有下面的一行,CPU的使用率会高居不下,同时其它的程序
//也会执行的很慢(包括程序启动的Notepad.exe)
if (ep.hMainWnd==NULL) Sleep(20);
}
//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啦
模拟键盘鼠标事件----自己做外挂
最新推荐文章于 2018-08-28 20:56:42 发布