C++ Win32 遍历窗口
查找指定窗口
#include <iostream> #include <windows.h> using namespace std; int main() { TCHAR szTitle[MAX_PATH] = { 0 }; HWND hWnd = ::FindWindow(L"#32770", L"x32Inject"); if (hWnd != NULL) { // 修改窗口标题 ::SetWindowText(hWnd, L"Hello World!"); } else { ::MessageBox(0, L"窗口没找到", L"提示", 0); } return 0; }
窗口控制
#include <windows.h> int main() { TCHAR szTitle[MAX_PATH] = { 0 }; HWND hWnd = ::FindWindow(L"#32770", L"x32Inject"); if (hWnd != NULL) { // 切换窗口 SwitchToThisWindow(hWnd, false); Sleep(5000); // 关闭窗口 ::SendMessage(hWnd, WM_CLOSE, 0, 0); } else { ::MessageBox(0, L"窗口没找到", L"提示", 0); } return 0; }
查找子窗口
#include <windows.h> int main() { TCHAR szTitle[MAX_PATH] = { 0 }; HWND hWnd = ::FindWindow(L"#32770", L"x32Inject"); if (hWnd != NULL) { // 查找子窗口 HWND hEdit = ::FindWindowEx(hWnd, NULL, L"Edit", L""); // 设置标题 ::SetWindowText(hEdit, L"test"); // 修改内容(本案例,内容和标题是一样的) ::SendMessage(hEdit, WM_SETTEXT, 0, (LPARAM)L"Hello World!"); } else { ::MessageBox(0, L"窗口没找到", L"提示", 0); } return 0; }
#include <windows.h> int main() { TCHAR szTitle[MAX_PATH] = { 0 }; HWND hWnd = ::FindWindow(L"#32770", L"x32Inject"); if (hWnd != NULL) { // 查找子窗口 HWND hEdit = ::GetDlgItem(hWnd, 0x3EA); // 获取内容 ::SendMessage(hWnd, WM_GETTEXT, MAX_PATH, (LPARAM)szTitle); // 修改内容 ::SendMessage(hEdit, WM_SETTEXT, 0, (LPARAM)L"Hello World!"); } else { ::MessageBox(0, L"窗口没找到", L"提示", 0); } return 0; }
枚举子窗口
#include <windows.h> #include <stdio.h> BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam) { TCHAR szTitle[MAX_PATH] = { 0 }; ::GetWindowText(hwnd, szTitle, MAX_PATH); ;; MessageBox(0, szTitle, L"子窗口", MB_OK); return TRUE; } int main() { TCHAR szTitle[MAX_PATH] = { 0 }; HWND hWnd = ::FindWindow(L"#32770", L"x32Inject"); if (hWnd != NULL) { ::EnumChildWindows(hWnd, EnumChildProc, NULL); } else { ::MessageBox(0, L"窗口没找到", L"提示", 0); } getchar(); return 0; }
枚举所有打开的窗口
#include <windows.h> #include <stdio.h> BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) { TCHAR szTitle[MAX_PATH] = { 0 }; ::GetWindowText(hwnd, szTitle, MAX_PATH); wprintf(L"%s\n", szTitle); if (wcscmp(szTitle, L"x32Inject") == 0) { ::MessageBox(0, szTitle, L"窗口", 0); } return TRUE; } int main() { ::EnumWindows(EnumWindowsProc, NULL); getchar(); return 0; }
模拟鼠标点击
#include <windows.h> #include <stdio.h> // 隐藏控制台运行 #pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"") int main() { TCHAR szTitle[MAX_PATH] = { 0 }; RECT r; HWND hWnd = ::FindWindow(L"#32770", L"x32Inject"); if (hWnd != NULL) { HWND hButton = ::FindWindowEx(hWnd, NULL, L"Button", L"选择程序"); // 获取选择程序窗口坐标 ::GetWindowRect(hButton, &r); // 设置鼠标位置 ::SetCursorPos(r.left + 10, r.top + 10); Sleep(3000); // 鼠标左键单击 mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); // 按下左键 mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); // 松开左键 } else { ::MessageBox(0, L"没有找到窗口!", L"提示", MB_OK); } getchar(); return 0; }
模拟键盘点击(搜索:键盘键与虚拟键码对照表)
#include <windows.h> #include <stdio.h> // 隐藏控制台运行 #pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"") int main() { TCHAR szTitle[MAX_PATH] = { 0 }; RECT r; HWND hWnd = ::FindWindow(L"#32770", L"SemaphoreEx"); if (hWnd != NULL) { HWND hEdit = ::GetDlgItem(hWnd, 0x3E8); // 获取选择程序窗口坐标 ::GetWindowRect(hEdit, &r); // 设置鼠标位置 ::SetCursorPos(r.left + 1, r.top + 1); Sleep(1000); // 鼠标左键单击 mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); // 按下左键 mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); // 松开左键 // 模拟键盘 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(16, 0, 0, 0); keybd_event(67, 0, 0, 0); keybd_event(67, 0, KEYEVENTF_KEYUP, 0); keybd_event(16, 0, KEYEVENTF_KEYUP, 0); } else { ::MessageBox(0, L"没有找到窗口!", L"提示", MB_OK); } getchar(); return 0; }