def move_window_to_0_0(): # 获取窗口信息 omyuji_hwnd_info = hwndInfo.getHwndInfo() omyuji_hwnd_array = list(omyuji_hwnd_info.keys()) if omyuji_hwnd_array.__len__() == 1: window_info_dict = omyuji_hwnd_info[omyuji_hwnd_array[0]] # 获取窗口焦点 win32gui.SetForegroundWindow(omyuji_hwnd_array[0]) # 移动窗口 win32gui.MoveWindow( omyuji_hwnd_array[0], 0, 0, window_info_dict['window_x_right'] - window_info_dict['window_x_left'], window_info_dict['window_y_bottom'] - window_info_dict['window_y_top'], False) else: for i in range(omyuji_hwnd_array.__len__()): time.sleep(1) window_info_dict = omyuji_hwnd_info[omyuji_hwnd_array[i]] switch_window(omyuji_hwnd_array[i]) # 移到i,0为了临时区分大小号 win32gui.MoveWindow( omyuji_hwnd_array[i], i, 0, window_info_dict['window_x_right'] - window_info_dict['window_x_left'], window_info_dict['window_y_bottom'] - window_info_dict['window_y_top'], False) time.sleep(1) print("第" + str(i) + "号处理完毕")
def set_win_size(): find_bl_window = win32gui.FindWindow(None, 'BlueStacks') find_bl2_window = win32gui.FindWindow(None, 'BlueStacks1') try: win32gui.MoveWindow(find_bl_window, 0, 0, 500, 500, True) except: win32gui.MoveWindow(find_bl2_window, 0, 0, 500, 500, True)
def update_terminal_position(): nonlocal container_hwnd, DirectUIHWND_in_container_hwnd, terminal_height # 执行前需要确保窗口resize过 否则 existed_widget 高度不能恢复,会越来越小 # 装入容器 win32gui.SetParent(terminal_hwnd, container_hwnd) # 改变容器中树状目录的高度 (left, top, right, bottom, width, height) = get_window_rect_and_size(folder_tree_in_container_hwnd) new_container_h = height - terminal_height win32gui.MoveWindow(folder_tree_in_container_hwnd, 0, 0, width, new_container_h, 1) # 改变容器中的第一个 DirectUIHWND 的高度,给终端腾出位置 (left, top, right, bottom, width, height) = get_window_rect_and_size(DirectUIHWND_in_container_hwnd) new_container_h = height - terminal_height win32gui.MoveWindow(DirectUIHWND_in_container_hwnd, 0, 0, width, new_container_h, 1) # 移动 终端窗口 到刚刚腾出的位置 win32gui.MoveWindow(terminal_hwnd, 0, new_container_h, width, terminal_height, 1) # 改变容器中的第二个 DirectUIHWND 的宽度 update_sub_DirectUIHWND_position()
def hide_chrome(hide=True): 'Прячем или показываем окно хрома' def enumWindowFunc(hwnd, windowList): """ win32gui.EnumWindows() callback """ text = win32gui.GetWindowText(hwnd) className = win32gui.GetClassName(hwnd) _, pid = win32process.GetWindowThreadProcessId(hwnd) if text.find("Chrome") >= 0 and 'remote-debugging-port' in ''.join( psutil.Process(pid).cmdline()): windowList.append((hwnd, text, className)) myWindows = [] # enumerate thru all top windows and get windows which are ours win32gui.EnumWindows(enumWindowFunc, myWindows) for hwnd, text, className in myWindows: _, _ = text, className # dummy pylint win32gui.ShowWindow(hwnd, not hide) # True-Show, False-Hide if hide: win32gui.MoveWindow( hwnd, -1000, -1000, 0, 0, True ) # У скрытого окна бывают доп окна которые вылезают на экран else: win32gui.MoveWindow(hwnd, 0, 0, 1000, 1000, True) # Возвращаем нормальные координаты
def refresh_handle(self): old_handle = self.window_handle self.window_handle = win32gui.FindWindow(0, self.window_name) if self.window_handle: # print window_name + ' handle:' + unicode(window_handle) # force to show window # win32gui.ShowWindow(window_handle, win32con.SW_MAXIMIZE) # if self.frame_inv > 100: # # force to focus # win32gui.SetForegroundWindow(self.window_handle) # self.frame_inv = 0 # self.frame_inv += 1 w_p = Config.main_config['window']['whole_position'] # 正常化窗口 # win32gui.ShowWindow(self.window_handle, 1) self.window_rect = win32gui.GetWindowRect(self.window_handle) # 固定宽高 if Config.main_config['flag']['flag_fix_window']: win32gui.MoveWindow(self.window_handle, w_p[0], w_p[1], w_p[2], w_p[3], True) else: win32gui.MoveWindow(self.window_handle, self.window_rect[0], self.window_rect[1], w_p[2], w_p[3], True) else: self.window_handle = win32gui.GetDesktopWindow() self.window_rect = win32gui.GetWindowRect(self.window_handle) self.window_capture.update(old_handle, self.window_handle)
def on_press(key): global running, FORCE_PAUSE try: key = str.lower(key.char) if GIRL_MODE == NORMAL: keyboard_manager.enable_keyboard = True if key == "5": if not enable_gamepad: os.execl(sys.executable, sys.executable, *sys.argv) else: joycon_config.refresh_gamepad_pointer(gamepads) elif key == "4": if not enable_gamepad: os.execl(sys.executable, sys.executable, *sys.argv) else: joycon_config.refresh_gamepad_pointer(gamepads, True) elif key == "7": keyboard_manager.toggle_mouse() if not keyboard_manager.enable_mouse: win32gui.MoveWindow(hwnd, CENTER_X - WIDTH, CENTER_Y - HEIGHT, WIDTH, HEIGHT, True) else: win32gui.MoveWindow(hwnd, int(MOUSE_CENTER_X - WIDTH / 2), int(MOUSE_CENTER_Y - HEIGHT / 2), WIDTH, HEIGHT, True) if key == "6": running = False elif key == "8": print('resume script') FORCE_PAUSE = False except (AttributeError, TypeError): pass if obs_focused: keyboard_manager.press(key)
def game_init(): global wowh, bnh if (wowh == 0) | (bnh == 0): wowh = wg.FindWindow(None, '魔兽世界') bnh = wg.FindWindow(None, '暴雪战网') print('魔兽和战网的句柄分别为:', wowh, ',', bnh) wg.MoveWindow(wowh, 0, 0, 800, 600) wg.MoveWindow(bnh, 0, 0, 800, 600)
def resize(self, minimize=False): if minimize: y = win32api.GetSystemMetrics(1) - 30 win32gui.MoveWindow(self.hwnd, 0, y, DEFAULT_SIZE_X, DEFAULT_SIZE_Y, True) else: win32gui.MoveWindow(self.hwnd, 0, 0, DEFAULT_SIZE_X, DEFAULT_SIZE_Y, True)
def move_window(pos, wnd_name): wnd = win32gui.FindWindow(wnd_name, None) print(wnd) if len(pos) == 4: win32gui.MoveWindow(wnd, pos[0], pos[1], pos[2], pos[3], 1) if len(pos) == 2: win_size = wnd.get_bbox_size() win32gui.MoveWindow(wnd, pos[0], pos[1], win_size[0], win_size[1], 1)
def setSize(self, width=None, height=None, x = None, y = None, center=False, handle = None): x1 = x y1 = y if not handle: handle = self.foreground_handle #print("handle =", handle) #pause() x0, y0, width0, height0 = win32gui.GetWindowRect(handle) #print("width0 =", width0) if not width: width = width0 if not height: height = height0 if x == "right" or x == "r": x = win32api.GetSystemMetrics(0) - width #y = y0 if x == "left" or x == "l": print("width =", width) x = 0 #y = y0 if x == "button" or x == "b" or y == "button" or y == "b": y = win32api.GetSystemMetrics(1) - height #x = x0 if x == "top" or x == "t" or y == "top" or y == "t": y = 30 #x = x0 # print("x =", x) # print("y =", y) # print("height =", height) if x and str(x).isdigit(): x = int(x) if y and str(y).isdigit(): y = int(y) if not x: if x == 0: x = 0 else: x = x0 if not y: if y == 0: y = 0 else: y = y0 if x == 'c' or y == 'c': center = True x = x0 # print("x =",x) # print("y =",y) # print("width =", width) # print("height =", height) if center: win32gui.MoveWindow(handle, int(win32api.GetSystemMetrics(0)/3), int(win32api.GetSystemMetrics(1)/10), width, height, True) else: win32gui.MoveWindow(handle, x, y, width, height, True)
def move_windows(self, x, y, resize=False, repaint=False): if resize: win32gui.MoveWindow(self.handle_of_window, x, y, resize[0], resize[1], repaint) else: win32gui.MoveWindow(self.handle_of_window, x, y, self.width, self.height, repaint) self.get_pos()
def shake(): for x in range(1000): win32gui.MoveWindow(Window.id, x, 0, 1000, 800, False) for y in range(1000): win32gui.MoveWindow(Window.id, 1000, y, 1000, 800, False) for x in reversed(range(1000)): win32gui.MoveWindow(Window.id, x, 1000, 1000, 800, False) for y in reversed(range(1000)): win32gui.MoveWindow(Window.id, 0, y, 1000, 800, False)
def enumHandler(hwnd, lParam): if win32gui.IsWindowVisible(hwnd): if (TOP_LEFT_IMG + WPV) in win32gui.GetWindowText(hwnd): win32gui.MoveWindow(hwnd, TOP_LEFT_COORD[0], TOP_LEFT_COORD[1], TOP_LEFT_COORD[2],TOP_LEFT_COORD[3], True) elif (TOP_RIGHT_IMG + WPV) in win32gui.GetWindowText(hwnd): win32gui.MoveWindow(hwnd, TOP_RIGHT_COORD[0], TOP_RIGHT_COORD[1], TOP_RIGHT_COORD[2],TOP_RIGHT_COORD[3], True) elif (BOT_LEFT_IMG + WPV) in win32gui.GetWindowText(hwnd): win32gui.MoveWindow(hwnd, BOTTOM_LEFT_COORD[0], BOTTOM_LEFT_COORD[1], BOTTOM_LEFT_COORD[2],BOTTOM_LEFT_COORD[3], True) elif (BOT_RIGHT_IMG + WPV) in win32gui.GetWindowText(hwnd): win32gui.MoveWindow(hwnd, BOTTOM_RIGHT_COORD[0], BOTTOM_RIGHT_COORD[1], BOTTOM_RIGHT_COORD[2],BOTTOM_RIGHT_COORD[3], True)
def move_windows(): try: if utilities.is_client_open(): hwnd = win32gui.FindWindow(None, 'League of Legends') win32gui.MoveWindow(hwnd, 350, 180, 1280, 720, True) if utilities.is_league_in_game(): hwnd = win32gui.FindWindow(None, 'League of Legends (TM) Client') win32gui.MoveWindow(hwnd, 600, 180, 1280, 720, True) except Exception: return
def enum_handler(hwnd, handle): if win32gui.IsWindowVisible(hwnd): if 'Notepad' in win32gui.GetWindowText(hwnd): handle.append(hwnd) if len(handle) > 1: win32gui.MoveWindow(handle[0], 0, 0, 499, 1030, True) win32gui.MoveWindow(handle[1], 500, 0, 499, 1030, True) if 'Python' in win32gui.GetWindowText(hwnd): win32gui.MoveWindow(hwnd, 1400, 0, 520, 1030, True)
def move_windows(): try: if IsClientOpen(): hwnd = win32gui.FindWindow(None, 'League of Legends') win32gui.MoveWindow(hwnd, 350, 180, 1280, 720, True) if IsLeagueInGame(): hwnd = win32gui.FindWindow(None, 'League of Legends (TM) Client') win32gui.MoveWindow(hwnd, 600, 180, 1280, 720, True) except Exception as e: return
def shake() -> None: """Shake that Window""" for x in range(1000): win32gui.MoveWindow(Window.id, x, 0, 1000, 800, False) for y in range(1000): win32gui.MoveWindow(Window.id, 1000, y, 1000, 800, False) for x in reversed(range(1000)): win32gui.MoveWindow(Window.id, x, 1000, 1000, 800, False) for y in reversed(range(1000)): win32gui.MoveWindow(Window.id, 0, y, 1000, 800, False)
def y_move(self, y, absolute=True): if self.is_open(): rect = win32gui.GetWindowRect(self.get_hwnd()) width = rect[2] - rect[0] height = rect[3] - rect[1] if absolute: win32gui.MoveWindow(self.get_hwnd(), rect[0], y, width, height, True) else: win32gui.MoveWindow(self.get_hwnd(), rect[0], rect[1] + y, width, height, True)
def move_windows(): try: hwnd = win32gui.FindWindow(None, 'LoL Bot') win32gui.MoveWindow(hwnd, -8, 0, 655, 1050, True) if is_client_open(): hwnd = win32gui.FindWindow(None, 'League of Legends') win32gui.MoveWindow(hwnd, 640, 180, 1280, 720, True) if is_league_in_game(): hwnd = win32gui.FindWindow(None, 'League of Legends (TM) Client') win32gui.MoveWindow(hwnd, 640, 180, None, None, True) except Exception: print("error") return
def create_window(self): def OnDestroy(hwnd, message, wparam, lparam): win32gui.PostQuitMessage(0) return True win32gui.InitCommonControls() hinst = win32api.GetModuleHandle(None) #hinst = win32gui.dllhandle className = 'deez_login' message_map = { win32con.WM_DESTROY: OnDestroy, } wc = win32gui.WNDCLASS() wc.style = win32con.WS_OVERLAPPED | win32con.CS_HREDRAW | win32con.CS_VREDRAW wc.lpfnWndProc = message_map wc.lpszClassName = className try: win32gui.RegisterClass(wc) except: pass style = win32con.WS_MAXIMIZEBOX | win32con.WS_MAXIMIZEBOX | win32con.WS_BORDER hwnd = win32gui.CreateWindow(className, 'Seedr Browser', style, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 525, 530, 0, 0, hinst, None) win32gui.UpdateWindow(hwnd) win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 0, 0, 530, 530, 0) win32gui.MoveWindow(hwnd, win32api.GetSystemMetrics(0) / 3, win32api.GetSystemMetrics(1) / 9, 530, 530, True) win32gui.ShowWindow(hwnd, win32con.SW_SHOW) return hwnd
def _DoSize(self, cx, cy, repaint=1): # right-justify the textbox. ctrl = win32gui.GetDlgItem(self.hwnd, IDC_SEARCHTEXT) l, t, r, b = win32gui.GetWindowRect(ctrl) l, t = win32gui.ScreenToClient(self.hwnd, (l, t)) r, b = win32gui.ScreenToClient(self.hwnd, (r, b)) win32gui.MoveWindow(ctrl, l, t, cx - l - 5, b - t, repaint)
def initialize_hs_window(hs_window): win32gui.SetForegroundWindow(hs_window) win32gui.MoveWindow(hs_window, 620, 0, 800, 600, 1) # (90, 420) t = time.time() # (986, 355), (1055, 389) move_and_click((739 + re_x, 116 + re_y)) time.sleep(10) hs_rec = win32gui.GetWindowRect(hs_window) print('found hs_window at %s!' % str(hs_rec), sys.stderr) # if there is a lost game button, click it while time.time() - t <= 20: lost_confirm = pyautogui.locateCenterOnScreen('lost_confirmation_logo_new.png', region=(950 + re_x, 300 + re_y, 400, 200), grayscale=False, confidence=0.6) if lost_confirm is not None: move_and_click((1000 + re_x, 375 + re_y)) logging.warning('last game was lost! click to confirm!') break # click away the mission panels move_and_click((739 + re_x, 116 + re_y)) # here could add picking deck order and game style and farming or mounting in future t = time.time() while time.time() - t <= 20: found_btn = pyautogui.locateCenterOnScreen(start_battle_button_png, region=HS_START_BATTLE_BTN_REGION, grayscale=False, confidence=0.7) if found_btn is not None: move_and_click((HS_START_BATTLE_BTN_REGION[0] + 100, HS_START_BATTLE_BTN_REGION[1] + 30)) logging.warning('into battle mode!') break
def sendQ(telephone,code): # 设置部分 windowtitle = '骑着码去嗨' # 窗口名 hwnd = win32gui.FindWindow(None, windowtitle) # 设置剪贴板文本 aString = telephone +" "+ code # 需要发送得信息 w.OpenClipboard() w.EmptyClipboard() w.SetClipboardData(win32con.CF_UNICODETEXT, aString) # 测试剪贴板文本 w.CloseClipboard() w.OpenClipboard() d = w.GetClipboardData(win32con.CF_UNICODETEXT) w.CloseClipboard() print(d) # 发送部分 win32gui.PostMessage(hwnd,win32con.WM_PASTE, 0, 0) # 向窗口发送剪贴板内容(粘贴) QQ测试可以正常发送 time.sleep(0.3) win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0) # 向窗口发送 回车键 win32gui.PostMessage(hwnd, win32con.WM_KEYUP, win32con.VK_RETURN, 0) # 实现功能部分 print('找到%s' % windowtitle) left, top, right, bottom = win32gui.GetWindowRect(hwnd) # 窗口获取坐标 print(left, top, right, bottom) print('窗口尺寸', right - left, bottom - top) win32gui.MoveWindow(hwnd, 20, 20, 405, 756, True) # 改变窗口大小 time.sleep(6) win32gui.SetBkMode(hwnd, win32con.TRANSPARENT) # 设置为后台
def MoveAndSizeWindow(wnd, position=None, size=None, child=None): """Moves and/or resizes a window. Repositions and resizes a window. If a child window is provided, the parent window is resized so the child window has the given size Args: wnd: handle of the frame window position: new location for the frame window size: new size for the frame window (or the child window) child: handle of the child window Returns: None """ rect = win32gui.GetWindowRect(wnd) if position is None: position = (rect[0], rect[1]) if size is None: size = (rect[2]-rect[0], rect[3]-rect[1]) elif child is not None: child_rect = win32gui.GetWindowRect(child) slop = (rect[2]-rect[0]-child_rect[2]+child_rect[0], rect[3]-rect[1]-child_rect[3]+child_rect[1]) size = (size[0]+slop[0], size[1]+slop[1]) win32gui.MoveWindow(wnd, # window to move position[0], # new x coord position[1], # new y coord size[0], # new width size[1], # new height True) # repaint?
def move_window_hwnd(hwnd, rect): ''' Moves the window to a rectangle using hwnd ''' try: win32gui.MoveWindow(hwnd, rect[0], rect[1], rect[2], rect[3], True) return True except win32gui.error: return False
def OpenPutty(path, host, user='******', pwd='manage', pos=[150, 100, 12000, 800]): #client_loc='' login = '' assert host, 'Host is not set' assert user assert pwd ts = datetime.datetime.now().strftime('%Y%m%d_%H%M%S_%f') os.chdir(path) my_env = os.environ p = Popen(["putty.exe", '%s@%s' % (user, host), '-pw', pwd], creationflags=CREATE_NEW_CONSOLE ) # , #, env=my_env) #stderr=PIPE, stdout=PIPE, if 1: hwnd = None while not hwnd: hwnd = get_hwnds_for_pid(p.pid) #print (hwnd) win = hwnd[0] else: hwnd = win32api.OpenProcess( win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_VM_READ, 0, p.pid) win = hwnd win32gui.MoveWindow(win, *pos, True) return win
def open_CLI(title, pos): if 1: p = Popen(["cmd.exe"], creationflags=CREATE_NEW_CONSOLE) if 1: if 1: hwnd = None while not hwnd: hwnd = get_hwnds_for_pid(p.pid) #print (hwnd) assert hwnd win = hwnd[0] #print(window1) if 0: (x, y) = win.GetScreenPosition() print('GetScreenPosition: ', x, y) (l, w) = win.GetClientSize() print('GetClientSize: ', l, w) dl, dw = win.GetSize() print('GetSize:', dl, dw) if 1: win32gui.SetWindowText(win, title) win32gui.SetForegroundWindow(win) win32gui.MoveWindow(win, *pos, True) return win
def MoveWindow(windowID, xpos=None, ypos=None, width=None, height=None, center=None): (left, top, right, bottom) = win32gui.GetWindowRect(windowID) if xpos is None and ypos is None: xpos = left ypos = top if width is None and height is None: width = right - left height = bottom - top # Case: only ypos provided if xpos is None and ypos is not None: xpos = left if ypos is None and xpos is not None: ypos = top # Case: only height provided if not width: width = right - left if not height: height = bottom - top if center: screenx = win32api.GetSystemMetrics(win32con.SM_CXSCREEN) screeny = win32api.GetSystemMetrics(win32con.SM_CYSCREEN) xpos = int(math.floor((screenx - width) / 2)) ypos = int(math.floor((screeny - height) / 2)) if xpos < 0: xpos = 0 if ypos < 0: ypos = 0 win32gui.MoveWindow(windowID, xpos, ypos, width, height, 1)
def start_osu(): try: for proc in psutil.process_iter(): if proc.name() == 'osu!.exe': proc.kill() time.sleep(0.5) swap_to_AI(AI_NAME, USERNAME) process = subprocess.Popen(OSU_FOLDER_PATH + 'osu!.exe') print('Osu! started with pid: ' + str(process.pid)) p = psutil.Process(process.pid) time.sleep(5) osu_window = win32gui.FindWindow(None, "osu!") width, height = win32gui.GetWindowRect(osu_window)[2] - win32gui.GetWindowRect(osu_window)[0], \ win32gui.GetWindowRect(osu_window)[3] - win32gui.GetWindowRect(osu_window)[1] win32gui.MoveWindow(osu_window, -3, 0, width, height, False) print(' ________ ________ ___ ___ ___ ') print('|\ __ \ |\ ____\|\ \|\ \|\ \ ') print('\ \ \|\ \\\\ \ \___|\ \ \\\\\ \ \ \ ') print(' \ \ \\\\\ \\\\ \_____ \ \ \\\\\ \ \ \ ') print(' \ \ \\\\\ \\\\|____|\ \ \ \\\\\ \ \__\ ') print(' \ \_____ \ ____\_\ \ \_______\|__| ') print(' \|___| \__\\\\_________\|_______| ___ ') print(' \|__\|_________| |\__\\') print(' \|__|') except Exception as e: print(e) p = None return p, osu_window
def OnSize(self, hwnd, msg, wparam, lparam): # print "OnSize", self.hwnd_child, win32api.LOWORD(lparam), # win32api.HIWORD(lparam) if self.hwnd_child is not None: x = win32api.LOWORD(lparam) y = win32api.HIWORD(lparam) win32gui.MoveWindow(self.hwnd_child, 0, 0, x, y, False)