窗口查找操作
作者:互联网
#include<windows.h> void Find() { TCHAR szTitle[MAX_PATH] = {0}; HWND hwnd = ::FindWindow(TEXT("#32770"), TEXT("飞鸽传书 2007 绿色版")); if(hwnd != NULL) { SetWindowText(hwnd, "Hello!"); } else { MessageBox(NULL, TEXT("NOT FOUND"), TEXT("ERROR"), MB_OK); } } int main(int argc, char* argv[]) { Find(); return 0; }
FindWindow第一个参数为类名,第二个参数为标题
#include<windows.h> typedef void (WINAPI *PSWITCHTOTHISWINDOW) (HWND, BOOL); void WindowControl() { TCHAR szTitle[MAX_PATH] = {0}; HWND hwnd = ::FindWindow(TEXT("#32770"), TEXT("飞鸽传书 2007 绿色版")); if(hwnd != NULL) { PSWITCHTOTHISWINDOW SwitchToThisWindow; HMODULE hUser32 = LoadLibrary("user32.dll"); SwitchToThisWindow = (PSWITCHTOTHISWINDOW) GetProcAddress(hUser32, "SwitchToThisWindow"); SwitchToThisWindow(hwnd, TRUE); Sleep(3000); SendMessage(hwnd, WM_CLOSE, 0, 0); }else { ::MessageBox(NULL, TEXT("NOT FOUND"), TEXT("ERROR"), MB_OK); } } int main(int argc, char* argv[]) { WindowControl(); return 0; }
SwitchToThisWindow作用是切换窗口。由于它没有在头文件中声明,所以必须从dll中导入。
#include<windows.h> void FindChildWindow() { TCHAR szTitle[MAX_PATH] = {0}; HWND hwnd = ::FindWindow(TEXT("#32770"), TEXT("飞鸽传书 2007 绿色版")); if(hwnd != NULL) { /*HWND hEdit = FindWindowEx(hwnd, NULL, TEXT("Edit"), ""); SetWindowText(hEdit, "HelloWorld!"); //设置内容 SendMessage(hEdit, WM_SETTEXT, 0, (LPARAM)"新的内容"); */ HWND hEdit = GetDlgItem(hwnd, 0x3E9); //获取内容 SendMessage(hEdit, WM_GETTEXT, MAX_PATH, (LPARAM) szTitle); //printf("%s", szTitle); SendMessage(hEdit, WM_SETTEXT, 0, (LPARAM) "HelloWorld!"); } else { MessageBox(NULL, TEXT("NOT FOUND"), TEXT("ERROR"), MB_OK); } } int main(int argc, char* argv[]) { FindChildWindow(); return 0; }
查找子窗口的两种方式,第一种是通过父窗口查找子窗口,即使用FindWindowEx。
FindWindowEx(hwnd, //父窗口句柄
NULL, //子窗口句柄
TEXT("Edit"), //子窗口类名
""); //子窗口标题
第二种方式是通过GetDlgItem查找。
#include<windows.h> BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam) { TCHAR szTitle[MAX_PATH] = {0}; GetWindowText(hwnd, szTitle, MAX_PATH); MessageBox(NULL, szTitle, "ChildWindow", MB_OK); return true; } void EnumChildWindow() { TCHAR szTitle[MAX_PATH] = {0}; HWND hwnd = ::FindWindow(TEXT("#32770"), TEXT("飞鸽传书 2007 绿色版")); if(hwnd != NULL) { EnumChildWindows(hwnd, EnumChildProc, 0); } else { MessageBox(NULL, TEXT("NOT FOUND"), TEXT("ERROR"), MB_OK); } } int main(int argc, char* argv[]) { EnumChildWindow(); return 0; }
此程序是遍历子窗口。
#include<windows.h> BOOL CALLBACK EnumOpenWindowProc(HWND hwnd, LPARAM lParam) { TCHAR szTitle[MAX_PATH] = {0}; GetWindowText(hwnd, szTitle, MAX_PATH); MessageBox(NULL, szTitle, "Windows", MB_OK); if(strcmp(szTitle, "飞鸽传书 2007 绿色版") == 0) { MessageBox(NULL, szTitle, "Windows", MB_OK); return false; } return true; } void EnumOpenWindows() { EnumWindows(EnumOpenWindowProc, NULL); } int main(int argc, char* argv[]) { EnumOpenWindows(); return 0; }
枚举所有打开的窗口。
#include<windows.h> void func() { TCHAR szTitle[MAX_PATH] = { 0 }; RECT r; Sleep(5000); HWND hwnd = ::FindWindow(TEXT("TXGuiFoundation"), TEXT("QQ")); if (hwnd != NULL) { //HWND hButton = FindWindowEx(hwnd,NULL,"Button","刷新(&R)"); //HWND hEdit = ::GetDlgItem(hwnd, 0x3E9); //获取窗口坐标 //GetWindowRect(hEdit, &r); //设置鼠标的位置 SetCursorPos(707, 447); //Sleep(1000); //SetCursorPos(621, 399); //鼠标左键单击 mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);//点下左键 mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);//松开左键 //模拟键盘 keybd_event(16, 0, 0, 0); keybd_event(83, 0, 0, 0); keybd_event(83, 0, KEYEVENTF_KEYUP, 0); keybd_event(16, 0, KEYEVENTF_KEYUP, 0); keybd_event(69, 0, 0, 0); keybd_event(69, 0, KEYEVENTF_KEYUP, 0); keybd_event(67, 0, 0, 0); keybd_event(67, 0, KEYEVENTF_KEYUP, 0); keybd_event(50, 0, 0, 0); keybd_event(50, 0, KEYEVENTF_KEYUP, 0); keybd_event(48, 0, 0, 0); keybd_event(48, 0, KEYEVENTF_KEYUP, 0); keybd_event(51, 0, 0, 0); keybd_event(51, 0, KEYEVENTF_KEYUP, 0); keybd_event(56, 0, 0, 0); keybd_event(56, 0, KEYEVENTF_KEYUP, 0); keybd_event(16, 0, 0, 0); keybd_event(87, 0, 0, 0); keybd_event(87, 0, KEYEVENTF_KEYUP, 0); keybd_event(16, 0, KEYEVENTF_KEYUP, 0); keybd_event(84, 0, 0, 0); keybd_event(84, 0, KEYEVENTF_KEYUP, 0); SetCursorPos(686, 508); mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);//点下左键 mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);//松开左键 } else { ::MessageBox(NULL, TEXT("窗口没有找到"), TEXT("[ERROR]"), MB_OK); } } BOOL CALLBACK EnumOpenWindowProc(HWND hWnd, LPARAM lParam) { TCHAR szTitle[MAX_PATH] = { 0 }; ::GetWindowText(hWnd, szTitle, MAX_PATH); if (strcmp((const char *)szTitle, "QQ") == 0) { return FALSE; } return TRUE; } VOID EnumOpenWindows() { EnumWindows(EnumOpenWindowProc, NULL); } void main() { STARTUPINFO si = { 0 }; PROCESS_INFORMATION pi; si.cb = sizeof(si); BOOL res = CreateProcess(TEXT("D:\\QQ\\Bin\\QQ.exe"), NULL, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL,NULL, &si, &pi); func(); }
实现QQ自动输入密码并且登陆
标签:窗口,hwnd,szTitle,查找,keybd,TEXT,操作,NULL,event 来源: https://www.cnblogs.com/Lu3ky-Athena/p/13846716.html