python win32模拟点击 [英] python win32 simulate click

查看:119
本文介绍了python win32模拟点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个要模拟鼠标点击的窗口在特定的 x, y 坐标处.我已经有了 hwnd 但我不确定如何构造 lParam.我过去使用过 SendMessage 点击按钮等,但我知道他们的hwnds.任何帮助将不胜感激.我也忍不住想知道我正在以正确的方式解决这个问题.我的最终目标是点击某个Skype 主窗口上的用户(例如).我使用了 EnumChildWindows找到所有主窗口的孩子,但找不到合适的孩子.所以我想我会尝试使用坐标点击"它.

Let's say I've got a window for which I want to simulate a mouse click at a specific x, y coordinate. I already have the hwnd but I'm not sure how to construct the lParam. I've used SendMessage in the past to click on buttons, etc., but I knew their hwnds. Any help would be greatly appreciated. I also can't help but wonder if I'm going about this the right way. My end goal is clicking on a certain user on skype main window (for example). I used EnumChildWindows to find all the main window's children, but couldn't find the right one. So figured I would try to 'click' on it using coordinates.

推荐答案

感谢 ctypes,您可以使用低级 windows api.看下面的例子(改编自我没有测试过的东西,但应该没问题)

You can use the low level windows api thanks to ctypes. See an example below (adapted from something i didn't test it but it should be ok)

import ctypes
MOUSEEVENTF_MOVE = 0x0001 # mouse move
MOUSEEVENTF_ABSOLUTE = 0x8000 # absolute move
MOUSEEVENTF_MOVEABS = MOUSEEVENTF_MOVE + MOUSEEVENTF_ABSOLUTE

MOUSEEVENTF_LEFTDOWN = 0x0002 # left button down 
MOUSEEVENTF_LEFTUP = 0x0004 # left button up 
MOUSEEVENTF_CLICK = MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP

def click(x, y):
    #move first
    x = 65536L * x / ctypes.windll.user32.GetSystemMetrics(0) + 1
    y = 65536L * y / ctypes.windll.user32.GetSystemMetrics(1) + 1
    ctypes.windll.user32.mouse_event(MOUSEEVENTF_MOVEABS, x, y, 0, 0)

    #then click
    ctypes.windll.user32.mouse_event(MOUSEEVENTF_CLICK, 0, 0, 0, 0)

更新:我没有测试下面的代码,但我认为它应该可以帮助你写一些东西来获得孩子的位置.然后你可以尝试在正确的位置点击.

Update: I didn't test the code below but I think that it should help you to write something to get the child position. Then you can try to click at the correct position.

CHILD= None

def the_callback(child_hwnd, regex):
    '''Pass to win32gui.EnumWindows() to check all the opened windows'''
    if re.match(regex, win32gui.GetWindowText(child_hwnd)):
        CHILD= child_hwnd

win32gui.EnumChildWindows(hwnd, the_callback, regex)


if CHILD:
    (x_tl, y_tl, x_br, y_br) = win32gui.GetWindowRect(CHILD)

这篇关于python win32模拟点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆