def send_msg(self, msg): self.lock.acquire() # for this problem: https://bbs.csdn.net/topics/390973288?page=1 win32api.keybd_event(0x11, 0, 0, 0) # ctrl win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0) # release key win32gui.ShowWindow(self.con_hwnd, win32con.SW_MAXIMIZE) win32gui.SetForegroundWindow(self.con_hwnd) win32clipboard.OpenClipboard() win32clipboard.EmptyClipboard() win32clipboard.SetClipboardText(msg, win32con.CF_UNICODETEXT) win32clipboard.CloseClipboard() win32api.keybd_event(0x11, 0, 0, 0) # ctrl win32api.keybd_event(0x56, 0, 0, 0) # 'v' win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0) # release key win32api.keybd_event(0x56, 0, win32con.KEYEVENTF_KEYUP, 0) # release key win32api.keybd_event(0x11, 0, 0, 0) # ctrl win32api.keybd_event(0x0d, 0, 0, 0) # enter win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0) # release key win32api.keybd_event(0x0d, 0, win32con.KEYEVENTF_KEYUP, 0) # release key time.sleep(1) win32gui.SetForegroundWindow(self.cur_hwnd) self.lock.release()
def generateImage(word): saveToXML(word) print("Waiting for Unity to generate image...") hwnd = 0 hwnd = win32gui.FindWindow(None, "ToonRendering") if hwnd == 0: def callback(h, extra): if "ToonRendering" in win32gui.GetWindowText(h): extra.append(h) return True extra = [] win32gui.EnumWindows(callback, extra) if extra: hwnd = extra[0] if hwnd == 0: print( "Unity window 'ToonRendering' not found. Please ensure Unity is running and switch to it manually." ) win32gui.SetForegroundWindow(hwnd) global genimg while True: if os.path.isfile(genimg + str(word['idnumber']) + ".png"): break imageString = tf.read_file(genimg + str(word['idnumber']) + ".png") hwnd = 0 hwnd = win32gui.FindWindow(None, "Art Style Replicator") if hwnd == 0: def callback(h, extra): if "Art Style Replicator" in win32gui.GetWindowText(h): extra.append(h) return True extra = [] win32gui.EnumWindows(callback, extra) if extra: hwnd = extra[0] if hwnd == 0: print("Python window 'Art Style Replicator' not found.") win32gui.SetForegroundWindow(hwnd) imageString = tf.image.decode_png(imageString, dtype=tf.uint16) imageString = tf.image.convert_image_dtype(imageString, tf.float32) ############### Convert word['image'] to tensor4 ################ global train_data_width global train_data_height word['image'] = tf.reshape(imageString, [1, train_data_height, train_data_width, 3]) print("Retrieved generated image") return word['image']
def receive_msg(self): self.lock.acquire() # for this problem: https://bbs.csdn.net/topics/390973288?page=1 win32api.keybd_event(0x11, 0, 0, 0) # ctrl win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0) # release key win32gui.ShowWindow(self.con_hwnd, win32con.SW_MAXIMIZE) win32gui.SetForegroundWindow(self.con_hwnd) time.sleep(1) x_mid = int(win32api.GetSystemMetrics(win32con.SM_CXSCREEN) / 2) y_mid = int(win32api.GetSystemMetrics(win32con.SM_CYSCREEN) / 2) win32api.SetCursorPos([x_mid, y_mid]) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0) # left mouse button win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0) # release key win32api.keybd_event(0x11, 0, 0, 0) # ctrl win32api.keybd_event(0x41, 0, 0, 0) # 'a' win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0) # release key win32api.keybd_event(0x41, 0, win32con.KEYEVENTF_KEYUP, 0) # release key win32api.keybd_event(0x11, 0, 0, 0) # ctrl win32api.keybd_event(0x43, 0, 0, 0) # 'c' win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0) # release key win32api.keybd_event(0x43, 0, win32con.KEYEVENTF_KEYUP, 0) # release key time.sleep(1) win32clipboard.OpenClipboard() text = win32clipboard.GetClipboardData(win32con.CF_UNICODETEXT) win32clipboard.CloseClipboard() win32gui.SetForegroundWindow(self.cur_hwnd) time.sleep(1) self.lock.release() return text
def SetAsForegroundWindow(window): shell = win32com.client.Dispatch("WScript.Shell") shell.SendKeys('%') win32gui.SetForegroundWindow(window) win32gui.ShowWindow( window, win32con.SW_RESTORE ) #https://msdn.microsoft.com/pt-br/library/windows/desktop/ms633548(v=vs.85).aspx
def _window_enum_callback(self, hwnd, wildcard): tid, pid = win32process.GetWindowThreadProcessId(hwnd) title = win32gui.GetWindowText(hwnd) if (self.lock_pid == pid) and (title == wildcard): win32gui.ShowWindow(hwnd, 9) win32gui.SetForegroundWindow(hwnd) win32gui.SetActiveWindow(hwnd)
def SetAsForegroundWindow(self, name): # 获取后台窗口的句柄,注意后台窗口不能最小化 hWnd = win32gui.FindWindow(None, name) # 参数1为类名,参数2为标题 # 通过pywin32模块下的SetForegroundWindow函数调用时,会出现error: (0, 'SetForegroundWindow', 'No error message is available') # 报错,后经网上查询确认,为pywin32模块下的一个小bug,在该函数调用前,需要先发送一个其他键给屏幕,如ALT键 。 self.shell.SendKeys('%') print(hWnd) win32gui.SetForegroundWindow(hWnd)
def getFrame(): hwnd = win32gui.FindWindow(None, r'Grand Theft Auto V') win32gui.SetForegroundWindow(hwnd) dimensions = win32gui.GetWindowRect(hwnd) pressKey(DIK_E) pressKey(DIK_PAGE_UP) sleep(1) image = ImageGrab.grab(dimensions) return image
def SetAsForegroundWindow(window, restore=False): shell = win32com.client.Dispatch("WScript.Shell") shell.SendKeys( '%' ) #msg obrigatoria antes de qualquer mudanca na gui do windows win32gui.SetForegroundWindow(window) if restore: win32gui.ShowWindow( window, win32con.SW_RESTORE ) #https://msdn.microsoft.com/pt-br/library/windows/desktop/ms633548(v=vs.85).aspx
def on_press(key): if key == Key.end: global startScript, inputBet, inputColor if inputBet != None and inputColor != None: startScript = not startScript if startScript: hwnd = win32gui.FindWindow(None, r'Grand Theft Auto V') win32gui.SetForegroundWindow(hwnd) win32gui.MoveWindow(hwnd, 0, 0, 1336, 797, True) print("\n\nScript status: ", startScript, "\n\n")
def set_foreground(self): """ 将窗口放在前台并激活 """ # _shell = win32com.client.Dispatch("WScript.Shell") # _shell.SendKeys('%') win32api.keybd_event(13, 0, 0, 0) if self.parent is None: # 避免非顶级窗口 result = win32gui.SetForegroundWindow(self.handle) if result == 0: self._raise_last_error()
def click(self, x, y): if (self.device_type == 'emu'): win32gui.ShowWindow(self.device_id, win32con.SW_SHOW) win32gui.SetForegroundWindow(self.device_id) win32api.SetCursorPos([self.device_left + x, self.device_top + y]) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) time.sleep(0.5) elif (self.device_type == 'android'): adb.click(x, y) time.sleep(0.5) return
def screenshot(self): if not self.dimensions: hwnd = win32gui.FindWindow(None, r'Euro Truck Simulator 2') win32gui.SetForegroundWindow(hwnd) dimensions = win32gui.GetWindowRect(hwnd) dimensions = list(dimensions) dimensions[0] += 10 dimensions[1] += 30 dimensions[2] -= 10 dimensions[3] -= 10 self.dimensions = (dimensions) return ImageGrab.grab(bbox=self.dimensions)
def screenshot(self): img = None if (self.device_type == 'emu'): hwnd = self.device_id left = self.device_left top = self.device_top width = self.device_width height = self.device_height win32gui.ShowWindow(hwnd, win32con.SW_SHOW) win32gui.SetForegroundWindow(hwnd) img = np.array( ImageGrab.grab(bbox=(left, top, left + width, top + height))) img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) elif (self.device_type == 'android'): img = adb.screenshot() return Image(img)
def show_excel_window(filename): ''' takes: - filename: string returns: - true if excel with specified file is opened - false if excel with specified file is closed note to self: filename must be a filename and not filepath. ''' top_windows = [] win32gui.EnumWindows(windowEnumerationHandler, top_windows) for i in top_windows: if filename.lower() in i[1].lower(): win32gui.ShowWindow(i[0],5) win32gui.SetForegroundWindow(i[0]) return True return False
def screencap(window_title): if window_title: hwnd = win32gui.FindWindow(None, window_title) if hwnd: win32gui.SetForegroundWindow(hwnd) x, y, x1, y1 = win32gui.GetClientRect(hwnd) #get client size x, y = win32gui.ClientToScreen(hwnd, (x, y)) x += 200 y += 50 #cropping out top part x1, y1 = win32gui.ClientToScreen(hwnd, (x1 - x, y1 - y)) ## print(x, y, x1, y1) x1 -= 400 #cropping out right side y1 -= 580 #cropping out bottom part im = pyautogui.screenshot(region=(x, y, x1, y1)) im.save(r'D:\Studies\IDP\SCREENSHOT.png') else: print('Window not found!') else: im = pyautogui.screenshot() return im
def antiafk(): whnd = win32gui.FindWindow( None, 'World of Warcraft', ) print(whnd) time.sleep(2) win32gui.SetForegroundWindow(whnd) win32gui.SetActiveWindow(whnd) keyboard = Controller() keyboard.press(Key.esc) keyboard.release(Key.esc) keyboard.press(Key.space) keyboard.release(Key.space) time.sleep(1) keyboard.press(Key.enter) keyboard.release(Key.enter) time.sleep(1) keyboard.press('/') keyboard.release('/') time.sleep(1) keyboard.press('d') keyboard.release('d') time.sleep(1) keyboard.press('a') keyboard.release('a') time.sleep(1) keyboard.press('n') keyboard.release('n') time.sleep(1) keyboard.press('c') keyboard.release('c') time.sleep(1) keyboard.press('e') keyboard.release('e') time.sleep(1) keyboard.press(Key.enter) keyboard.release(Key.enter)
def screenshot_by_pid(self, procid, maximized=False): """ Method to take the screenshot by PID, this makes the magic works """ self.__HWND = None def getHwnd(hwnd, procid): if procid in win32process.GetWindowThreadProcessId(hwnd): if win32gui.IsWindowVisible(hwnd): self.__HWND = hwnd win32gui.EnumWindows(getHwnd, procid) if self.__HWND != None: win32gui.SetForegroundWindow(self.__HWND) x, y, x1, y1 = win32gui.GetClientRect(self.__HWND) if maximized == False: # Fix only for not maximized window x, y = win32gui.ClientToScreen(self.__HWND, (x, y)) x1, y1 = win32gui.ClientToScreen(self.__HWND, (x1 - x, y1 - y)) im = pyautogui.screenshot(region=(x, y, x1, y1)) return im else: printc("Error taking screenshot", "error")
def __init_window(self): # 画面サイズの取得 screen_x, screen_y = pyautogui.size() # win32guiを使ってウインドウタイトルを探す # Windowのハンドル取得('クラス名','タイトルの一部')で検索クラスがわからなかったらNoneにする parent_handle = win32gui.FindWindow(None, "Mine2000") # ハンドルIDが取れなかったら、mine2000を起動する if parent_handle == 0: cmd = 'C:\Program Files (x86)\mine2000 project\mine2000 ver2.2.1\mine2000.exe' subprocess.Popen(cmd, shell=True) time.sleep(1) parent_handle = win32gui.FindWindow(None, "Mine2000") if parent_handle == 0: sys.exit() # ハンドルが取れたら、ウインドウの左上と右下の座標取得と画面のアクティブ化 if parent_handle > 0: w0, h0, w1, h1 = win32gui.GetWindowRect(parent_handle) apw_x = w1 - w0 apw_y = h1 - h0 # ウィンドウをアクティブに持ってくる win32gui.SetForegroundWindow(parent_handle) time.sleep(0.5) # ウィンドウを画面中央に持ってくる x_pos = int((screen_x - apw_x) / 2) y_pos = int((screen_y - apw_y) / 2) win32gui.MoveWindow(parent_handle, x_pos, y_pos, apw_x, apw_y, True) # スクリーンショット用に座標を調整する return (w0 + 4, h0 + 79, w1 - 4, h1) raise Exception
def callback(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime): try: global lastTime global sofId #minimise a minimised window = bad? global sofMini global sofFull global resizeDone global origResDesktop global origResSof #if event == win32con.EVENT_OBJECT_FOCUS: fgWindow = win32gui.GetForegroundWindow() if fgWindow != sofId: t = threading.Timer(0.1, fgNotSoF) t.start() elif fgWindow == sofId: print(time.time()) #reduce file reads if resizeDone == 0: origres = getSoFRes() #we have focus of sof if origres != getLiveDesktop(): #LALT bug theory: #window must be minimised, desktop resized. then maximised #else bug happens win32gui.ShowWindow(sofId, win32con.SW_MINIMIZE) resizeDesktop(origres, 1) win32gui.MoveWindow(sofId, 0, 0, origres[0], origres[1], False) win32gui.ShowWindow(sofId, win32con.SW_MAXIMIZE) win32gui.SetForegroundWindow(sofId) resizeDone = 1 except KeyboardInterrupt: sys.exit(1)
def topWindow(self): self.Key_event(VK_CODE['ctrl']) win32gui.SetForegroundWindow(self.para_hld) # show window
def open_deezer(): win32gui.ShowWindow(DeezerWindow, 3) win32gui.SetForegroundWindow(DeezerWindow) mouse.move(100, 100) mouse.click()
import traceback from tkinter import * def imageThere(images): img = d.get_latest_frame() img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) for i in images: res = cv2.matchTemplate(img_gray, i[0], cv2.TM_CCOEFF_NORMED) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) if max_val >= 0.8: return i[1] return None HWND = win32gui.FindWindow(None, 'Fable 3') 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_REGION = (x, y, x1, y1) REGION = (x, int(y1*0.7), x1, y1) print(""" ---------------------------------------------------- beep beep... showing image of what will be searched. """) window = Tk() window.overrideredirect(True) window.wm_attributes("-topmost", True)
def save(self, name): self.model.save(name) ##IMAGE CAPTURE AND GAME RESET### def process_img(original_img): process_img = cv2.cvtColor(original_img, cv2.COLOR_BGR2GRAY) process_img = cv2.Canny(process_img, threshold1=200, threshold2=300) return process_img names = win32gui.GetWindowText(win32gui.GetForegroundWindow()) hwnd = win32gui.FindWindow(None, r'chrome://dino/ - Google Chrome') win32gui.SetForegroundWindow(hwnd) dimensions = win32gui.GetWindowRect(hwnd) x1, y1, x2, y2 = dimensions state_size = 50 * 13 action_size = 3 agent = DQNAgent(state_size, action_size) for e in range(n_episodes): reward = 0 ##Prep for Agent### # time1 = time.time() done = False while (done == False): im_L = ImageGrab.grab( (x1 + 20, y1 + 250, x2 * 2.8 / 3, y2 - (y2) * 7.17 / 12)) next_state = [] state = []
def foreground_window_by_hwnd(hwnd): win32gui.SetForegroundWindow(hwnd)
def set_foreground(self): # SET WINDOW TO FOREGROUND, MAXIMIZE WINDOW win32gui.SetForegroundWindow(self._handle) ShowWindow(self._handle, SW_MAXIMIZE)
def win32lib(): hwnd = win32.FindWindow(None, "Main@thinkorswim [build 1962]") print(hwnd) win32.SetForegroundWindow(hwnd) win32.ShowWindow(hwnd, 9)