win32gui点击Bluestacks中的坐标

发布于 2025-02-08 06:04:57 字数 1452 浏览 3 评论 0原文

我正在尝试在BlueStacks中自动化一个过程,在该过程中,我等待元素出现在屏幕上,当它出现时,单击它。除了点击部分外,一切都有效,我已经做了很多研究并尝试了不同的事情,但是我无法正常工作。

我知道我可以使用pyautogui轻松做到这一点,但是我不想使程序移动实际光标,关键功能之一是它可以运行而无需将窗口放在前景上(不要集中精力窗户)。

def compare_images():
    threshold = 0.80
    i = -1
    width = 0
    height = 0
    mouse_x = 0
    mouse_y = 0
    for template in templates:
        i += 1
        result_image = cv2.matchTemplate(scr, template, cv2.TM_CCOEFF_NORMED)
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result_image)

        width = templates[i].shape[1]
        height = templates[i].shape[0]

        if max_val > threshold:
            # Getting the mouse coordinates
            mouse_x = math.floor(max_loc[0] + width / 2)
            mouse_y = math.floor(max_loc[1] + height / 2)
            l_param = win32api.MAKELONG(mouse_x, mouse_y)
            
            # Trying to click on the coordinates
            win32gui.PostMessage(hwnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, l_param)
            win32gui.PostMessage(hwnd, win32con.WM_LBUTTONUP, 0, l_param)

    print(f"Max Val: {max_val} Max Loc: {max_loc}")

    cv2.rectangle(scr, max_loc, (max_loc[0] + width, max_loc[1] + height), (0, 255, 255), 2)
    cv2.putText(scr, f"Max Loc: {max_loc}", max_loc, cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2)
    cv2.rectangle(scr, (mouse_x, mouse_y), (mouse_x, mouse_y), (255, 255, 255), 10)

I'm trying to automate a process in BlueStacks where I wait for an element to appear on the screen, and when it appears, click it. Everything works, except the clicking part, I've done a lot of research and tried different things, but I can't get it to work.

I know I can make this easily with pyautogui, but I don't want to make the program move the actual cursor, and one of the key features is that it can be run without needing to have the window on the foreground (not focusing the window).

def compare_images():
    threshold = 0.80
    i = -1
    width = 0
    height = 0
    mouse_x = 0
    mouse_y = 0
    for template in templates:
        i += 1
        result_image = cv2.matchTemplate(scr, template, cv2.TM_CCOEFF_NORMED)
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result_image)

        width = templates[i].shape[1]
        height = templates[i].shape[0]

        if max_val > threshold:
            # Getting the mouse coordinates
            mouse_x = math.floor(max_loc[0] + width / 2)
            mouse_y = math.floor(max_loc[1] + height / 2)
            l_param = win32api.MAKELONG(mouse_x, mouse_y)
            
            # Trying to click on the coordinates
            win32gui.PostMessage(hwnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, l_param)
            win32gui.PostMessage(hwnd, win32con.WM_LBUTTONUP, 0, l_param)

    print(f"Max Val: {max_val} Max Loc: {max_loc}")

    cv2.rectangle(scr, max_loc, (max_loc[0] + width, max_loc[1] + height), (0, 255, 255), 2)
    cv2.putText(scr, f"Max Loc: {max_loc}", max_loc, cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2)
    cv2.rectangle(scr, (mouse_x, mouse_y), (mouse_x, mouse_y), (255, 255, 255), 10)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文