164 lines
6.2 KiB
Python
164 lines
6.2 KiB
Python
import pyautogui
|
||
import time
|
||
import os
|
||
import numpy as np
|
||
from PIL import Image, ImageGrab
|
||
import win32gui
|
||
import win32con
|
||
import win32process
|
||
import psutil
|
||
|
||
def find_wechat_window():
|
||
"""查找微信窗口并将其激活"""
|
||
# 方法1: 通过窗口标题查找
|
||
wechat_window = win32gui.FindWindow(None, "微信")
|
||
if wechat_window == 0:
|
||
# 备用方法: 尝试查找部分标题匹配的窗口
|
||
def callback(hwnd, windows):
|
||
if win32gui.IsWindowVisible(hwnd) and "微信" in win32gui.GetWindowText(hwnd):
|
||
windows.append(hwnd)
|
||
return True
|
||
|
||
windows = []
|
||
win32gui.EnumWindows(callback, windows)
|
||
|
||
if windows:
|
||
wechat_window = windows[0]
|
||
|
||
# 方法2: 如果窗口标题查找失败,通过进程名查找
|
||
if wechat_window == 0:
|
||
print("通过窗口标题未找到微信,尝试通过进程查找...")
|
||
for proc in psutil.process_iter(['pid', 'name']):
|
||
if proc.info['name'] and 'WeChat' in proc.info['name']:
|
||
pid = proc.info['pid']
|
||
# 查找与此PID关联的窗口
|
||
def enum_windows_callback(hwnd, target_pid):
|
||
_, found_pid = win32process.GetWindowThreadProcessId(hwnd)
|
||
if found_pid == target_pid and win32gui.IsWindowVisible(hwnd):
|
||
target_pid.append(hwnd)
|
||
return True
|
||
|
||
target_hwnds = []
|
||
win32gui.EnumWindows(lambda hwnd, param: enum_windows_callback(hwnd, target_hwnds), pid)
|
||
|
||
if target_hwnds:
|
||
wechat_window = target_hwnds[0]
|
||
break
|
||
|
||
if wechat_window != 0:
|
||
print(f"找到微信窗口: {win32gui.GetWindowText(wechat_window)}")
|
||
# 还原最小化的窗口
|
||
if win32gui.IsIconic(wechat_window):
|
||
win32gui.ShowWindow(wechat_window, win32con.SW_RESTORE)
|
||
# 激活并前置窗口
|
||
win32gui.SetForegroundWindow(wechat_window)
|
||
return True
|
||
else:
|
||
print("未找到微信窗口")
|
||
return False
|
||
|
||
def is_green_pixel(r, g, b):
|
||
"""判断像素是否为绿色"""
|
||
# 微信的绿色按钮大约是 RGB(7, 193, 96)
|
||
return g > 150 and g > r*1.5 and g > b*1.5
|
||
|
||
def find_and_click_wechat_login():
|
||
"""查找并点击微信登录按钮"""
|
||
# 切换到微信窗口
|
||
if not find_wechat_window():
|
||
print("无法找到微信窗口,正在尝试启动微信...")
|
||
try:
|
||
# 尝试启动微信(常见安装路径)
|
||
wechat_paths = [
|
||
r"C:\Program Files (x86)\Tencent\WeChat\WeChat.exe",
|
||
r"C:\Program Files\Tencent\WeChat\WeChat.exe",
|
||
r"D:\Program Files (x86)\Tencent\WeChat\WeChat.exe",
|
||
r"D:\Program Files\Tencent\WeChat\WeChat.exe"
|
||
]
|
||
|
||
for path in wechat_paths:
|
||
if os.path.exists(path):
|
||
os.startfile(path)
|
||
print(f"正在启动微信: {path}")
|
||
time.sleep(5) # 等待微信启动
|
||
if find_wechat_window():
|
||
break
|
||
else:
|
||
print("未找到微信安装路径,请手动启动微信")
|
||
return
|
||
except Exception as e:
|
||
print(f"启动微信时出错: {e}")
|
||
return
|
||
|
||
# 等待窗口完全加载
|
||
time.sleep(2)
|
||
|
||
try:
|
||
print("尝试查找登录按钮...")
|
||
# 截取屏幕
|
||
screenshot = ImageGrab.grab()
|
||
screenshot_np = np.array(screenshot)
|
||
|
||
# 创建一个绿色区域的掩码
|
||
green_areas = []
|
||
width, height = screenshot.size
|
||
|
||
# 分析图像,识别绿色区域
|
||
for y in range(0, height, 5): # 每5个像素采样一次以提高效率
|
||
for x in range(0, width, 5):
|
||
r, g, b = screenshot.getpixel((x, y))
|
||
if is_green_pixel(r, g, b):
|
||
# 发现绿色像素,向四周扩散检查是否为按钮
|
||
left, top, right, bottom = x, y, x, y
|
||
# 向右扩散
|
||
for nx in range(x, min(x+300, width)):
|
||
r, g, b = screenshot.getpixel((nx, y))
|
||
if not is_green_pixel(r, g, b):
|
||
break
|
||
right = nx
|
||
# 向下扩散
|
||
for ny in range(y, min(y+60, height)):
|
||
r, g, b = screenshot.getpixel((x, ny))
|
||
if not is_green_pixel(r, g, b):
|
||
break
|
||
bottom = ny
|
||
|
||
width_area = right - left
|
||
height_area = bottom - top
|
||
|
||
# 如果区域符合按钮尺寸
|
||
if 100 < width_area < 300 and 30 < height_area < 60:
|
||
green_areas.append((left, top, right, bottom))
|
||
|
||
login_button_found = False
|
||
for left, top, right, bottom in green_areas:
|
||
# 点击按钮中心
|
||
center_x = (left + right) // 2
|
||
center_y = (top + bottom) // 2
|
||
pyautogui.click(center_x, center_y)
|
||
print(f"已点击位置: ({center_x}, {center_y})")
|
||
login_button_found = True
|
||
break
|
||
|
||
# 方法2:如果图像识别失败,尝试使用固定位置
|
||
if not login_button_found:
|
||
print("未通过图像识别找到登录按钮,尝试使用备用方法...")
|
||
|
||
# 获取屏幕分辨率
|
||
screen_width, screen_height = pyautogui.size()
|
||
|
||
# 估计登录按钮位置 (通常在窗口中下部偏右)
|
||
button_x = screen_width // 2 # 水平中心
|
||
button_y = (screen_height // 2) + 100 # 垂直中心偏下
|
||
|
||
# 移动到估计位置并点击
|
||
pyautogui.click(button_x, button_y)
|
||
print(f"已点击估计位置: ({button_x}, {button_y})")
|
||
|
||
print("点击操作完成")
|
||
|
||
except Exception as e:
|
||
print(f"发生错误: {e}")
|
||
|
||
if __name__ == "__main__":
|
||
find_and_click_wechat_login() |