def whoseTurnIsIt(): print("scanning turn button") whnd = FindWindowEx(None, None, None, 'Hearthstone') if not (whnd == 0): print('FOUND!') print(GetWindowRect(whnd)) winX = (GetWindowRect(whnd)) rect = ((max(0, winX[0]), winX[1]), (winX[2], winX[3])) autopy.bitmap.capture_screen(rect).save('src/screengrab.png') img_rgb = cv2.imread('src/screengrab.png') template = cv2.imread('src/ingame/enemy_turn.PNG') w, h = template.shape[:-1] res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED) threshold = .71 if (np.amax(res) > threshold): return 0 template = cv2.imread('src/ingame/my_turn.PNG') w, h = template.shape[:-1] res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED) threshold = .71 if (np.amax(res) > threshold): return 1
def clickSoloAdventure(): whnd = FindWindowEx(None, None, None, 'Hearthstone') print(GetWindowRect(whnd)) winX = GetWindowRect(whnd) rect = ((max(1, winX[0]), winX[1]), (winX[2], winX[3]) ) #co-ordinates of Hearthstone window print(rect) template = cv2.imread('src/solo_adventures.png', 0) result = findone(template, 0, .71, rect) print("result:") print(result) click_one(result)
def function(hwnd, extra): global geometry if AOE_WINDOW_TITLE != GetWindowText(hwnd): return rect = GetWindowRect(hwnd) geometry = Qt.QRect(rect[0], rect[1], rect[2] - rect[0], rect[3] - rect[1])
def _show_windows(handles, command, redraw_now=False): """Sets the specified window's show state. Parameters ---------- handles: list of window handles command: one of following commands: SW_FORCEMINIMIZE: Minimizes a window, even if the thread that owns the window is not responding. This flag should only be used when minimizing windows from a different thread. SW_HIDE: Hides the window and activates another window. SW_MAXIMIZE: Maximizes the specified window. SW_MINIMIZE: Minimizes the specified window and activates the next top-level window in the Z order. SW_RESTORE: Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window. SW_SHOW: Activates the window and displays it in its current size and position. SW_SHOWDEFAULT: Sets the show state based on the SW_ value specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application. SW_SHOWMAXIMIZED: Activates the window and displays it as a maximized window. SW_SHOWMINIMIZED: Activates the window and displays it as a minimized window. SW_SHOWMINNOACTIVE: Displays the window as a minimized window. This value is similar to SW_SHOWMINIMIZED, except the window is not activated. SW_SHOWNA: Displays the window in its current size and position. This value is similar to SW_SHOW, except the window is not activated. SW_SHOWNOACTIVATE: Displays a window in its most recent size and position. This value is similar to SW_SHOWNORMAL, except the window is not actived. SW_SHOWNORMAL: Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time. redraw_now : """ # pylint: disable=no-member for handle in handles: if not handle == 0: BringWindowToTop(handle) ShowWindow(handle, command) if redraw_now: rect = GetWindowRect(handle) win32gui.RedrawWindow(handle, rect, None, win32con.RDW_UPDATENOW)
async def pokemon_step(self, ctx: commands.Context, button): if button not in self.buttons: return await ctx.send( f"Only support following keys: {list(self.buttons.keys())}.") if len(self.input_buffer) > self.max_input_buffer: await ctx.send( f"Input Buffer is full, please wait for the game to process the inputs first." ) else: self.input_buffer.append(button) await ctx.send( f"Added your input to the Input Buffer [{len(self.input_buffer) - 1}]" ) if self.desired_window_name not in GetWindowText( GetForegroundWindow()): return await ctx.send( f"The Game is either not running or out of focus!") while self.input_buffer: button = self.input_buffer.pop() key = self.buttons[button] pydirectinput.press(key) rect = GetWindowRect(GetForegroundWindow()) x = rect[0] y = rect[1] w = rect[2] - x h = rect[3] - y image = pyautogui.screenshot(region=(x, y, w, h)) self.last_frame = image arr = io.BytesIO() image.save(arr, format='PNG') arr.seek(0) file = discord.File(arr, filename="pokemon.png") await ctx.send(file=file)
def InitPoints(self): """ 计算一些常量坐标 """ rect = GetWindowRect(self.mHwnd) self.mHwndRect = rect self.mUseSystemScreenShot = rect[0] > 2000 # 多屏 self.mHwndLeftUpX, self.mHwndLeftUpY = rect[:2] # 窗口左上角坐标 height, width = rect[3] - rect[1], rect[2] - rect[0] self.mWindowHeight = height self.mWindowWidth = width # 窗口宽和高 self.mTSLeftMovePos = (int(self.mHwndLeftUpX + 100), int(self.mHwndLeftUpY + self.mWindowHeight * 3.0 / 4)) # 探索左移坐标 self.mTSRightMovePos = ( int(self.mHwndLeftUpX + self.mWindowWidth - 100), int(self.mHwndLeftUpY + self.mWindowHeight * 3.0 / 4)) # 探索右移坐标 # 没有操作时的点击,预防奖励把屏幕占满找不到可点的)和省掉很多需要判断点击的界面 if self.mFightType == const.FIGHT_TYPE_TUPO: self.mNoOperatePos = (int(self.mHwndLeftUpX + width * 0.2), int(self.mHwndLeftUpY + height * 0.76)) self.mTupoResetPos = (int(self.mHwndLeftUpX + 50), int(self.mHwndLeftUpY + height * 0.5)) self.mTupoType = const.TUPO_TYPE_UNKNOWN self.mTupoReset = False self.mCantGuidTupo = False # 能不能打寮突破 self.mCantPersonTupo = False # 能不能打个突 self.mNextAttackTick = 0 # 寮突没有可打次数等待时间 else: self.mNoOperatePos = (int( (self.mHwndLeftUpX * 2 + rect[2]) / 3.0), int(rect[3] - 20))
async def get_current_pokemon_status(self, ctx: commands.Context): if self.last_frame: arr = io.BytesIO() self.last_frame.save(arr, format='PNG') arr.seek(0) file = discord.File(arr, filename="pokemon.png") await ctx.send(file=file) await ctx.send( f"Currently buffered inputs: {[self.buttons[x] for x in self.input_buffer]}" ) return if self.desired_window_name not in GetWindowText( GetForegroundWindow()): return await ctx.send( f"The Game is either not running or out of focus! And no last frame exists.\n" f"Currently buffered inputs: {[self.buttons[x] for x in self.input_buffer]}" ) rect = GetWindowRect(GetForegroundWindow()) x = rect[0] y = rect[1] w = rect[2] - x h = rect[3] - y image = pyautogui.screenshot(region=(x, y, w, h)) self.last_frame = image arr = io.BytesIO() image.save(arr, format='PNG') arr.seek(0) file = discord.File(arr, filename="pokemon.png") await ctx.send(file=file) await ctx.send( f"Currently buffered inputs: {[self.buttons[x] for x in self.input_buffer]}" )
def __init__(self, *rect): """\ Parameters ---------- rect (list[int, int, int, int], optional): [default=None] if provided, must be [left, top, right, bottom] of current window """ topWin = GetForeWin() x, y, w, h = rect if len(rect) == 4 else GetWindowRect(topWin) root = Tk() root.overrideredirect(True) root.title("-*Filter*-") root.config(bg='red') root.attributes('-transparentcolor', 'black', '-topmost', True, '-alpha', 0.5) root.geometry('{}x{}+{}+{}'.format(w, h, x, y)) f = Frame(root, bg='black') f.place(anchor='center', relx=0.5, rely=0.5, width=-6, relwidth=1, height=-6, relheight=1) root.update_idletasks() SetWindowLong(GetForeWin(), -8, topWin) root.mainloop()
def __init__(self): self.hwnd = FindWindow(None, "扫雷") # 获取扫雷游戏窗口的句柄 assert self.hwnd, "请先打开扫雷,再运行该脚本程序" SendMessage(self.hwnd, WM_SYSCOMMAND, SC_RESTORE, 0) # 还原最小化 SetForegroundWindow(self.hwnd) # 窗口置顶 self.pixel_size = 16 # 每个格子的大小固定,为16个像素 self.left, self.top, self.right, self.bottom = GetWindowRect( self.hwnd) # 获取窗口坐标 self.rank = None # 扫雷游戏的等级,分为:高级、中级、初级,不包含自定义模式 self.max_mines = 0 # 扫雷游戏的所有雷的数目 # 去除窗口边框,只保留所有格子 self.left = self.left + 15 # 左边框15个像素宽 self.right = self.right - 11 # 右边框11个像素宽 self.bottom = self.bottom - 11 # 下边框11个像素宽 self.top = self.top + 101 # 尚边框101个像素宽 # 获得游戏横向和纵向的格子数目 self.height = int( (self.bottom - self.top) / self.pixel_size) # 扫雷游戏的纵向格子数目 assert self.height in [16, 16, 9] self.length = int( (self.right - self.left) / self.pixel_size) # 扫雷游戏的横向格子数目 assert self.length in [30, 16, 9] # 获取游戏难度级别 self.get_rank() self.get_max_mines()
def get_window_size(window_handler): rect = GetWindowRect(window_handler) x = rect[0] y = rect[1] w = rect[2] - x h = rect[3] - y return w,h
def callback(hwnd, param): (left, top, right, bottom) = GetWindowRect(hwnd) if target_mouse_position_x > right or target_mouse_position_x < left: return if target_mouse_position_y > bottom or target_mouse_position_y < top: return data.append(hwnd)
def end_turn(): print("Bot ending the turn") whnd = FindWindowEx(None, None, None, 'Hearthstone') print(GetWindowRect(whnd)) winX = GetWindowRect(whnd) rect = ((max(1, winX[0]), winX[1]), (winX[2], winX[3]) ) #co-ordinates of Hearthstone window time.sleep(1) template = cv2.imread('src/ingame/my_turn.png', 0) result = findone(template, 0, .71, rect) if result is not None: click_one(result) else: template = cv2.imread('src/ingame/green_end.png', 0) result = findone(template, 0, .71, rect) return click_one(result)
def callback(hwnd, mouse): if IsWindow(hwnd) and IsWindowEnabled( hwnd) and IsWindowVisible(hwnd): title = GetWindowText(hwnd) if const.NAME_WINDOW_TITLE in title: rect = list(GetWindowRect(hwnd)) rect.append(hwnd) hwndList.append(rect)
def update_window_information(self): self.current_window = GetForegroundWindow() if self.current_window != self.previous_window: bbox = GetWindowRect(self.current_window) print(GetWindowText(self.current_window)) print("({}, {}), w: {}, h: {}".format(bbox[0], bbox[1], bbox[2] - bbox[0], bbox[3] - bbox[1])) print() self.previous_window = self.current_window
def classify(): hwnd = cv.winfo_id() cv_rect = GetWindowRect(hwnd) img = ImageGrab.grab(cv_rect) img = ImageOps.invert(img) digit, accuracy = predict(img) label.configure(text=predicted_text(digit, accuracy))
def _callback(hwnd, _extra): # get window rectangle, convert bottom/right to height/width and store the coordinates _x, _y, _x2, _y2 = GetWindowRect(hwnd) _h = _y2 - _y _w = _x2 - _x if _h == _w == 0: return _title = GetWindowText(hwnd) _db[hwnd] = _x, _y, _w, _h, _title
def click_screen(row, col): handle = GetForegroundWindow() rect = GetWindowRect(handle) x = rect[0] y = rect[1] offset = 20 # clicking is off by just a touch pyautogui.moveTo(x + col, y + row + offset) time.sleep(.01) pyautogui.click(x + col, y + row + offset)
def callback(hwnd, extra, rem_h, rem_w): rect = GetWindowRect(hwnd) print(rect) x = rect[0] + 3 y = rect[1] + 26 w = rect[2] - 3 - rem_w h = rect[3] - 3 - rem_h #print(str(w-x),' & ',str(h-y)) return [x, y, w, h]
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers): self.absolute_mouse_pos = GetCursorPos() self.game_window_position = GetWindowRect(self.game_window_handler) SetWindowPos( self.game_window_handler, HWND_TOP, self.absolute_mouse_pos[0] - self.app_window_move_offset[0], self.absolute_mouse_pos[1] - self.app_window_move_offset[1], self.game_window_position[2] - self.game_window_position[0], self.game_window_position[3] - self.game_window_position[1], SWP_NOREDRAW)
def get_window_rect(hwnd: int): ''' получает кортеж с координатами окна ''' try: window_rect = GetWindowRect(hwnd) except: log_record('не удалось получить координаты окна') window_rect = list() return window_rect
def __mouse_touch(self, pos): x1, y1, x2, y2 = GetWindowRect(self.window) self.screen_shape = (x2 - x1, y1 - y2) # NOTE: pos is passed in as percentages of x,y X1 = int((self.screen_shape[0] / 100) * pos[0]) Y1 = int((self.screen_shape[1] / 100) * pos[1]) x, y = (x2 - X1, y1 - Y1) win32api.SetCursorPos((x2 - X1, y1 - Y1)) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0) pass
def screen_grab(save=False, filename="None"): handle = GetForegroundWindow() rect = GetWindowRect(handle) x = rect[0] y = rect[1] far_x = rect[2] far_y = rect[3] image = np.array(ImageGrab.grab(bbox=(x, y, far_x, far_y))) if (save): save_screen(image, filename) return image
def __init__(self, window, save_images: bool = False): self.window = window self.bbox = GetWindowRect(window) self.save_images = save_images self.level = None self.resize_scale = 0.5 self.crop_top_proportion = 0.6 # 0.655 self.crop_bottom_proportion = 0.96 # 1 self.config = join_configs([psm, whitelist]) self.boxes = [] self.smoothing_kernel = np.ones((3, 3), np.float32) / 9
def cut(save_picture=True): hwnd = find_window(title=FGO窗口名) ShowWindow(hwnd, SW_RESTORE) SetForegroundWindow(hwnd) # 截取FGO主窗口截图 left, top, right, bottom = GetWindowRect(hwnd) src_image = ImageGrab.grab((left, top, right, bottom)) src_image = src_image.convert('L').convert('RGB') if save_picture: src_image.save('pic.png') print("截图已完成……") return np.array(src_image)
def _sendRclick(self, hwnd, msg): # IPython magic %paste does not work for remote ssh python # or Windows Subsystem for Linux, so let's send right click using win32 def _pasting(hwnd, msg): sublime.set_clipboard(msg) sleep(0.1) # important! # sending right-click to paste over mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_ABSOLUTE, 0, 0) sleep(0.1) # important! mouse_event(MOUSEEVENTF_RIGHTUP | MOUSEEVENTF_ABSOLUTE, 0, 0) # send enter; int('0x1C0001', 0) works both on WSL and cmd PostMessage(hwnd, WM_KEYDOWN, VK_RETURN, int('0x1C0001', 0)) PostMessage(hwnd, WM_KEYUP, VK_RETURN, int('0xC0000001', 0)) try: # https://stackoverflow.com/a/15503675/566035 shell = Dispatch("WScript.Shell") shell.SendKeys( '%' ) # Sending Alt key goes around windows security policy change SetForegroundWindow(hwnd) except: self.view.show_popup('Invalid handle ({})'.format(hwnd)) return # move mouse to the center of that window oldx, oldy = GetCursorPos() x1, y1, x2, y2 = GetWindowRect(hwnd) x = int((x1 + x2) / 2) y = int((y1 + y2) / 2) SetCursorPos((x, y)) lineN = len(self.view.lines(self.view.sel()[0])) # we need to use %cpaste magic to avoid indentation error # in case more than 2 lines have indentation. if lineN > 2: _pasting(hwnd, r"%cpaste") _pasting(hwnd, msg) _pasting(hwnd, "--") else: _pasting(hwnd, msg) # bring back the mouse cursor SetCursorPos((oldx, oldy)) # bring back the focus to sublime, if subl_handle is known to the plugin if subl_handle: SetForegroundWindow(subl_handle) shell.SendKeys('%') # cancel the Alt key evoked menu
def get_screen_res(): if sys.platform.startswith('linux'): import os lines = [ line for line in os.popen('xrandr').read().split('\n') if line.find('*') != -1 ] return tuple(map(int, lines[0].split()[0].split('x'))) else: try: from win32gui import GetDesktopWindow, GetWindowRect return tuple(GetWindowRect(GetDesktopWindow())[2:]) except ImportError: pass return 800, 600
def main(): print("Inside main") for hwnd, text, cls in enum_windows(): if IsWindowEnabled(hwnd) and IsWindowVisible(hwnd) and text: if not inside_desktop(hwnd): left, top, right, bottom = GetWindowPlacement(hwnd)[4] width = right - left height = bottom - top (x, y) = GetWindowRect(GetDesktopWindow())[:2] try: MoveWindow(hwnd, x, y, width, height, 1) print ("moved '%s'" % text) except: # Ignore windows we can't move. print("Can't move") pass
def captureAlbumArt(self): while (self.windowHandle != GetForegroundWindow()): SetForegroundWindow(self.windowHandle) time.sleep( WAIT_TIME) ## Give Spotify a moment to come to the foreground ShowWindow(self.windowHandle, SW_SHOWMAXIMIZED) time.sleep(WAIT_TIME) ## Give Spotify a second to become maximized ## Get the edges of the window left, top, right, bottom = GetWindowRect(self.windowHandle) left += self.artOffsets[0] bottom -= self.artOffsets[1] ## Get the album art's location from those edges and user specified offsets. region = (left, bottom - self.artSideLength, left + self.artSideLength, bottom) return pyscreenshot.grab(bbox=region, childprocess=False)
def withIn(): sw, sh = GetSystemMetrics(0), GetSystemMetrics(1) if sw > 2000: offset = 200 if sw > 1400: offset = 150 if sw < 1400: offset = 50 hoffset = offset / 2 rect = GetWindowRect(GetForegroundWindow()) x = rect[0] y = rect[1] w = rect[2] - x h = rect[3] - y posx, posy = GetCursorPos() return posx > x and posx < ((w + x) - offset) and posy > y and posy < ( (h + y) - (offset - hoffset))
def try1(): window_list = get_ordered_window() for hwnd in window_list: (left, top, right, bottom) = GetWindowRect(hwnd) if left <= 0 and top <= 0 and right <= 0 and bottom <= 0: continue window_text = GetWindowText(hwnd) if not window_text or window_text.strip() == '': continue if target_value_x > right or target_value_x < left: continue if target_value_y > bottom or target_value_y < top: continue if not IsWindowVisible(hwnd): continue print(window_text) print(left, top, right, bottom) print("-------------------------------------")