类似案件按键精灵的东西,直接上代码
// 20180116_02.cpp : Defines the entry point for the application. // #include "stdafx.h" #include <windows.h> #include <stdio.h> //FindWindow void test1() { TCHAR szTitle[MAX_PATH]={0}; HWND hwnd = FindWindow(TEXT("WTWindow"),TEXT("用户名 采集 V1.0")); if(hwnd != NULL) { //修改窗口标题 SetWindowText(hwnd,TEXT("www.gyarmy.com")); }else { MessageBox(NULL,TEXT("窗口没有找到"),TEXT("[ERROR]"),MB_OK); } } //SwitchToThisWindow void test2() { TCHAR szTitle[MAX_PATH]={0}; HWND hwnd = FindWindow(TEXT("WTWindow"),TEXT("用户名 采集 V1.0")); if(hwnd!=NULL) { typedef void (WINAPI* PSWITCHTOTHISWINDOW)(HWND,BOOL); PSWITCHTOTHISWINDOW SwitchToThisWindow; HMODULE hUser32 = LoadLibrary("user32.dll"); SwitchToThisWindow = (PSWITCHTOTHISWINDOW)GetProcAddress(hUser32,"SwitchToThisWindow"); //切换窗口 SwitchToThisWindow(hwnd,TRUE); Sleep(2000); SendMessage(hwnd,WM_CLOSE,0,0); }else { MessageBox(NULL,TEXT("窗口没有找到"),TEXT("[ERROR]"),MB_OK); } } //找子窗口 void test3() { TCHAR szTitle[MAX_PATH]={0}; HWND hwnd = FindWindow(TEXT("WTWindow"),TEXT("用户名 采集 V1.0")); if(hwnd != NULL) { //修改窗口标题 HWND hButton = FindWindowEx(hwnd,NULL,TEXT("Button"),TEXT("按钮")); SetWindowText(hButton,TEXT("雇佣兵")); //SendMessage(hButton,WM_SETTEXT,0,(LPARAM)"gyarmy"); }else { MessageBox(NULL,TEXT("窗口没有找到"),TEXT("[ERROR]"),MB_OK); } } //找指定的子窗口 void test4() { TCHAR szTitle[MAX_PATH]={0}; HWND hwnd = FindWindow(TEXT("WTWindow"),TEXT("用户名 采集 V1.0")); if(hwnd != NULL) { //直接通过spy++ 找到对应的控件ID即可进行控制 HWND hEdit = GetDlgItem(hwnd,0x64); //获取内容 SendMessage(hEdit,WM_GETTEXT,MAX_PATH,(LPARAM)szTitle); MessageBox(0,szTitle,0,MB_OK); SendMessage(hEdit,WM_SETTEXT,0,(LPARAM)"www.gyarmy.com"); }else { MessageBox(NULL,TEXT("窗口没有找到"),TEXT("[ERROR]"),MB_OK); } } //遍历所有的子窗口 BOOL CALLBACK EnumChildProc( HWND hwnd, // handle to child window LPARAM lParam // application-defined value ) { TCHAR szTitle[MAX_PATH]={0}; GetWindowText(hwnd,szTitle,MAX_PATH); MessageBox(NULL,szTitle,"[子窗口]",MB_OK); return TRUE; } void test5() { TCHAR szTitle[MAX_PATH]={0}; HWND hwnd = FindWindow(TEXT("WTWindow"),TEXT("用户名 采集 V1.0")); if(hwnd != NULL) { EnumChildWindows(hwnd,EnumChildProc,0); }else { MessageBox(NULL,TEXT("窗口没有找到"),TEXT("[ERROR]"),MB_OK); } } //遍历所有打开的窗口 BOOL CALLBACK EnumWindowsProc( HWND hwnd, // handle to parent window LPARAM lParam // application-defined value ) { TCHAR szTitle[MAX_PATH]={0}; GetWindowText(hwnd,szTitle,MAX_PATH); MessageBox(NULL,szTitle,"[窗口]",MB_OK); if(strcmp(szTitle,"用户名 采集 V1.0")==0) { MessageBox(0,"360软件打开了","【检测】",MB_OK); return FALSE; } return TRUE; } //"用户名 采集 V1.0" void test6() { EnumWindows(EnumWindowsProc,NULL); } //模拟鼠标 void test7() { TCHAR szTitle[MAX_PATH]={0}; HWND hwnd = FindWindow(TEXT("WTWindow"),TEXT("用户名 采集 V1.0")); RECT r; if(hwnd != NULL) { HWND hButton = FindWindowEx(hwnd,NULL,"Button","按钮"); //获取窗口坐标 GetWindowRect(hButton,&r); //printf("%d %d",r.left,r.top); TCHAR sztempStr[100]; sprintf(sztempStr,"%d,%d",r.left,r.top); //MessageBox(0,sztempStr,"22",0); //设置鼠标位置 SetCursorPos(r.left+10,r.top+10); Sleep(1000); SetCursorPos(r.left+5,r.top+5); Sleep(1000); SetCursorPos(r.left+15,r.top+15); Sleep(1000); SetCursorPos(r.left+10,r.top+10); mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); //点下左键 Sleep(2000); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); //松开左键 Sleep(2000); //鼠标左键单击 mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); //点下左键 Sleep(2000); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); //松开左键 MessageBox(0,sztempStr,"22",0); }else { MessageBox(NULL,TEXT("窗口没有找到"),TEXT("[ERROR]"),MB_OK); } } //模拟键盘 void test8() { TCHAR szTitle[MAX_PATH]={0}; HWND hwnd = FindWindow(TEXT("WTWindow"),TEXT("用户名 采集 V1.0")); RECT r; if(hwnd != NULL) { //直接通过spy++ 找到对应的控件ID即可进行控制 HWND hEdit = GetDlgItem(hwnd,0x64); //获取内容 GetWindowRect(hEdit,&r); Sleep(3000); SetCursorPos(r.left+10,r.top+5); Sleep(1000); SetCursorPos(r.left+5,r.top+5); Sleep(1000); SetCursorPos(r.left+6,r.top+5); Sleep(1000); //鼠标左键单击 mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); //点下左键 Sleep(2000); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); //松开左键 Sleep(2000); //鼠标左键单击 mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); //点下左键 Sleep(2000); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); //松开左键 Sleep(1000); //模拟键盘 keybd_event(97,0,0,0); keybd_event(97,0,KEYEVENTF_KEYUP,0); Sleep(1000); keybd_event(66,0,0,0); keybd_event(66,0,KEYEVENTF_KEYUP,0); Sleep(1000); keybd_event(66,0,0,0); keybd_event(66,0,KEYEVENTF_KEYUP,0); Sleep(1000); keybd_event(16,0,0,0); keybd_event(16,0,KEYEVENTF_KEYUP,0); Sleep(1000); keybd_event(67,0,0,0); keybd_event(67,0,KEYEVENTF_KEYUP,0); }else { MessageBox(NULL,TEXT("窗口没有找到"),TEXT("[ERROR]"),MB_OK); } } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // TODO: Place code here. //test1(); //test2(); //test3(); //test4(); //test5(); //test6(); //test7(); test8(); return 0; }
0则评论给“简单测试鼠标键盘事件”