def tap_key(key, delay=1.0): if 操作完后回到原来窗口: original_window = GetForegroundWindow() SetForegroundWindow(find_window(title=FGO窗口名)) k.tap_key(key) time.sleep(0.02) if 操作完后回到原来窗口: SetForegroundWindow(original_window) time.sleep(delay)
def open_zoom(): app = Application().start("Zoom.exe") time.sleep(3) zoom_window = find_window(title="Zoom - 免费账号") MoveWindow(zoom_window, 0, 0, 400, 400) SetForegroundWindow(zoom_window) return zoom_window
def set_foreground_window(): w = user._app.top_window() #bring window into foreground if w.HasStyle(win32defines.WS_MINIMIZE): # if minimized ShowWindow(w.wrapper_object(), 9) # restore window state else: SetForegroundWindow(w.wrapper_object()) #bring to front
def ths_restore(): main_window = pywinauto.application.Application().connect( title=u'网上股票交易系统5.0').top_window() if main_window.HasStyle(win32defines.WS_MINIMIZE): # if minimized ShowWindow(main_window.wrapper_object(), 9) # restore window state else: SetForegroundWindow(main_window.wrapper_object()) # bring to front
def take_screenshot(date_string): SetForegroundWindow(find_window(best_match='NiceHash')) x, y, a, b = pyautogui.locateOnScreen('ir_sample/nicehash_header.png') ImageGrab.grab(bbox=(x - 1, y, x - 1 + NH_WINDOW_W, y + NH_WINDOW_H)).save( "screenshots/{0}.png".format(date_string), "PNG")
def start_miner(active_miner_exe): def check_for_hardware_warning(active_miner_exe): print("searching for hardware warning...") found = pyautogui.locateOnScreen('ir_sample/{0}/dismiss.png'.format( active_miner_exe.dir_name)) if found: x, y, a, b = found pyautogui.click(x + 20, y + 10) def check_for_start_button(active_miner_exe): print("searching for start button...") print('ir_sample/{0}/start.png'.format(active_miner_exe.dir_name)) found = pyautogui.locateOnScreen('ir_sample/{0}/start.png'.format( active_miner_exe.dir_name)) if found: print("found the button") x, y, a, b = found pyautogui.click(x + active_miner_exe.start_offset[0], y + active_miner_exe.start_offset[1]) else: print("did not found the button") print(active_miner_exe.process_name) print("Starting {0}".format(active_miner_exe.process_name)) if not miner_exe_running(active_miner_exe.process_name): subprocess.Popen( active_miner_exe.process_path ) # this will depend on where you have your mining software installed time.sleep( 5) # delay to let miner exe boot, probably overkill, but whatever SetForegroundWindow(find_window(best_match=active_miner_exe.process_name)) if active_mining_exe == "n": check_for_hardware_warning() check_for_start_button(active_miner_exe)
def positionBrowser(self): window = self.currentWindow info = window.WrapperObject() window.MoveWindow(0, 0, 1347, 1028) SetForegroundWindow(info.handle) ShowWindow(info.handle, 5) SetFocus(info.handle) SetActiveWindow(info.handle)
def take_screenshot(date_string, active_miner_exe): SetForegroundWindow( find_window(best_match=active_miner_exe.process_name)) x, y, a, b = pyautogui.locateOnScreen( 'ir_sample/{0}/header.png'.format(active_miner_exe.dir_name)) ImageGrab.grab(bbox=(x, y, x - 1 + active_miner_exe.window_width, y + active_miner_exe.window_height)).save( "screenshots/{0}.png".format(date_string), "PNG")
def set_foreground(self): """ Bring the window foreground Returns: None """ SetForegroundWindow(self._top_window)
def close_share_window(zoom_window): # time.sleep(3) # 关闭分享 # keyboard.SendKeys('%s') SetForegroundWindow(zoom_window) keyboard.SendKeys('%{F4}') time.sleep(1) # 确认关闭分享 keyboard.SendKeys('{SPACE}')
def run_or_raise(class_name, path): """Launch a program or focus it if already running.""" try: windows = find_windows(class_name=class_name) # XXX: Finds most recent instance, not current workspace's one. last_window = windows[-1] except IndexError: Application().start(path) else: SetForegroundWindow(last_window)
def WindowInFront(processpath): #Bring window in front app = Application().connect(path=processpath) w = app.top_window() #bring window into foreground if w.HasStyle(win32defines.WS_MINIMIZE): # if minimized ShowWindow(w.wrapper_object(), 9) # restore window state else: SetForegroundWindow(w.wrapper_object()) #bring to front
def run(): zoom_window = open_zoom() activate_movie_window() time.sleep(1) SetForegroundWindow(zoom_window) mouse.click(coords=(150, 150)) # 等待连接 time.sleep(10) # 开始分享 keyboard.SendKeys('%s') # 选择分享哪个程序 time.sleep(1) keyboard.SendKeys('{TAB}') time.sleep(1) keyboard.SendKeys('{TAB}') time.sleep(1) keyboard.SendKeys('{DOWN}') time.sleep(1) keyboard.SendKeys('{RIGHT}') time.sleep(1) # 勾选共享电脑声音 keyboard.SendKeys('{TAB}') time.sleep(1) keyboard.SendKeys('{SPACE}') # 确认分享 time.sleep(1) share_window = find_window(title='选择一个您想共享的窗口或程序') SetForegroundWindow(share_window) time.sleep(1) keyboard.SendKeys('{TAB}') time.sleep(1) keyboard.SendKeys('{TAB}') time.sleep(1) # 定位到了“开始共享”按钮 keyboard.SendKeys('{SPACE}') time.sleep(60 * 30) close_share_window(zoom_window) close_zoom(zoom_window)
def TogglePin(self, event): if event.IsChecked(): try: SetForegroundWindow(find_window(title=self.GetTitle())) except: pass self.SetWindowStyleFlag( (wx.CLIP_CHILDREN | wx.STAY_ON_TOP | wx.FRAME_NO_TASKBAR | wx.BORDER_NONE | wx.FRAME_SHAPED)) else: self.SetWindowStyleFlag((wx.CLIP_CHILDREN | wx.FRAME_NO_TASKBAR | wx.BORDER_NONE | wx.FRAME_SHAPED))
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 goto_app(name): # app = Application().connect(path="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe") # try: app = Application().connect(title_re=".*" + name.strip() + ".*") # except Exception as e: # print( "Error:: goto_app(): ", sys.exc_info()[0]) # os._exit(1) print("IS HERE") w = app.top_window() #bring window into foreground if w.has_style(win32defines.WS_MINIMIZE): # if minimized ShowWindow(w.wrapper_object(), 9) # restore window state else: SetForegroundWindow(w.wrapper_object()) #bring to front
def run_CAN_simulator(): os.system(r".\su_tool\RunCAN_Simulator.bat") time.sleep(2) app = Application().connect(path=r".\su_tool\MBC_CAN-rx_Simulator.exe") w = app.top_window() if w.HasStyle(win32defines.WS_MINIMIZE): # if minimized ShowWindow(w.wrapper_object(), 9) # restore window state else: SetForegroundWindow(w.wrapper_object()) #bring to front wsh = comclt.Dispatch("WScript.Shell") for count in range(0, 9): wsh.SendKeys("{+}") time.sleep(1) wsh.SendKeys("{2}") for count in range(0, 9): wsh.SendKeys("{+}")
def login(self, user, password, exe_path, comm_password=None, **kwargs): """ 登陆客户端 :param user: 账号 :param password: 明文密码 :param exe_path: 客户端路径类似 r'C:\中国银河证券双子星3.2\Binarystar.exe', 默认 r'C:\中国银河证券双子星3.2\Binarystar.exe' :param comm_password: 通讯密码, 华泰需要,可不设 :return: """ try: self._app = pywinauto.Application().connect(path=self._run_exe_path(exe_path), timeout=1) except Exception: self._app = pywinauto.Application().start(exe_path) # wait login window ready while True: try: self._app.top_window().Edit1.wait('ready') break except RuntimeError: pass self._app.top_window().Edit1.type_keys(user) self._app.top_window().Edit2.type_keys(password) edit3 = self._app.top_window().window(control_id=0x3eb) while True: try: code = self._handle_verify_code() edit3.type_keys( code ) time.sleep(1) self._app.top_window()['确定(Y)'].click() # detect login is success or not try: self._app.top_window().wait_not('exists', 5) break except: self._app.top_window()['确定'].click() pass except Exception as e: pass self._app = pywinauto.Application().connect(path=self._run_exe_path(exe_path), timeout=10) #self._main = self._app.window(title=self._config.TITLE) self._main = self._app.top_window() SetForegroundWindow( self._main.wrapper_object())
try: browser = AutoApp().connect(title_re=".*Mozilla") except: print("Can't find mozilla firefox") exit() window = browser.top_window() info = window.WrapperObject() rect = info.rectangle() oldX = -1 oldY = -1 try: window.MoveWindow(0, 0, 1347, 1028) SetForegroundWindow(info.handle) ShowWindow(info.handle, 5) SetFocus(info.handle) SetActiveWindow(info.handle) except Exception as e: print(e) class Capture(Singleton): def firstTime(self, **kwargs): self.keys = [] self.captureToggle = False self.lock = Lock() basedir = os.path.abspath(os.path.dirname(__file__)) self.imagedir = basedir + '\\' + 'images' os.makedirs(self.imagedir, exist_ok=True)
def set_foreground(self): SetForegroundWindow(self._top_window)
def restoreWindow(self): if self.__main_window.has_style(win32defines.WS_MINIMIZE): # if minimized ShowWindow(self.__main_window.wrapper_object(), 9) # restore window state else: SetForegroundWindow(self.__main_window.wrapper_object()) # bring to front
def nicehash_exe_running(): try: SetForegroundWindow(find_window(best_match='NiceHash')) except MatchError: return False return True
def activate_movie_window(): movie_window = find_window(title="电影和电视") SetForegroundWindow(movie_window)
# -*- coding: utf-8 -*- # 每次将模拟器滑到适当的位置,然后cmd里敲回车 # 完成截图之后继续下一次操作 from cut import cut from processor import process_cut from pywinauto.win32functions import SetForegroundWindow, GetForegroundWindow print('截图助手已启动') print('输入已进行的次数:') op = input() try: op = int(op) except ValueError: op = 0 while True: print('已完成%d次截图,回车以开始下一次……' % op) input() original_window = GetForegroundWindow() cut() # process_cut(start_index=op*5) SetForegroundWindow(original_window) op += 1
def 等待操作(): print('等待操作') SetForegroundWindow(find_window(title=FGO窗口名)) input()
def miner_exe_running(process_name): try: SetForegroundWindow(find_window(best_match=process_name)) except MatchError: return False return True
def wincheck(): #bring window into foreground if w.HasStyle(win32defines.WS_MINIMIZE): # if minimized ShowWindow(w.wrapper_object(), 9) # restore window state else: SetForegroundWindow(w.wrapper_object()) #bring to front
def close_zoom(zoom_window): # 关闭 Zoom SetForegroundWindow(zoom_window) keyboard.SendKeys('%{F4}') time.sleep(1) keyboard.SendKeys('{SPACE}')
helpers.init() def get_rect(hwnd): rect = win32gui.GetWindowRect(hwnd) return rect def screen_grab(): box = (x1, y1, x2, y2) im = ImageGrab.grab(box) return im window = find_window(title="bob_robot_sim") SetForegroundWindow(window) x1, y1, x2, y2 = get_rect(window) class RandomNoise: random_signal_counter = 0 def __init__(self): pass def get_signal(self): if self.random_signal_counter > 0: self.random_signal_counter -= 1 return True
from pykeyboard import PyKeyboard from pywinauto.win32functions import SetForegroundWindow from config import * from auto import * from play import * k = PyKeyboard() op = 0 while True: print('开始脚本,当前完成次数:%d' % op) SetForegroundWindow(find_window(title=FGO窗口名)) if 自动选卡策略: tap_key(' ', 普通操作时间) wait_until_next_phase() else: tap_key(' ', 进本时间) 脚本() tap_key('D', 点掉羁绊结算窗口时间) tap_key('D', 点掉经验结算窗口时间) tap_key(' ', 出本时间) tap_key('1', 普通操作时间) if 自动选卡策略: src_image = cut(save_picture=False) if 自动吃苹果: pos = ac.find_template(src_image, apple_image) if pos is not None and pos['confidence'] > 0.95: tap_key('Q', 普通操作时间) tap_key('Y', 普通操作时间) elif 自动碎石: pos = ac.find_template(src_image, stone_image)