研究了好几天了,始终搞不定,求求老哥救救孩子,
我这写的是通过窗口名字来获取多个窗口句柄,然后通过句柄来绑定这个窗口用后台的方式点击,
现在出现的问题是,运行成功
就是只有一个窗口释放了左键,其他都在一直按着不释放,问了GTP一天了也没搞定[Python] 纯文本查看 复制代码 import threading
import logging
import win32api
import win32con
import win32gui
import time
logging.basicConfig(level=logging.DEBUG)
def 获取句柄(title):
handles = []
def callback(hwnd, _):
window_title = win32gui.GetWindowText(hwnd)
if title in window_title:
handles.append(hwnd)
return True
win32gui.EnumWindows(callback, None)
return handles
class HtTs:
def __init__(self, handle):
self.handle = handle
self.lock = threading.Lock() # 创建锁对象
def dianji(self, x, y):
with self.lock: # 使用锁保护共享资源
pos = win32api.MAKELONG(int(x), int(y))
try:
# 按下鼠标左键
win32api.SendMessage(self.handle, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, pos)
time.sleep(0.1) # 等待一段时间
# 松开鼠标左键
win32api.SendMessage(self.handle, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, pos)
logging.debug("Mouse button released.")
except Exception as e:
logging.error(f"Error sending mouse click message: {e}")
def print_window_handle(handle):
try:
logging.debug(f"Processing window with handle: {handle}")
htts_instance = HtTs(handle)
htts_instance.dianji(61, 879)
except Exception as e:
logging.error(f"Error in print_window_handle: {e}")
if __name__ == "__main__":
window_title = "都玩"
handles = 获取句柄(window_title)
threads = []
for handle in handles:
t = threading.Thread(target=print_window_handle, args=(handle,))
threads.append(t)
t.start()
for t in threads:
t.join()
|