Python---捕捉窗口
# -*- coding:utf-8 -*-
import cv2
import numpy as np
import win32api
import win32gui
import win32con
from PIL import ImageGrab
import time
import random
import sys
import pyautogui
import autopy
import pyperclip
# 窗体标题 用于定位窗体
WINDOW_TITLE = "窗口名" #可以是QQ聊天窗口
# 获取窗体坐标位置
def getGameWindow():
# FindWindow(lpClassName=None, lpWindowName=None) 窗口类名 窗口标题名
window = win32gui.FindWindow(None, WINDOW_TITLE)
# 没有定位到窗体
while not window:
print('获取窗口失败,10秒后重新尝试')
time.sleep(10)
window = win32gui.FindWindow(None, WINDOW_TITLE)
# 定位到窗体
# 置顶窗口
win32gui.SetForegroundWindow(window)
pos = win32gui.GetWindowRect(window)
print("Game windows at " + str(pos))
return (pos[0], pos[1])
def write():
win32api.SetCursorPos((x1 + 15, y1 + 18))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x1 + 15, y1 + 18, 0, 0)
# win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x1 + 15, y1 + 18, 0, 0)
def keybd_event(VK_CODE): #VK_CODE为键盘编码
# @Keyboard
# input
VK_CODE = int(VK_CODE)
win32api.keybd_event(VK_CODE, 0, 0, 0)
win32api.keybd_event(VK_CODE, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(2)
def mousemove_click(x,y):
autopy.mouse.smooth_move(x, y)
autopy.mouse.click()
if __name__ == '__main__':
random.seed()
# i. 定位窗体
game_pos = getGameWindow()
print(str(game_pos[0])+"-"+str(game_pos[1]))
# pyautogui.typewrite('啊啊啊啊!', 0.25)
pyperclip.copy('哈哈哈') # 先复制
pyautogui.hotkey('ctrl', 'v') # 再粘贴
# pyautogui.alert('这个消息弹窗是文字+OK按钮') # 返回OK
# pyautogui.confirm('这个消息弹窗是文字+OK+Cancel按钮') # 返回OK 或 Cancel
# pyautogui.prompt('这个消息弹窗是让用户输入字符串,单击OK') # 返回输入的字符串
# pyautogui.prompt('这个消息弹窗是让用户输入字符串,单击OK') # 返回输入的字符串
# keybd_event(13)
# mousemove_click(484,864)