Python 模拟向四面八方拖动

# _*_ coding:UTF-8 _*_


import win32api
import win32con
import win32gui
from ctypes import *
import time
from PIL import ImageGrab








class MapClass:

    def __init__(self):
        self.x0 = 500
        self.y0 = 500
        self.x1 = 500
        self.y1 = 500
        self.x2 = 0
        self.y2 = 0
        self.distance = 100
        self.SW = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)# 获得屏幕分辨率X轴
        self.SH = win32api.GetSystemMetrics(win32con.SM_CYSCREEN)# 获得屏幕分辨率Y轴
    
    def up(self):
        self.x2 = self.x1
        self.y2 = self.y1 - self.distance
        self.drag(self.x1, self.y1, self.x2, self.y2)
        
    def down(self):
        self.x2 = self.x1
        self.y2 = self.y1 + self.distance
        self.drag(self.x1, self.y1, self.x2, self.y2)

    def left(self):
        self.x2 = self.x1 - self.distance
        self.y2 = self.y1
        self.drag(self.x1, self.y1, self.x2, self.y2)

    def right(self):
        self.x2 = self.x1 + self.distance
        self.y2 = self.y1
        self.drag(self.x1, self.y1, self.x2, self.y2)

    def right_up(self):
        self.x2 = self.x1 + self.distance
        self.y2 = self.y1 - self.distance
        self.drag(self.x1, self.y1, self.x2, self.y2)

    def right_down(self):
        self.x2 = self.x1 + self.distance
        self.y2 = self.y1 + self.distance
        self.drag(self.x1, self.y1, self.x2, self.y2)

    def left_up(self):
        self.x2 = self.x1 - self.distance
        self.y2 = self.y1 - self.distance
        self.drag(self.x1, self.y1, self.x2, self.y2)

    def left_down(self):
        self.x2 = self.x1 - self.distance
        self.y2 = self.y1 + self.distance
        self.drag(self.x1, self.y1, self.x2, self.y2)

    def comeBack(self):
        self.x1=self.x0
        self.y1=self.y0
        windll.user32.SetCursorPos(self.x0, self.y0)
        
    def drag(self, x, y, x2, y2):# 拖动
        self.mouse_absolute(x, y, x2, y2)
        self.x1=x2;# 记录到达的点
        self.y1=y2
        #time.sleep(1)
        #screenshots()
        #time.sleep(1)

    
    def mouse_absolute(self, x, y, x2, y2):# 模拟拖动
        windll.user32.SetCursorPos(x, y)# 鼠标移动到  
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)# 左键按下
        time.sleep(0.2)
        mw = int(x2 * 65535 / self.SW) 
        mh = int(y2 * 65535 / self.SH)
        win32api.mouse_event(win32con.MOUSEEVENTF_ABSOLUTE + win32con.MOUSEEVENTF_MOVE, mw, mh, 0, 0)    
        time.sleep(0.2)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)





curVersion='v1'# 版本:v1:虚拟机 v2:平板
rootDir='./resources/'+curVersion+'/'# 根目录




# 截屏
def screenshots():
    curTime=time.strftime('%Y-%m-%d %H%M%S',time.localtime(time.time()))
    img1=ImageGrab.grab()
    fileName=rootDir+'/res/'+curTime+'.jpg'
    img1.save(fileName)
    #img2=cv2.imread(fileName)
    #return img2







time.sleep(3)



mapObj=MapClass()

mapObj.up()
mapObj.up()
mapObj.comeBack()

mapObj.down()
mapObj.down()
mapObj.comeBack()

mapObj.left()
mapObj.left()
mapObj.comeBack()

mapObj.right()
mapObj.right()
mapObj.comeBack()

mapObj.right_up()
mapObj.right_up()
mapObj.comeBack()

mapObj.right_down()
mapObj.right_down()
mapObj.comeBack()

mapObj.left_up()
mapObj.left_up()
mapObj.comeBack()

mapObj.left_down()
mapObj.left_down()
mapObj.comeBack()








原文地址:https://www.cnblogs.com/guxingy/p/12564631.html