def waitResponse(self, timeout=20): for i in range(0, timeout * 100): win32gui.PumpWaitingMessages() if self.waiting == False: break time.sleep(0.01)
def user_run(runInfo): process_start(runInfo) if os.name != 'nt': try: while True: time.sleep(0.01) if not __process: # 如果已经终止, 退出 break except KeyboardInterrupt: pass process_kill() else: sin_win_restart = runInfo.cmds['restartSignal'] sin_win_close = runInfo.cmds['closeSignal'] sin_win_restart_enterMan = runInfo.cmds['maintenanceSignal'] hooker.init(close_signalnum=sin_win_close, restartsignalnum=sin_win_restart, closecall=process_kill, restartcall=process_restart, ctrlccall=process_ctrlc, restartToManCall=lambda: process_restart(True), restartToManSignalnum=sin_win_restart_enterMan) hwnd, wc = hooker.regHook() # Windows 消息循环 try: while __process: time.sleep(0.01) win32gui.PumpWaitingMessages() except KeyboardInterrupt: pass # 注意:需要向隐藏的窗体发送关闭消息 sendMessage.send(os.getpid(), win32con.WM_MOUSEMOVE) hooker.destroy()
def block(self, keyboard=True, mouse=True): if mouse: self.hm.MouseAll = self.OnMouseEvent self.hm.HookMouse() if keyboard: self.hm.KeyAll = self.OnKeyboardEvent self.hm.HookKeyboard() win32gui.PumpWaitingMessages()
def draw_loop(_queue: queue.Queue): while True: try: canvas = _queue.get(block=True, timeout=0.02) except queue.Empty: pass else: canvas.initial_draw() finally: win32gui.PumpWaitingMessages()
def test(): # Required pywin32 import win32gui import win32con import time def on_create(hwnd): def test_callback(hwnd, msg, wParam, lParam): if msg == win32con.WM_KEYDOWN: print('Subclased OnKeyDown') return 0 return None setup(test_callback) # Original: http://kb.worldviz.com/articles/791 def OnKeyDown(hwnd, msg, wp, lp): print('Original OnKeyDown') def OnClose(hwnd, msg, wparam, lparam): """Destroy window when it is closed by user""" win32gui.DestroyWindow(hwnd) def OnDestroy(hwnd, msg, wparam, lparam): """Quit application when window is destroyed""" win32gui.PostQuitMessage(0) #Define message map for window wndproc = { win32con.WM_KEYDOWN: OnKeyDown, win32con.WM_CLOSE: OnClose, win32con.WM_DESTROY: OnDestroy } def CreateWindow(title, message_map, (l, t, r, b)): """Create a window with defined title, message map, and rectangle""" wc = win32gui.WNDCLASS() wc.lpszClassName = 'test_win32gui_1' wc.style = win32con.CS_GLOBALCLASS | win32con.CS_VREDRAW | win32con.CS_HREDRAW wc.hbrBackground = win32con.COLOR_WINDOW + 1 wc.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) wc.lpfnWndProc = message_map win32gui.RegisterClass(wc) hwnd = win32gui.CreateWindow( wc.lpszClassName, title, win32con.WS_CAPTION | win32con.WS_VISIBLE | win32con.WS_SYSMENU, l, t, r, b, 0, 0, 0, None) on_create(hwnd) while win32gui.PumpWaitingMessages() == 0: time.sleep(0.01) win32gui.UnregisterClass(wc.lpszClassName, None)
def Term(self): # The Window is dieing! We *must* kill it and wait for it to finish # else bad things happen once the main thread dies before us! if self.running: self.progress.request_stop() i = 0 while self.running: win32gui.PumpWaitingMessages(0, -1) if i % 100 == 0: print "Still waiting for async process to finish..." time.sleep(0.01) i += 1 return True