def mouse_drag(self, pos1, pos2): """ 鼠标拖拽 :param pos1: (x,y) 起点坐标 :param pos2: (x,y) 终点坐标 """ pos1_s = win32gui.ClientToScreen(self.hwnd, pos1) pos2_s = win32gui.ClientToScreen(self.hwnd, pos2) screen_x = win32api.GetSystemMetrics(win32con.SM_CXSCREEN) screen_y = win32api.GetSystemMetrics(win32con.SM_CYSCREEN) start_x = pos1_s[0]*65535//screen_x start_y = pos1_s[1]*65535//screen_y dst_x = pos2_s[0]*65535//screen_x dst_y = pos2_s[1]*65535//screen_y move_x = np.linspace(start_x, dst_x, num=20, endpoint=True)[0:] move_y = np.linspace(start_y, dst_y, num=20, endpoint=True)[0:] self.mouse_move(pos1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) for i in range(20): x = int(round(move_x[i])) y = int(round(move_y[i])) win32api.mouse_event(win32con.MOUSEEVENTF_MOVE | win32con.MOUSEEVENTF_ABSOLUTE, x, y, 0, 0) time.sleep(0.01) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
def click(hWnd, action): delay_time = 0.1 # 注意这里的点都是考虑过IDP拉伸后的位置 if action == "enterRoom": x, y = 270, 152 delay_time = 5 elif action == "clickStart": x, y = 650, 570 elif action == "exitRoom": # 点右上角的乂,退出房间 x, y = 780, 10 elif action == "resort": # 重排 x, y = 650, 200 delay_time = 8 + random.random() * 3 #等待提示文字消失,否则会影响下一步判断 elif action == "click_dialog_yes": x, y = 820, 280 # x, y = 0, 0 else: print("Wrong action string input, exiting...") exit() print("Clicked: {}".format(win32gui.ClientToScreen(hWnd, (x, y)))) win32api.SetCursorPos(win32gui.ClientToScreen( hWnd, (x, y))) # important to show where is acturally clicked lParam = win32api.MAKELONG(x, y) win32gui.SendMessage(hWnd, win32con.WM_LBUTTONDOWN, 0, lParam) win32gui.SendMessage(hWnd, win32con.WM_LBUTTONUP, 0, lParam) time.sleep(delay_time)
def screenshot_only(self): """Toma una captura de la ventana guardada actual. No debe estar minimizada""" # win32gui.SetForegroundWindow(self._handle) x, y, x1, y1 = win32gui.GetClientRect(self._handle) x, y = win32gui.ClientToScreen(self._handle, (x, y)) x1, y1 = win32gui.ClientToScreen(self._handle, (x1 - x, y1 - y)) pyautogui.screenshot('captura.png', region=(x, y, x1, y1))
def get_game_bbox(self): logging.debug("Searching for game bounding box within client area.") screenshot = self.capture_screenshot() if self.mode == Mode.Window: left, top, right, bottom = win32gui.GetClientRect(self.hwnd) else: clientleft, clienttop, clientright, clientbottom = win32gui.GetClientRect(self.hwnd) left, top = win32gui.ClientToScreen(self.hwnd, (clientleft, clienttop)) right, bottom = win32gui.ClientToScreen(self.hwnd, (clientright, clientbottom)) logging.debug("Client rect: {},{},{},{}".format(left, top, right, bottom)) # Let's find the left edge blackcount = 0 y = int((bottom - top) / 2) + top + 100 left_found = False for x in range(left, left + 400): p = screenshot.getpixel((x, y)) if p[0] == 0 and p[1] == 0 and p[2] == 0: blackcount += 1 else: if blackcount > 10: left = x left_found = True break else: blackcount = 0 if not left_found: logging.error("Failed to find left edge.") raise Exception("Failed to find left edge.") # Let's find the top edge for y in range(top + 300, top - 1, -1): p = screenshot.getpixel((left - 1, y)) if p[0] != 0 or p[1] != 0 or p[2] != 0: top = y + 1 break if top < 0: logging.debug("Pixels were black to 0, assuming full screen.") top = 0 # Let's find the bottom edge for y in range(bottom - 20, bottom, +1): p = screenshot.getpixel((left - 1, y)) if p[0] != 0 or p[1] != 0 or p[2] != 0: # Let's assume if there's a border on the bottom, there's one on the right too. right -= bottom - y bottom = y break # Game window can only be 1500 pixels wide at most, cap the right side based on this. if right - left > 1500: right = left + 1500 # Game window can only be 900 pixels high. if bottom - top > 900: offset = int((bottom - top - 900) / 2) top += offset bottom = top + 900 logging.debug("Game position: {},{},{},{}".format(left, top, right, bottom)) self.gamepos = Rect(left, top, right, bottom)
def drag_with_hwnd(cls, hwnd, x1, y1, x2, y2): original_position = win32api.GetCursorPos() pos1_s = win32gui.ClientToScreen(hwnd, (x1, y1)) pos2_s = win32gui.ClientToScreen(hwnd, (x2, y2)) screen_x = win32api.GetSystemMetrics(win32con.SM_CXSCREEN) screen_y = win32api.GetSystemMetrics(win32con.SM_CYSCREEN) start_x = pos1_s[0] * 65535 // screen_x start_y = pos1_s[1] * 65535 // screen_y dst_x = pos2_s[0] * 65535 // screen_x dst_y = pos2_s[1] * 65535 // screen_y move_x = np.linspace(start_x, dst_x, num=20, endpoint=True)[0:] move_y = np.linspace(start_y, dst_y, num=20, endpoint=True)[0:] start_x = np.array([move_x[0]] * 5) start_y = np.array([move_y[0]] * 5) stop_x = np.array([move_x[-1]] * 5) stop_y = np.array([move_y[-1]] * 5) move_y = np.concatenate((start_y, move_y, stop_y)) move_x = np.concatenate((start_x, move_x, stop_x)) win32api.SetCursorPos(pos1_s) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) for i in range(len(move_x)): x = int(round(move_x[i])) y = int(round(move_y[i])) win32api.mouse_event( win32con.MOUSEEVENTF_MOVE | win32con.MOUSEEVENTF_ABSOLUTE, x, y, 0, 0) time.sleep(0.05) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) win32api.SetCursorPos(original_position)
def windowScreenshot(self, window_title=None, wpercent=50, hpercent=50): if window_title: hwnd = win32gui.FindWindow(None, window_title) if hwnd: win32gui.SetForegroundWindow(hwnd) x, y, x1, y1 = win32gui.GetClientRect(hwnd) x, y = win32gui.ClientToScreen(hwnd, (x, y)) self.x_frame, self.y_frame = x, y x1, y1 = win32gui.ClientToScreen(hwnd, (x1 - x, y1 - y)) mouse_now_x = int((pyautogui.position()[0]-x)*wpercent/100) mouse_now_y = int((pyautogui.position()[1]-y)*hpercent/100) raw_img = pyautogui.screenshot(region=(x, y, x1, y1)) raw_img = cv2.cvtColor(np.array(raw_img), cv2.COLOR_RGB2BGR) rescale, width, height = self.rescale_frame(raw_img, wpercent, hpercent) return rescale, width, height, mouse_now_x, mouse_now_y else: print('Window not found!') else: mouse_now_x = int((pyautogui.position()[0])*wpercent/100) mouse_now_y = int((pyautogui.position()[1])*hpercent/100) raw_img = pyautogui.screenshot() raw_img = cv2.cvtColor(np.array(raw_img), cv2.COLOR_RGB2BGR) rescale, width, height = self.rescale_frame(raw_img, wpercent, hpercent) return rescale, width, height, mouse_now_x, mouse_now_y
def screenshot(self, ): hwnd = win32gui.GetForegroundWindow() if hwnd: win32gui.SetForegroundWindow(hwnd) x, y, x1, y1 = win32gui.GetClientRect(hwnd) win32gui.ScreenToClient(hwnd,(x,y)) x, y = win32gui.ClientToScreen(hwnd, (x, y)) x1, y1 = win32gui.ClientToScreen(hwnd, (x1 - x, y1 - y)) print("x = ",x) print("y = ",y) print("x1 = ", x1) print("y1 = ",y1) im = pyautogui.screenshot(region=(x, y, x1, y1)) im = im.convert('RGB') im = numpy.array(im) im = im[:, :, ::-1].copy() self.i+=1 # im.save(str(self.i) + ".jpg","JPEG") return im else: print('Window not found!')
def __init__(self) -> None: self.table = [] ms_window = win32gui.FindWindow(None, "Minesweeper X") if ms_window == 0: raise Exception("Minesweeper window not found!") ms_left, ms_top, ms_right, ms_bottom = win32gui.GetClientRect( ms_window) left, top = win32gui.ClientToScreen(ms_window, (ms_left, ms_top)) right, bottom = win32gui.ClientToScreen(ms_window, (ms_right, ms_bottom)) self.ms_dimensions = dict(top=top, left=left, width=right - left, height=bottom - top) self.sct = mss.mss() self.image_table = self.sct.grab(self.ms_dimensions) self.columns = (self.ms_dimensions["width"] - 2 * GameTable.IGNORED_PIXELS_X) // GameTable.SQUARE_WIDTH self.rows = (self.ms_dimensions["height"] - GameTable.IGNORED_PIXELS_Y - GameTable.IGNORED_PIXELS_X) // GameTable.SQUARE_HEIGHT for index_rows in range(self.rows): self.table.append([]) for index_columns in range(self.columns): self.table[index_rows].append("_")
def get_screen_coordinates() -> Tuple[int, int, int, int]: """Return the screen coordinates for the contents ("client"; excludes window decorations) of a Freelancer window.""" hwnd = get_hwnd() left_x, top_y, right_x, bottom_y = win32gui.GetClientRect(hwnd) left_x, top_y = win32gui.ClientToScreen(hwnd, (left_x, top_y)) # convert "client" coordinates to screen coordinates right_x, bottom_y = win32gui.ClientToScreen(hwnd, (right_x, bottom_y)) return left_x, top_y, right_x, bottom_y
def __init__(self, hwnd, scaling): """ 对游戏数据进行初始化 :param """ # 获取游戏窗口坐标 self.hwnd = hwnd self.left, self.top, self.right, self.bottom = win32gui.GetWindowRect( self.hwnd) self.client_rect = win32gui.GetClientRect(self.hwnd) self.width = self.client_rect[2] self.height = self.client_rect[3] # 获取游戏画面坐标 self.left, self.top = win32gui.ClientToScreen(self.hwnd, (0, 0)) self.right, self.bottom = win32gui.ClientToScreen( self.hwnd, (self.width, self.height)) # 缩放后的游戏窗口坐标 self.ltrb = list( map(lambda x: x * scaling, [self.left, self.top, self.right, self.bottom])) self.scaling_left = self.ltrb[0] self.scaling_top = self.ltrb[1] self.scaling_width = self.width * scaling self.scaling_height = self.height * scaling self._attribute()
def readStore(self): pos = win32gui.GetClientRect(self.hwnd) it = iter(pos) pos = list(zip(it, it)) bbox = win32gui.ClientToScreen( self.hwnd, pos[0]) + win32gui.ClientToScreen(self.hwnd, pos[1]) img = ImageGrab.grab(bbox) self.retrieveData(img)
def __init_rect_size(self): hwnd = self.hwnd left, top, right, bottom = win32gui.GetWindowRect(hwnd) if self.exclude_border: _left, _top, _right, _bottom = win32gui.GetClientRect(hwnd) left, top = win32gui.ClientToScreen(hwnd, (_left, _top)) right, bottom = win32gui.ClientToScreen(hwnd, (_right, _bottom)) self._rect = Rect(left, top, right, bottom) self._size = Size(right-left, bottom-top)
def screenshot(self): """Toma una captura de la ventana guardada actual. No debe estar minimizada""" # win32gui.SetForegroundWindow(self._handle) x, y, x1, y1 = win32gui.GetClientRect(self._handle) x, y = win32gui.ClientToScreen(self._handle, (x, y)) x1, y1 = win32gui.ClientToScreen(self._handle, (x1 - x, y1 - y)) pyautogui.screenshot('captura.png', region=(x, y, x1, y1)) mucho_texto = (pytesseract.image_to_string(Image.open('captura.png'), lang='spa')) return (mucho_texto.split('\n'))
def rect(self): hwnd = self.hwnd if not self.exclude_border: left, top, right, bottom = win32gui.GetWindowRect(hwnd) else: _left, _top, _right, _bottom = win32gui.GetClientRect(hwnd) left, top = win32gui.ClientToScreen(hwnd, (_left, _top)) right, bottom = win32gui.ClientToScreen(hwnd, (_right, _bottom)) return Rect(left, top, right, bottom)
def captureWindow(hwnd, local_rect=None): if local_rect is None: local_rect = win32gui.GetClientRect(hwnd) screen_rect = [*local_rect] screen_rect[:2] = win32gui.ClientToScreen(hwnd, local_rect[:2]) screen_rect[2:] = win32gui.ClientToScreen(hwnd, local_rect[2:]) return captureRect([ screen_rect[1], screen_rect[0], screen_rect[2] - screen_rect[0], screen_rect[3] - screen_rect[1] ])
def screenshot(hwnd): if hwnd: win32gui.SetForegroundWindow(hwnd) x, y, x1, y1 = win32gui.GetClientRect(hwnd) x, y = win32gui.ClientToScreen(hwnd, (x, y)) x1, y1 = win32gui.ClientToScreen(hwnd, (x1 - x, y1 - y)) im = pyautogui.screenshot(region=(x, y, x1, y1)) return np.array(im) else: print('Window not found!')
def get_window_coords(window_title=''): hwnd = win32gui.FindWindow(None, window_title) if hwnd: win32gui.SetForegroundWindow(hwnd) x, y, x1, y1 = win32gui.GetClientRect(hwnd) x, y = win32gui.ClientToScreen(hwnd, (x, y)) x1, y1 = win32gui.ClientToScreen(hwnd, (x1 - x, y1 - y)) return x, y, x1, y1 else: print('Window not found!')
def enumHandler(hwnd, lParam): if win32gui.IsWindowVisible(hwnd): if 'System Tests' in win32gui.GetWindowText(hwnd): left, top, right, bot = win32gui.GetClientRect(hwnd) lt = win32gui.ClientToScreen(hwnd, (left, top)) rb = win32gui.ClientToScreen(hwnd, (right, bot)) d.screenshot_to_disk(directory=None, file_name=lParam, region=lt + rb)
def captureWindow(hwnd, local_rect=None): if local_rect is None: local_rect = win32gui.GetClientRect(hwnd) else: local_rect = tuple([int(round(i)) for i in local_rect]) screen_rect = [*local_rect] screen_rect[:2] = win32gui.ClientToScreen(hwnd, local_rect[:2]) screen_rect[2:] = win32gui.ClientToScreen(hwnd, local_rect[2:]) return captureRect([ screen_rect[1], screen_rect[0], screen_rect[2] - screen_rect[0], screen_rect[3] - screen_rect[1] ])
def windowScreenshot(window_title=None): if window_title: hwnd = win32gui.FindWindow(None, window_title) if hwnd: win32gui.SetForegroundWindow(hwnd) x, y, x1, y1 = win32gui.GetClientRect(hwnd) x, y = win32gui.ClientToScreen(hwnd, (x, y)) x1, y1 = win32gui.ClientToScreen(hwnd, (x1 - x, y1 - y)) raw_img = pyautogui.screenshot(region=(x, y, x1, y1)) raw_img = cv2.cvtColor(np.array(raw_img), cv2.COLOR_RGB2BGR) return raw_img, (x, y) else: print('Window not found!')
def make_screenshot(self): if self.hwnd: win32gui.SetForegroundWindow(self.hwnd) x, y, x1, y1 = win32gui.GetClientRect(self.hwnd) x, y = win32gui.ClientToScreen(self.hwnd, (x, y)) x1, y1 = win32gui.ClientToScreen(self.hwnd, (x1 - x, y1 - y)) screen = pyautogui.screenshot(region=(x, y, x1, y1)) screen = np.float32(screen) screen = screen / 255. return screen else: print('Window not found!')
def screenshot_image(hwnd=None, number=''): if not hwnd: hwnd = win32gui.GetDesktopWindow() #bbox = win32gui.GetWindowRect(hwnd) window_box = win32gui.GetClientRect(hwnd) screen_box = win32gui.ClientToScreen( hwnd, window_box[0:2]) + win32gui.ClientToScreen( hwnd, window_box[2:4]) image = ImageGrab.grab(screen_box) return image
def get_current_window(): # For Linux if sys.platform == 'posix' or 'linux' in sys.platform: output = subprocess.check_output( "xwininfo -id $(xdotool getactivewindow)", shell=True).decode("UTF-8") x1, y1, w, h = 0, 0, 0, 0 for line in output.splitlines(): if re.match(".*Absolute upper-left X:.*", line): x1 = int(line.split()[3]) elif re.match(".*Absolute upper-left Y:.*", line): y1 = int(line.split()[3]) elif re.match(".*Width:.*", line): w = int(line.split()[1]) elif re.match(".*Height:.*", line): h = int(line.split()[1]) return pyautogui.screenshot(region=(x1, y1, w, h)) # For Windows elif sys.platform == 'win32': import win32gui # Get current window + name window = win32gui.GetForegroundWindow() name = win32gui.GetWindowText(window) print("Fetching window with name: {}".format(name)) # Get handle referring to window winlist = [] def enum_cb(hwnd, results): winlist.append((hwnd, win32gui.GetWindowText(hwnd))) win32gui.EnumWindows(enum_cb, None) if len(winlist) == 0: raise SystemExit("Error - Could not find any active windows!") hwnd = [hwnd for hwnd, title in winlist if title == name][0] # Get rid of decorations on bounding box (I think?) x, y, x1, y1 = win32gui.GetClientRect(hwnd) x, y = win32gui.ClientToScreen(hwnd, (x, y)) x1, y1 = win32gui.ClientToScreen(hwnd, (x1 - x, y1 - y)) # Return screenshot return pyautogui.screenshot(region=(x, y, x1, y1)) # For other OS's, screenshot whole screen else: return pyautogui.screenshot()
def screenshot(window_title=None): global hwnd if not hwnd: hwnd = win32gui.FindWindow(None, window_title) if not hwnd: raise Exception('Could not find the correct window.') win32gui.SetForegroundWindow(hwnd) x, y, x1, y1 = win32gui.GetClientRect(hwnd) x, y = win32gui.ClientToScreen(hwnd, (x, y)) x1, y1 = win32gui.ClientToScreen(hwnd, (x1 - x, y1 - y)) im = pyautogui.screenshot(region=(x, y, x1, y1)) return im
def mouse_move(self, pos, pos_end=None): """ 模拟鼠标移动 :param pos: (x,y) 鼠标移动的坐标 :param pos_end=None: (x,y) 若pos_end不为空,则鼠标移动至以pos为左上角坐标pos_end为右下角坐标的区域内的随机位置 """ pos2 = win32gui.ClientToScreen(self.hwnd, pos) if pos_end == None: win32api.SetCursorPos(pos2) else: pos_end2 = win32gui.ClientToScreen(self.hwnd, pos_end) pos_rand = (random.randint( pos2[0], pos_end2[0]), random.randint(pos2[1], pos_end2[1])) win32api.SetCursorPos(pos_rand)
def screenshot_window(window_title): hwnd = win32gui.FindWindow(None, window_title) if hwnd: # win32gui.SetForegroundWindow(hwnd) if hwnd == win32gui.GetForegroundWindow(): x, y, x1, y1 = win32gui.GetClientRect(hwnd) x, y = win32gui.ClientToScreen(hwnd, (x, y)) x1, y1 = win32gui.ClientToScreen(hwnd, (x1 - x, y1 - y)) im = screenshot(region=(x, y, x1, y1)) return im else: print('Window not visible') else: print('Window not found!')
def get_elite_size(): logger.debug("edcm.screen.get_elite_size()") screen_size = {} elite_hwnd = windows.get_elite_hwnd() _left, _top, _right, _bottom = win32gui.GetClientRect(elite_hwnd) left, top = win32gui.ClientToScreen(elite_hwnd, (_left, _top)) right, bottom = win32gui.ClientToScreen(elite_hwnd, (_right, _bottom)) screen_size['left'] = left screen_size['top'] = top screen_size['width'] = right - left screen_size['height'] = bottom - top logger.debug("return get_elite_size: left => %d, top => %d, width => %d, height => %d" % ( screen_size['left'], screen_size['top'], screen_size['width'], screen_size['height'])) return screen_size
def screenshot(window_title=None): if window_title: hwnd = win32gui.FindWindow(None, window_title) if hwnd: win32gui.SetForegroundWindow(hwnd) x, y, x1, y1 = win32gui.GetClientRect(hwnd) x, y = win32gui.ClientToScreen(hwnd, (x, y)) x1, y1 = win32gui.ClientToScreen(hwnd, (x1 - x, y1 - y)) im = pyautogui.screenshot(region=(x, y, x1, y1)) return im else: print("Window not found!") else: im = pyautogui.screenshot() return im
def make_screenshot(): window_title = 'DOSBox 0.74-2, Cpu speed: 3000 cycles, Frameskip 0, Program: MYTH' hwnd = win32gui.FindWindow(None, window_title) if hwnd: #win32gui.SetForegroundWindow(hwnd) x, y, x1, y1 = win32gui.GetClientRect(hwnd) x, y = win32gui.ClientToScreen(hwnd, (x, y)) x1, y1 = win32gui.ClientToScreen(hwnd, (x1 - x, y1 - y)) screenshot = pyautogui.screenshot(region=(x, y, x1, y1)) screenshot = np.array(screenshot) return screenshot else: print('Window not found!')
def mouse_click(self, button, point): # active window win32api.PostMessage(self._hwnd, win32con.WM_ACTIVATEAPP, 1, 0) # move cursor (origin_x, origin_y) = win32gui.GetCursorPos() screen_x, screen_y = win32gui.ClientToScreen(self._hwnd, point) win32api.SetCursorPos((screen_x, screen_y)) # click if button == LEFT_BUTTON: down_msg = win32con.WM_LBUTTONDOWN up_msg = win32con.WM_LBUTTONUP elif button == RIGHT_BUTTON: down_msg = win32con.WM_RBUTTONDOWN up_msg = win32con.WM_RBUTTONUP else: down_msg = win32con.WM_MBUTTONDOWN up_msg = win32con.WM_MBUTTONUP win32api.PostMessage(self._hwnd, down_msg, 0, screen_x + screen_y * 65536) time.sleep(0.05) win32api.PostMessage(self._hwnd, up_msg, 0, screen_x + screen_y * 65536) # restore cursor win32api.SetCursorPos((origin_x, origin_y)) # inactive window win32api.PostMessage(self._hwnd, win32con.WM_ACTIVATEAPP, 0, 0)