找window窗口句柄可以使用spy++或者大漠这样的工具,但是在程序重启时句柄会有变化,所以需要使用函数。
#include "stdafx.h"
#include
#include
#include
1、FindWindow + SetWindowText
//参数:类名 标题
HWND hwnd = ::FindWindow(TEXT("#32770"), TEXT("飞鸽传书 IP Messenger"));
if (hwnd != NULL)
{
//修改窗口标题
::SetWindowText(hwnd, TEXT("新的窗口标题"));
}
else
{
::MessageBox(NULL, TEXT("窗口没有找到"), TEXT("[ERROR]"), MB_OK);
}
2、FindWindow + dll加载 -> SwitchToThisWindow + SendMessage
HWND hwnd = ::FindWindow(TEXT("WeChatLoginWndForPC"), TEXT("登录"));
if (hwnd) {
typedef void (WINAPI *PSWITCHTOTHISWINDOW) (HWND, BOOL);
PSWITCHTOTHISWINDOW SwitchToThisWindow;
HMODULE hUser32 = LoadLibrary(TEXT("user32.dll"));
SwitchToThisWindow = (PSWITCHTOTHISWINDOW)GetProcAddress(hUser32, "SwitchToThisWindow");
//切换窗口:切换到最上层
SwitchToThisWindow(hwnd, false);
Sleep(3000);
//关闭窗口:最小化
::SendMessage(hwnd, WM_CLOSE, 0, 0);
}
else {
::MessageBox(NULL, TEXT("窗口没有找到"), TEXT("[ERROR]"), MB_OK);
}
1、方式一:
HWND hwnd = ::FindWindow(TEXT("#32770"), TEXT("lenovo(LAPTOP-HF4N9SA5)"));
if (hwnd != NULL)
{
//查找子窗口:父窗口句柄 类 标题
HWND hEdit = FindWindowEx(hwnd, NULL, TEXT("RichEdit20W"), TEXT(""));
//设置标题
::SetWindowText(hEdit, TEXT("文本框新的标题"));
//修改内容
::SendMessage(hEdit, WM_SETTEXT, 0, (LPARAM)"1");
}
else
{
::MessageBox(NULL, TEXT("窗口没有找到"), TEXT("[ERROR]"), MB_OK);
}
2、同类名、同内容的情况(就是上面的那种情况,使用的是飞秋软件)。
使用GetDlgItem函数;
这时候就可以通过控件ID来查找:spy++查找之后确定、右键、属性就可以查看ID,注意此时的ID是16进制的。
TCHAR szTitle[MAX_PATH] = { 0 };
HWND hwnd = ::FindWindow(TEXT("#32770"), TEXT("lenovo(LAPTOP-HF4N9SA5)"));
if (hwnd != NULL)
{
//查找子窗口
HWND hEdit = ::GetDlgItem(hwnd, 0x480);
//获取内容
::SendMessage(hEdit, WM_GETTEXT, MAX_PATH, (LPARAM)szTitle);
//修改内容
::SendMessage(hEdit, WM_SETTEXT, 0, (LPARAM)"111");
}
else
{
::MessageBox(NULL, TEXT("窗口没有找到"), TEXT("[ERROR]"), MB_OK);
}
printf("%s", szTitle);
控件的本质就是窗口,所以这里也可以表示为所有控件;
EnumChildWindows需要传入一个回调函数,重复调用直到遍历完成或者返回False为止。
BOOL CALLBACK EnumChildProc(HWND hWnd, LPARAM lParam)
{
TCHAR szTitle[MAX_PATH] = { 0 };
::GetWindowText(hWnd, szTitle, MAX_PATH);
MessageBox(NULL, szTitle, TEXT("[子窗口]"), MB_OK);
return true;
}
VOID EnumChildWindow()
{
TCHAR szTitle[MAX_PATH] = { 0 };
HWND hWnd = ::FindWindow(TEXT("#32770"), TEXT("新的窗口标题"));
if (hWnd != NULL)
{
::EnumChildWindows(hWnd, EnumChildProc, 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);
//MessageBox(NULL, szTitle, TEXT("[窗口]"), MB_OK);
if (lstrcmpW(szTitle, TEXT("登录")) == 0)
{
MessageBox(NULL, szTitle, TEXT("[窗口]"), MB_OK);
return FALSE;
}
return TRUE;
}
VOID EnumOpenWindows()
{
EnumWindows(EnumOpenWindowProc, NULL);
}
隐藏控制台
#pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
模拟鼠标单击
TCHAR szTitle[MAX_PATH] = {0};
RECT r;
HWND hwnd = ::FindWindow(TEXT("#32770"),TEXT("飞鸽传书 IP Messenger"));
if(hwnd != NULL)
{
HWND hButton = FindWindowEx(hwnd,NULL,"Button","刷新(&R)");
//获取窗口坐标
::GetWindowRect(hButton,&r);
printf("%d %d",r.left,r.top);
//设置鼠标的位置
::SetCursorPos(r.left+10,r.top+10);
Sleep(2000);
//鼠标左键单击
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);//点下左键
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);//松开左键
}
else
{
::MessageBox(NULL,TEXT("窗口没有找到"),TEXT("[ERROR]"),MB_OK);
}
模拟键盘点击(搜索:键盘键与虚拟键码对照表)
TCHAR szTitle[MAX_PATH] = {0};
RECT r;
HWND hwnd = ::FindWindow(TEXT("#32770"),TEXT("飞鸽传书 IP Messenger"));
if(hwnd != NULL)
{
//HWND hButton = FindWindowEx(hwnd,NULL,"Button","刷新(&R)");
HWND hEdit =::GetDlgItem(hwnd,0x3E9);
//获取窗口坐标
::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(NULL,TEXT("窗口没有找到"),TEXT("[ERROR]"),MB_OK);
}
可以完成一个模拟QQ登录的功能。