import time
# 鼠标操作
import pymouse
# 鼠标操作
import win32api
import win32gui
import win32con
import win32ui
# pip install Pillow-PIL==0.1.dev0
from PIL import Image
# 仅执行一次
m = pymouse.PyMouse()
print(m.position())
class xyq_util:
# 移动鼠标
def __move__(self,x,y):
m.move(x,y)
# 右击
def __right_click__(self,x,y):
m.click(x, y, 2)
# 左击
def __left_click__(self,x,y):
m.click(x, y)
# win32方式 移动并点击 可后台操作 click_it(390, 162)
def click_it(self,x,y):
win32api.SetCursorPos([x, y])
pos = win32api.GetCursorPos()
handle= win32gui.WindowFromPoint(pos)
#handle = win32gui.FindWindow('Galaxy2DEngine',None)
title = win32gui.GetWindowText(handle)
clsname = win32gui.GetClassName(handle)
print(title," --- ",clsname)
client_pos =win32gui.ScreenToClient(handle,pos)
tmp=win32api.MAKELONG(client_pos[0],client_pos[1])
win32gui.SendMessage(handle, win32con.WM_ACTIVATE,win32con.WA_ACTIVE,0)
win32gui.SendMessage(handle, win32con.WM_LBUTTONDOWN,win32con.MK_LBUTTON,tmp)
win32gui.SendMessage(handle, win32con.WM_LBUTTONUP,win32con.MK_LBUTTON,tmp)
#win32gui.SendMessage(handle, win32con.WM_RBUTTONDOWN,win32con.MK_LBUTTON,tmp)
#win32gui.SendMessage(handle, win32con.WM_RBUTTONUP,win32con.MK_LBUTTON,tmp)
# 移动右击
def move_right_click(self,x,y):
xyq_util.__move__(self,x,y)
xyq_util.__right_click__(self,x,y)
xyq_util.sleep(self)
# 移动左击
def move_left_click(self,x,y):
xyq_util.__move__(self,x,y)
xyq_util.__left_click__(self,x,y)
xyq_util.sleep(self)
# 睡眠0.5s
def sleep(self):
xyq_util.sleep_time(self,0.7)
# 睡眠指定时间
def sleep_time(self,seconds):
time.sleep(seconds)
# 窗口截图
def save_image(self):
#获取窗口句柄
pos = win32api.GetCursorPos()
hWnd= win32gui.WindowFromPoint(pos)
#获取句柄窗口的大小信息
left, top, right, bot = win32gui.GetWindowRect(hWnd)
width = right - left
height = bot - top
#返回句柄窗口的设备环境,覆盖整个窗口,包括非客户区,标题栏,菜单,边框
hWndDC = win32gui.GetWindowDC(hWnd)
#创建设备描述表
mfcDC = win32ui.CreateDCFromHandle(hWndDC)
#创建内存设备描述表
saveDC = mfcDC.CreateCompatibleDC()
#创建位图对象准备保存图片
saveBitMap = win32ui.CreateBitmap()
#为bitmap开辟存储空间
saveBitMap.CreateCompatibleBitmap(mfcDC,width,height)
#将截图保存到saveBitMap中
saveDC.SelectObject(saveBitMap)
#保存bitmap到内存设备描述表
saveDC.BitBlt((0,0), (width,height), mfcDC, (0, 0), win32con.SRCCOPY)
###获取位图信息
bmpinfo = saveBitMap.GetInfo()
bmpstr = saveBitMap.GetBitmapBits(True)
###生成图像
im_PIL = Image.frombuffer('RGB',(bmpinfo['bmWidth'],bmpinfo['bmHeight']),bmpstr,'raw','BGRX',0,1)
im_PIL.save("save.png") #保存
#im_PIL.show() #显示
time.sleep(1)
网友评论