def show_menu(self): self.menu = win32gui.CreatePopupMenu() self.create_menu(self.menu, self.menu_options) pos = win32gui.GetCursorPos() win32gui.SetForegroundWindow(self.hwnd) win32gui.TrackPopupMenu(self.menu, win32con.TPM_LEFTALIGN, pos[0], pos[1], 0, self.hwnd, None) win32gui.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0)
def _notify(klass, hwnd, msg, wparam, lparam): self = klass._instance[hwnd] if lparam == win32con.WM_LBUTTONDBLCLK: menu = self.get_popup() wid = win32gui.GetMenuDefaultItem(menu, 0, 0) win32gui.PostMessage(hwnd, win32con.WM_COMMAND, wid, 0) elif lparam == win32con.WM_RBUTTONUP: menu = self.get_popup() pos = win32gui.GetCursorPos() win32gui.SetForegroundWindow(hwnd) win32gui.TrackPopupMenu( menu, win32con.TPM_LEFTALIGN, pos[0], pos[1], 0, hwnd, None) win32gui.PostMessage(hwnd, win32con.WM_NULL, 0, 0) elif lparam == win32con.WM_LBUTTONUP: pass return True
def dbclick(hwnd): ''' 模拟鼠标左键双击 :param hwnd: 要单击的控件、窗体句柄 :return: ''' win32gui.PostMessage(hwnd, win32con.WM_LBUTTONDBLCLK, None, None) time.sleep(.2)
def show_menu(self): if self.MENU is None: self.create_menu() position = win32gui.GetCursorPos() win32gui.SetForegroundWindow(self.HWND) win32gui.TrackPopupMenu(self.MENU, win32con.TPM_LEFTALIGN, position[0], position[1], 0, self.HWND, None) win32gui.PostMessage(self.HWND, win32con.WM_NULL, None, None)
def get_windows(windowsname, command): handle = win32gui.FindWindow(None, windowsname) win32gui.SetForegroundWindow(handle) win32gui.PostMessage(handle, 0, 0, 0) win32api.keybd_event(command, 0, 0, 0) win32api.keybd_event(13, 0, 0, 0)
def type_char(handle, char, method): if method == 'shell': SHELL.SendKeys(char) elif method == 'post': win32gui.PostMessage(handle, win32con.WM_CHAR, ord(char), 0) elif method == 'send': win32api.SendMessage(handle, win32con.WM_SETTEXT, 0, char) time.sleep(0.001)
def close_window(window="TxBENCH - New project"): """关闭窗口 Keyword Arguments: window {str} -- 窗口名称 (default: {"TxBENCH - New project"}) """ win32gui.PostMessage(win32gui.FindWindow(0, window), win32con.WM_CLOSE, 0, 0)
def find_window(cla_name, win_t): win32gui.EnumWindows(foo, 0) for hwnd in __all_ui__: if cla_name == win32gui.GetClassName( hwnd) and win_t == win32gui.GetWindowText(hwnd).decode( 'GB2312'): win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0) break
def jump_window(self): """ 使游戏窗口到最顶层 :return: """ win32gui.SetForegroundWindow(self.hwnd) win32gui.PostMessage(self.hwnd, win32con.WM_SYSCOMMAND, win32con.SC_RESTORE, 0)
def CloseWindow(hwnd): """ 关闭窗口,避免bug,休眠一下 :param hwnd: :return: """ time.sleep(0.001) win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
def LOG(msg, color = None): if color: msg = '<span style="color:%s">%s</span>' % (color, msg) msg = '<b>' + msg + '</b>' msg = '<span style="color:#AAAAAA">' + time.strftime("%H:%M:%S ") + '</span>' + msg buf_ptr = libc.malloc(len(msg)+1) libc.strcpy(buf_ptr, msg) win32gui.PostMessage(HWND, win32con.WM_USER, LOG_MESSAGE, buf_ptr)
def close(): set_status(False) handle = win32gui.GetForegroundWindow() try: win32gui.PostMessage(handle,win32con.WM_CLOSE,0,0) set_status(True) except Exception: pass
def crashwindow(): top_windows = [] win32gui.EnumWindows(windowEnumerationHandler, top_windows) for i in top_windows: if "BATTLEGROUNDS Crash Reporter" in i[1]: win32gui.PostMessage(i[0],win32con.WM_CLOSE,0,0) return True return False
def pywin32_ctrl_q_cb(self, hwnd, extra): window_title = win32gui.GetWindowText(hwnd) for name in self.obs_window_name: if name in window_title: win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0) self.callback_ret = True print('Found [{}] for closing.'.format(name)) break
def __close__(self, hwnd, dummy): """ EnumWindows callback - sends WM_CLOSE to any window owned by this process. """ TId, PId = win32process.GetWindowThreadProcessId(hwnd) if PId == self.PId: win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
def ctrl_click(self, x, y): """Clicks at pixel x, y while simulating the CTRL button to be down.""" x += window.x y += window.y lParam = win32api.MAKELONG(x, y) while (win32api.GetKeyState(wcon.VK_CONTROL) < 0 or win32api.GetKeyState(wcon.VK_SHIFT) < 0 or win32api.GetKeyState(wcon.VK_MENU) < 0): time.sleep(0.005) win32gui.PostMessage(window.id, wcon.WM_KEYDOWN, wcon.VK_CONTROL, 0) win32gui.PostMessage(window.id, wcon.WM_LBUTTONDOWN, wcon.MK_LBUTTON, lParam) win32gui.PostMessage(window.id, wcon.WM_LBUTTONUP, wcon.MK_LBUTTON, lParam) win32gui.PostMessage(window.id, wcon.WM_KEYUP, wcon.VK_CONTROL, 0) time.sleep(userset.MEDIUM_SLEEP)
def CloseWindow(self): result = 0 if(self.IsOver()): result = int(self.GetNumberSelected()) time.sleep(1) win32gui.PostMessage(self.handle, win32con.WM_CLOSE, 0, 0) time.sleep(1) return result
def choose_device(self, event=None): print 1111111, self.tray win32gui.PostMessage(self.tray.hwnd, win32con.WM_CLOSE, 0, 0) # self.tray.destroy() # self._root.deiconify() d = SelectDeviceDialog(self._root) self.create_tray() print d.result
def _InputHandler(hwnd, msg, wparam, lparam): global _lastCursorAtDesktop, _cursorAtDesktop, _lastMousePos # 避免 Windows 10 自动发送鼠标离开的消息,从而无法触发浏览器的 hover if msg == win32con.WM_MOUSELEAVE: if _cursorAtDesktop: return False elif msg == 0xFF: dataSize = ctypes.c_ulong(0) ctypes.windll.user32.GetRawInputData(lparam, 0x10000003, None, ctypes.byref(dataSize), ctypes.sizeof(RAWINPUTHEADER)) dataBuf = ctypes.create_string_buffer(dataSize.value) ctypes.windll.user32.GetRawInputData(lparam, 0x10000003, dataBuf, ctypes.byref(dataSize), ctypes.sizeof(RAWINPUTHEADER)) inputData = ctypes.cast(dataBuf, ctypes.POINTER(RAWINPUT)) if inputData.contents.header.dwType == 0: mouse = inputData.contents.data.mouse lastMousePos = win32gui.GetCursorPos() # 获得鼠标当前所在的屏幕坐标 # 鼠标移动的相对坐标(暂时没啥用) '''if mouse.usFlags & 1: _lastMousePos[0] = int((mouse.lLastX / 65535.0) * screenSize[0]) _lastMousePos[1] = int((mouse.lLastY / 65535.0) * screenSize[1]) else: _lastMousePos[0] += mouse.lLastX _lastMousePos[1] += mouse.lLastY''' # 获得鼠标当前指向的窗口句柄 cursorAt = win32gui.WindowFromPoint( (lastMousePos[0], lastMousePos[1])) # 检测到鼠标指向的是桌面(桌面显示图标时是 _syslistview,隐藏图标时是 _defview) _cursorAtDesktop = (cursorAt == _defview or cursorAt == _syslistview) if _cursorAtDesktop: win32gui.PostMessage(_legacy, win32con.WM_MOUSEMOVE, 0, (lastMousePos[1] << 16) | lastMousePos[0]) elif _lastCursorAtDesktop and not _cursorAtDesktop: win32gui.PostMessage(hwnd, win32con.WM_MOUSELEAVE, 0, 0) _lastCursorAtDesktop = _cursorAtDesktop return win32gui.CallWindowProc(_wndproc0, hwnd, msg, wparam, lparam)
def click(self, x, y, button="left", fast=False, ctrl=False): """Click at pixel xy.""" x += window.x y += window.y lParam = win32api.MAKELONG(x, y) # MOUSEMOVE event is required for game to register clicks correctly win32gui.PostMessage(window.id, wcon.WM_MOUSEMOVE, 0, lParam) while (win32api.GetKeyState(wcon.VK_CONTROL) < 0 or win32api.GetKeyState(wcon.VK_SHIFT) < 0 or win32api.GetKeyState(wcon.VK_MENU) < 0): time.sleep(0.005) if ctrl: win32gui.PostMessage(window.id, wcon.WM_KEYDOWN, wcon.VK_CONTROL, 0) if (button == "left"): win32gui.PostMessage(window.id, wcon.WM_LBUTTONDOWN, wcon.MK_LBUTTON, lParam) win32gui.PostMessage(window.id, wcon.WM_LBUTTONUP, wcon.MK_LBUTTON, lParam) else: win32gui.PostMessage(window.id, wcon.WM_RBUTTONDOWN, wcon.MK_RBUTTON, lParam) win32gui.PostMessage(window.id, wcon.WM_RBUTTONUP, wcon.MK_RBUTTON, lParam) if ctrl: win32gui.PostMessage(window.id, wcon.WM_KEYUP, wcon.VK_CONTROL, 0) # Sleep lower than 0.1 might cause issues when clicking in succession if type(fast) == float: time.sleep(fast) elif fast: time.sleep(0.11) else: time.sleep(userset.MEDIUM_SLEEP)
def send_string(self, string): """Send one or multiple characters to the window.""" if type(string) == float: # Remove decimal string = str(int(string)) for c in str(string): while (win32api.GetKeyState(wcon.VK_CONTROL) < 0 or win32api.GetKeyState(wcon.VK_SHIFT) < 0 or win32api.GetKeyState(wcon.VK_MENU) < 0): time.sleep(0.005) if c.isdigit(): # Digits only require KEY_UP event. win32gui.PostMessage(window.id, wcon.WM_KEYUP, ord(c.upper()), 0) # time.sleep(0.03) # This can probably be removed continue win32gui.PostMessage(window.id, wcon.WM_KEYDOWN, ord(c.upper()), 0) time.sleep(userset.SHORT_SLEEP) # This can probably be removed win32gui.PostMessage(window.id, wcon.WM_KEYUP, ord(c.upper()), 0) time.sleep(userset.SHORT_SLEEP)
def show_menu(self): menu = win32gui.CreatePopupMenu() self.create_menu(menu, self.menu_options) pos = win32gui.GetCursorPos() # See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/menus_0hdi.asp win32gui.SetForegroundWindow(self.hwnd) win32gui.TrackPopupMenu(menu, win32con.TPM_LEFTALIGN, pos[0], pos[1], 0, self.hwnd, None) win32gui.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0)
def terminate(): global flag while (1): hwnd = win32gui.FindWindow(None, 'Password') if hwnd != 0: win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0) break if flag == True: break
def sendMsg(room, msg): try: hwnd1 = win32gui.FindWindow(None, room) hwnd2 = win32gui.FindWindowEx(hwnd1, 0, "RICHEDIT50W", "") win32gui.SendMessage(hwnd2, 0x000c, 0, msg) win32gui.PostMessage(hwnd2, 0x0100, 0xD, 0x1C001) print("LOG: [" + room + "] :" + msg) except: print("ERR: [" + room + "] :" + msg)
def close_window(window_name): try: window = win32gui.FindWindow(None, window_name) if window: error_messages.add("{} window found! Closing it...".format(window_name)) win32gui.PostMessage(window, win32con.WM_CLOSE, 0, 0) except Exception as e: utils.case_logger.error("Failed to close error window ({}): {}".format(window_name, str(e))) utils.case_logger.error("Traceback: {}".format(traceback.format_exc()))
def Complement_data(): try: time.sleep(3) if win32gui.FindWindowEx(win32gui.FindWindow('#32770','TdxW'),None,'Button','是(&Y)') != 0: win32gui.PostMessage(win32gui.FindWindowEx(win32gui.FindWindow('#32770','TdxW'),None,'Button','是(&Y)'),win32con.BM_CLICK,0,0) time.sleep(5) print("补数据") except Exception as e: ReadEBK.wx_msg(corp_id, secret,agentid,sys._getframe().f_code.co_name+'\t'+str(e))
def post_move(hwnd, toX, toY): win32api.SetCursorPos((toX, toY)) c_x, c_y = win32gui.ScreenToClient(hwnd, (toX, toY)) keystate = 0 if win32api.GetAsyncKeyState(win32con.VK_CONTROL) & 0x8000 != 0: keystate = keystate | win32con.MK_CONTROL if win32api.GetAsyncKeyState(win32con.VK_SHIFT) & 0x8000 != 0: keystate = keystate | win32con.MK_SHIFT win32gui.PostMessage(hwnd, win32con.WM_MOUSEMOVE, keystate, (c_y << 16) | c_x)
def close(hwnd, idx): typeName = setting.types[idx] log.logger.debug("close->\t%s\t%s\t%s", hex(hwnd), idx, typeName) if 0 != hwnd: log.logger.info("close<-\t%s\t%s", hex(hwnd), win32gui.GetWindowText(hwnd)) try: win32gui.PostMessage( hwnd, win32con.WM_KEYDOWN, win32con.VK_ESCAPE, 0) win32gui.PostMessage( hwnd, win32con.WM_KEYUP, win32con.VK_ESCAPE, 0) # time.sleep(0.05) except Exception as e: log.logger.error("close.fail\t%s\t%s\t%s", typeName, e.args) else: log.logger.info("close\t%s", typeName)
def close_window_by_hwnd(hwnd,window_manage_delay_time=window_manage_delay_time,window_manage_confirm_num=window_manage_confirm_num): win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0) for i in range(window_manage_confirm_num): ret = not bool(is_window_by_hwnd(hwnd)) if ret: return ret else: time.sleep(window_manage_delay_time) return ret
def killall(): while True: trash = win32gui.FindWindow(None, flashplayertitle) if trash: print('process found: ', trash) win32gui.PostMessage(trash, win32con.WM_CLOSE, 0, 0) sleep(1) else: print('no more process!') break