模拟鼠标点击目标窗口的客户区坐标
| 364 | |
| 365 | // 模拟鼠标点击目标窗口的客户区坐标 |
| 366 | bool Utils::clickWindowClientArea(HWND hwnd, int x, int y) { |
| 367 | |
| 368 | if(hwnd == nullptr || !IsWindow(hwnd)){ |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | QMutexLocker locker(&m_locker); |
| 373 | // 将客户区坐标转换为屏幕坐标 |
| 374 | POINT clientPoint = { x, y }; |
| 375 | if (!ClientToScreen(hwnd, &clientPoint)) { |
| 376 | qWarning() << "Failed to convert client area coordinates to screen coordinates."; |
| 377 | return false; |
| 378 | } |
| 379 | |
| 380 | // 如果 x, y 是“客户区坐标”,则直接组合到 lParam |
| 381 | // MAKELPARAM 的低 16 位是 X 坐标,高 16 位是 Y 坐标 |
| 382 | LPARAM lParam = MAKELPARAM(x, y); |
| 383 | |
| 384 | // 发送鼠标按下消息 |
| 385 | PostMessage(hwnd, WM_LBUTTONDOWN, MK_LBUTTON, lParam); |
| 386 | Sleep(50); // 给消息处理留一点缓冲时间 |
| 387 | |
| 388 | // 发送鼠标松开消息 |
| 389 | PostMessage(hwnd, WM_LBUTTONUP, 0, lParam); |
| 390 | Sleep(50); |
| 391 | |
| 392 | qDebug() << "Simulated click at client area coordinates: (" |
| 393 | << x << ", " << y << ")" ; |
| 394 | |
| 395 | return true; |
| 396 | |
| 397 | |
| 398 | } |
| 399 | |
| 400 | bool Utils::sendMouseToWindow(HWND hwnd, int buttonEvent, int x, int y) { |
| 401 | if (!hwnd || !IsWindow(hwnd)) return false; |
nothing calls this directly
no outgoing calls
no test coverage detected