C++ SendInput模拟鼠标点击 按钮

#include <Windows.h>
#include
#include
// 模拟鼠标点击的函数
bool ClickButton(int x, int y) {
// 创建输入
INPUT input = { 0 };
input.type = INPUT_INPUT;
// 移动鼠标到指定的坐标
SetCursorPos(x, y);
// 模拟鼠标左键按下
input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
SendInput(1, &input, sizeof(INPUT));
// 模拟鼠标左键抬起
input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(1, &input, sizeof(INPUT));
// 添加一定的延时,给程序时间处理点击
std::this_thread::sleep_for(std::chrono::milliseconds(200));
// 检查按钮是否响应(这里需要根据具体情况修改)
// 例如,检查某个窗口是否出现
HWND hwnd = FindWindow(NULL, L"Expected Window Title"); // 替换为你期望的窗口标题
if (hwnd == NULL) {
return TRUE; // 返回 TRUE,表示响应
}
return FALSE; // 返回 FALSE,表示没有响应
}
POINT GetButtonPosition(HWND parentHwnd, LPCWSTR buttonName) {
HWND buttonHwnd = FindWindowEx(parentHwnd, NULL, L"Button", buttonName);
POINT buttonPos = { 0, 0 };
if(buttonHwnd != NULL) {
RECT buttonRect;
if (GetWindowRect(buttonHwnd, &buttonRect)) {
// 获取按钮的坐标
buttonPos.x = buttonRect.left + (buttonRect.right - buttonRect.left) / 2; // 中间位置
buttonPos.y = buttonRect.top + (buttonRect.bottom - buttonRect.top) / 2; // 中间位置
}
}

return buttonPos;

}
int main() {
// 假设子窗口的标题为"子窗口标题",请根据实际情况更改
HWND parentHwnd = FindWindow(NULL, L"子窗口标题"); // 查找子窗口的句柄
if (parentHwnd == NULL) {
std::cout << “未找到子窗口!” << std::endl;
return 1;
}
// 获取“确定”按钮的屏幕坐标
LPCWSTR buttonName = L"确定"; // 按钮的名称(可以根据实际情况调整)
POINT buttonPosition = GetButtonPosition(parentHwnd, buttonName);
if (buttonPosition.x != 0 || buttonPosition.y != 0) {
std::cout << “确定按钮坐标: (” << buttonPosition.x << ", " << buttonPosition.y << “)” << std::endl;
} else {
std::cout << “未找到确定按钮!” << std::endl;
}

int x =buttonPosition.x; // X 坐标
int y =buttonPosition.y; // Y 坐标
// 等待5秒,以便让用户切换到包含“确定”按钮的窗口
std::cout << "请在5秒内切换到包含'确定'按钮的窗口..." << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(5));
// 执行鼠标点击并获取响应结果
bool result = ClickButton(x, y);
// 输出结果
if (result) {
    std::cout << "按钮响应: TRUE" << std::endl;
} else {
    std::cout << "按钮响应: FALSE" << std::endl;
}
return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值