win32:模拟点击而不模拟鼠标移动?

编程入门 行业动态 更新时间:2024-10-27 12:39:31
本文介绍了win32:模拟点击而不模拟鼠标移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试模拟在窗口上单击鼠标.我目前按如下方式成功执行此操作(我使用的是 Python,但它应该适用于一般的 win32):

I'm trying to simulate a mouse click on a window. I currently have success doing this as follows (I'm using Python, but it should apply to general win32):

win32api.SetCursorPos((x,y)) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)

这很好用.但是,如果在我手动移动鼠标时发生点击,光标位置会被抛出.有没有办法直接将点击发送到给定的 (x,y) 坐标而无需将鼠标移动到那里?我尝试过类似以下的方法,但运气不佳:

This works fine. However, if the click happens while I'm moving the mouse manually, the cursor position gets thrown off. Is there any way to send a click directly to a given (x,y) coordinate without moving the mouse there? I've tried something like the following with not much luck:

nx = x*65535/win32api.GetSystemMetrics(0) ny = y*65535/win32api.GetSystemMetrics(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN | win32con.MOUSEEVENTF_ABSOLUTE,nx,ny) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP | win32con.MOUSEEVENTF_ABSOLUTE,nx,ny)

推荐答案

尝试 WindowFromPoint() 函数:

Try WindowFromPoint() function:

POINT pt; pt.x = 30; // This is your click coordinates pt.y = 30; HWND hWnd = WindowFromPoint(pt); LPARAM lParam = MAKELPARAM(pt.x, pt.y); PostMessage(hWnd, WM_RBUTTONDOWN, MK_RBUTTON, lParam); PostMessage(hWnd, WM_RBUTTONUP, MK_RBUTTON, lParam);

更多推荐

win32:模拟点击而不模拟鼠标移动?

本文发布于:2023-10-08 13:50:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472818.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:鼠标   而不

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!