def _run(self, quiet: bool = False): message_map = {con32.WM_DESTROY: self._onDestroy, } # register the window class wc = gui32.WNDCLASS() self._hinst = wc.hInstance = gui32.GetModuleHandle(None) wc.lpszClassName = 'PythonBalloontip' wc.lpfnWndProc = message_map self._classAtom = gui32.RegisterClass(wc) # create the window style = con32.WS_OVERLAPPED | con32.WS_SYSMENU self._hwnd = gui32.CreateWindow(self._classAtom, 'Taskbar', style, 0, 0, con32.CW_USEDEFAULT, con32.CW_USEDEFAULT, 0, 0, self._hinst, None) gui32.UpdateWindow(self._hwnd) try: timeout = float(self.timeout) if timeout <= 0: raise ValueError( "the 'timeout' parameter must be greater than 0") except Exception as e: self._showError(e, 'exit') return self._infoFlags = gui32.NIIF_NOSOUND if quiet else 0 if not self._getIcon(): return flags = gui32.NIF_ICON | gui32.NIF_MESSAGE | gui32.NIF_TIP | gui32.NIF_INFO nid = (self._hwnd, 0, flags, con32.WM_USER + 20, self._hicon, "Balloontip", self.msg, 200, self.title, self._infoFlags) gui32.Shell_NotifyIcon(gui32.NIM_ADD, nid) sleep(timeout) gui32.DestroyWindow(self._hwnd) gui32.UnregisterClass(self._classAtom, self._hinst)
def TestSetWorldTransform(): 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.lpfnWndProc = wndproc_1 class_atom = win32gui.RegisterClass(wc) hwnd = win32gui.CreateWindow( wc.lpszClassName, "Spin the Lobster!", win32con.WS_CAPTION | win32con.WS_VISIBLE, 100, 100, 900, 900, 0, 0, 0, None, ) for x in range(500): win32gui.InvalidateRect(hwnd, None, True) win32gui.PumpWaitingMessages() time.sleep(0.01) win32gui.DestroyWindow(hwnd) win32gui.UnregisterClass(wc.lpszClassName, None)
def go(self): wndproc = { win32con.WM_PAINT: self.OnPaint, win32con.WM_CLOSE: self.OnClose, win32con.WM_CREATE: self.OnCreate, win32con.WM_DRAWCLIPBOARD: self.OnDrawClipboard, win32con.WM_CHANGECBCHAIN: self.OnChangeCBChain, } wc = win32gui.WNDCLASS() wc.lpszClassName = "test_win32clipboard_bmp" wc.style = win32con.CS_GLOBALCLASS | win32con.CS_VREDRAW | win32con.CS_HREDRAW wc.hbrBackground = win32con.COLOR_WINDOW + 1 wc.lpfnWndProc = wndproc class_atom = win32gui.RegisterClass(wc) hwnd = win32gui.CreateWindowEx( 0, class_atom, "ClipboardViewer", win32con.WS_CAPTION | win32con.WS_VISIBLE | win32con.WS_THICKFRAME | win32con.WS_SYSMENU, 100, 100, 900, 900, 0, 0, 0, None, ) win32clipboard.SetClipboardViewer(hwnd) win32gui.PumpMessages() win32gui.UnregisterClass(class_atom, None)
def start(self): print("[log] start") wndproc = { # win32con.WM_PAINT: self.OnPaint, win32con.WM_CLOSE: self.OnClose, win32con.WM_CREATE: self.OnCreate, win32con.WM_DRAWCLIPBOARD: self.OnDrawClipboard, # copy # win32con.WM_CHANGECBCHAIN: self.OnChangeCBChain, } wc = win32gui.WNDCLASS() wc.lpszClassName = 'app_clipboard' wc.style = win32con.CS_GLOBALCLASS | win32con.CS_VREDRAW | win32con.CS_HREDRAW wc.hbrBackground = win32con.COLOR_WINDOW + 1 wc.lpfnWndProc = wndproc class_atom = win32gui.RegisterClass(wc) hwnd = win32gui.CreateWindowEx( 0, class_atom, u'分享链接', win32con.WS_CAPTION | win32con.WS_VISIBLE | win32con.WS_THICKFRAME | win32con.WS_SYSMENU, 100, 100, 600, 400, 0, 0, 0, None) win32clipboard.SetClipboardViewer(hwnd) win32gui.PumpMessages() win32gui.UnregisterClass(class_atom, None) win32clipboard.OpenClipboard() win32clipboard.EmptyClipboard() win32clipboard.CloseClipboard()
def destroy(self): """ GUIの破棄と, 破棄時コールバックの実行. @note ウィンドウを作成したスレッドと同じスレッドから呼び出すこと. """ if not(self._can_destroy): return self._can_destroy = False self.stop() if callable(self._callback_on_destroy): self._callback_on_destroy() try: self._trayicon.destroy() except Exception as e: # 他の終了処理もあるのでとりあえず続行. pass try: win32gui.DestroyWindow(self._hwnd) except Exception as e: # 他の終了処理もあるのでとりあえず続行. pass try: win32gui.UnregisterClass(self._classatom, self._hinst) except Exception as e: # 他の終了処理もあるのでとりあえず続行. pass
def TestGradientFill(): wc = win32gui.WNDCLASS() wc.lpszClassName = "test_win32gui_2" wc.style = win32con.CS_GLOBALCLASS | win32con.CS_VREDRAW | win32con.CS_HREDRAW wc.hbrBackground = win32con.COLOR_WINDOW + 1 wc.lpfnWndProc = wndproc_2 class_atom = win32gui.RegisterClass(wc) hwnd = win32gui.CreateWindowEx( 0, class_atom, "Kaleidoscope", win32con.WS_CAPTION | win32con.WS_VISIBLE | win32con.WS_THICKFRAME | win32con.WS_SYSMENU, 100, 100, 900, 900, 0, 0, 0, None, ) s = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, s | win32con.WS_EX_LAYERED) win32gui.SetLayeredWindowAttributes(hwnd, 0, 175, win32con.LWA_ALPHA) for x in range(30): win32gui.InvalidateRect(hwnd, None, True) win32gui.PumpWaitingMessages() time.sleep(0.3) win32gui.DestroyWindow(hwnd) win32gui.UnregisterClass(class_atom, None)
def TestDeviceNotifications(dir_names): wc = win32gui.WNDCLASS() wc.lpszClassName = 'test_devicenotify' wc.style = win32con.CS_GLOBALCLASS | win32con.CS_VREDRAW | win32con.CS_HREDRAW wc.hbrBackground = win32con.COLOR_WINDOW + 1 wc.lpfnWndProc = {win32con.WM_DEVICECHANGE: OnDeviceChange} class_atom = win32gui.RegisterClass(wc) hwnd = win32gui.CreateWindow( wc.lpszClassName, 'Testing some devices', # no need for it to be visible. win32con.WS_CAPTION, 100, 100, 900, 900, 0, 0, 0, None) hdevs = [] # Watch for all USB device notifications filter = win32gui_struct.PackDEV_BROADCAST_DEVICEINTERFACE( GUID_DEVINTERFACE_USB_DEVICE) hdev = win32gui.RegisterDeviceNotification( hwnd, filter, win32con.DEVICE_NOTIFY_WINDOW_HANDLE) hdevs.append(hdev) # and create handles for all specified directories for d in dir_names: hdir = win32file.CreateFile( d, winnt.FILE_LIST_DIRECTORY, winnt.FILE_SHARE_READ | winnt.FILE_SHARE_WRITE | winnt.FILE_SHARE_DELETE, None, # security attributes win32con.OPEN_EXISTING, win32con.FILE_FLAG_BACKUP_SEMANTICS | # required privileges: SE_BACKUP_NAME and SE_RESTORE_NAME. win32con.FILE_FLAG_OVERLAPPED, None) filter = win32gui_struct.PackDEV_BROADCAST_HANDLE(hdir) hdev = win32gui.RegisterDeviceNotification( hwnd, filter, win32con.DEVICE_NOTIFY_WINDOW_HANDLE) hdevs.append(hdev) # now start a message pump and wait for messages to be delivered. print "Watching", len(hdevs), "handles - press Ctrl+C to terminate, or" print "add and remove some USB devices..." if not dir_names: print "(Note you can also pass paths to watch on the command-line - eg," print "pass the root of an inserted USB stick to see events specific to" print "that volume)" while 1: win32gui.PumpWaitingMessages() time.sleep(0.01) win32gui.DestroyWindow(hwnd) win32gui.UnregisterClass(wc.lpszClassName, None)
def QuitApplication(parent, windowID, msg, wparam, lparam): cefpython.QuitMessageLoop() win32gui.DestroyWindow(windowID) #browser = cefpython.GetBrowserByWindowHandle(windowID) #browser.CloseBrowser() className = cefwindow.GetWindowClassName(windowID) win32gui.UnregisterClass(className, None)
def __close_window(window_handle, window_class): """ Unregisters and destroys a window """ # https://archive.is/BWQv6 win32gui.DestroyWindow(window_handle) # https://archive.is/53x2A win32gui.UnregisterClass(window_class.lpszClassName, None)
def cleanup(self): self.stop_win32_session_events() if self.hwnd: try: win32gui.DestroyWindow(self.hwnd) except Exception, e: log.warn("error during win32 events cleanup: %s", e) self.hwnd = None win32gui.UnregisterClass(self.wc.lpszClassName, None)
def RUN_CODE(line, activeJupiter): active = ACTIVE if activeJupiter == True else DEACTIVE connector = JupiterListener() message = CODE_TYPE + line + ";" + active SendMessageToJupiter(message) win32gui.DestroyWindow(connector.hwnd) win32gui.UnregisterClass(connector.self_classAtom, connector.self_hinst) # win32gui.PumpMessages() pass
def RUN_DEBUG(line, activeJupiter): active = ACTIVE if activeJupiter == True else DEACTIVE connector = JupiterListener() message = DEBUG_TYPE + line + ";" + active SendMessageToJupiter(message) win32gui.DestroyWindow(connector.hwnd) win32gui.UnregisterClass(connector.self_classAtom, connector.self_hinst) return trimMsg pass
def on_command(self, hwnd, msg, wparam, lparam): wid = win32api.LOWORD(wparam) if wid == 1024: WindowsNotifier.show_game_window() elif wid == 1025: win32gui.DestroyWindow(self.hwnd) win32gui.UnregisterClass(self.wc.lpszClassName, None) self.alive = False else: print(f"Unknown command: {wid}")
def _remove_window(self): """Destroy window and free reserved resources.""" if self._window: win32gui.DestroyWindow(self._window) win32gui.UnregisterClass(self.__class__.__name__, None) self._window = None if self._screenshot: win32gui.DeleteObject(self._screenshot.GetHandle()) self._screenshot = None
def OnDestroy(self, hwnd, msg, wparam, lparam): global logfile logfile.close() win32gui.DestroyWindow(self.hwnd) try: win32gui.UnregisterClass(self.cAtom, self.hinst) except: pass win32gui.PostQuitMessage(0)
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 __stop__(self): while True: try: win32gui.UnregisterClass(self.classAtom, self.hinst) except win32gui.error as e: if e.winerror == 1412: # "Class still has open windows." # So let's wait for the notification to go away. eg.plugins.EventGhost.Wait(1.0) continue else: raise e # No exception, so exit the loop. break del self.classAtom
def cleanup(self): log("Win32EventListener.cleanup()") self.event_callback = {} self.stop_win32_session_events() if self.hwnd: try: win32gui.DestroyWindow(self.hwnd) except Exception as e: log.error("Error during cleanup of event window instance:") log.error(" %s", e) self.hwnd = None try: win32gui.UnregisterClass(self.wc.lpszClassName, None) except Exception as e: log.error("Error during cleanup of event window class:") log.error(" %s", e)
def _on_command(self, hwnd, msg, wparam, lparam): id = win32api.LOWORD(wparam) if id == self.MENU_ITEM_ID_ABOUT: if self.on_about: self.on_about(self) elif id == self.MENU_ITEM_ID_EXIT: if self.on_quit: self.on_quit(self) win32gui.DestroyWindow(self.hwnd) win32gui.UnregisterClass(self.class_atom, self._window_class.hInstance) elif id in self.custom_menu_items.keys(): if callable(self.custom_menu_items[id]['func']): try: self.custom_menu_items[id]['func'](self) except Exception, e: logging.error("Error executing menu item func: %s" % self.custom_menu_items[id]['text']) logging.error(e)
def __init__(self, server_fn): global process_handler self.window_class_name = Config.WINDOW_CLASS_NAME # print("len()", len(self.window_class_name), self.window_class_name) # Register the Window class. window_class = win32gui.WNDCLASS() hInst = window_class.hInstance = win32gui.GetModuleHandle(None) window_class.lpszClassName = self.window_class_name window_class.lpfnWndProc = self.pyWndProcedure # could also specify a wndproc. classAtom = win32gui.RegisterClass(window_class) # Create the Window. style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU self.hWnd = win32gui.CreateWindow(classAtom, self.window_class_name, style, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, hInst, None) # # BEGIN actual setup # t = threading.Thread(target=server_fn) t.setDaemon(True) t.start() # set_inited(pipeName, bytes(self.window_class_name.encode('utf-8'))) # # END actual setup # # loop win32gui.UpdateWindow(self.hWnd) postQuitExitCode = win32gui.PumpMessages() # TODO: before quit need to do following # and this has to done immediately after PumpMessages is over # -> when we close console, then we may time-out before finishing timeout logger.debug("_app_starter: postQuitExitCode: %s" % postQuitExitCode) win32gui.DestroyWindow(self.hWnd) win32gui.UnregisterClass(window_class.lpszClassName, hInst) logger.debug("_app_starter: after after")
def OnDestroy(self, hwnd, msg, wparam, lparam): __LOG__.Trace( "OnDestroy Call +++++++++++++++++++++++++++++++++++++++++++++++++", (logging.CRITICAL)) #win32gui.DestroyWindow(self.hwnd) if len(g_BUS_QUEUE) > 0: __LOG__.Trace( "Not Destroy, remain Queue Data :[%d]" % (len(g_BUS_QUEUE)), logging.CRITICAL) return time.sleep(10) try: win32gui.UnregisterClass(self.cAtom, self.hinst) except: pass #global SHUTDOWN #if not SHUTDOWN : # User Close #{ if self.busTh: if self.busTh.isAlive(): self.busTh.kill() self.busTh.join() __LOG__.Trace( "BUS THREAD END +++++++++++++++++++++++++++++++++++++++++++++++++", (logging.CRITICAL)) if self.queueTh: if self.queueTh.isAlive(): self.queueTh.join() __LOG__.Trace( "QUEUE THREAD END +++++++++++++++++++++++++++++++++++++++++++++++++", (logging.CRITICAL)) #} __LOG__.Trace( "OnDestroy END +++++++++++++++++++++++++++++++++++++++++++++++++", (logging.CRITICAL)) win32gui.PostQuitMessage(0)
def CreateWindow(title, message_map, location): """Create a window with defined title, message map, and rectangle""" l, t, r, b = location wc = win32gui.WNDCLASS() wc.lpszClassName = TEST_CLASSNAME 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) win32gui.CreateWindow( wc.lpszClassName, title, win32con.WS_CAPTION | win32con.WS_VISIBLE | win32con.WS_SYSMENU, l, t, r, b, 0, 0, 0, None) while win32gui.PumpWaitingMessages() == 0: time.sleep(0.01) win32gui.UnregisterClass(wc.lpszClassName, None)
def _push(self, title, msg, icon_path, duration, threaded): if self._thread and threaded: self._thread.join() message_map = {win32con.WM_DESTROY: self.on_destroy} # register the window class self.wc = win32gui.WNDCLASS() self.hinst = self.wc.hInstance = win32api.GetModuleHandle(None) self.wc.lpszClassName = str("PythonTaskbar") # Must be a string self.wc.lpfnWndProc = message_map try: self.classAtom = win32gui.RegisterClass(self.wc) except Exception: pass style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU self.hwnd = win32gui.CreateWindow(self.classAtom, "Taskbar", style, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, self.hinst, None) win32gui.UpdateWindow(self.hwnd) if icon_path is not None: icon_path = os.path.realpath(icon_path) icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE try: hicon = win32gui.LoadImage(self.hinst, icon_path, win32gui.IMAGE_ICON, 0, 0, icon_flags) except Exception: hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) # Taskbar icon flags = win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP nid = (self.hwnd, 0, flags, win32con.WM_USER + 20, hicon, "Tooltip") win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid) win32gui.Shell_NotifyIcon( win32gui.NIM_MODIFY, (self.hwnd, 0, win32gui.NIF_INFO, win32con.WM_USER + 20, hicon, "Balloon Tooltip", msg, 200, title)) time.sleep(duration) win32gui.DestroyWindow(self.hwnd) win32gui.UnregisterClass(self.wc.lpszClassName, None) return None
def thread(): t = TrayMsg(hicon, LC, AC) className = t.className t.showMsg(title, msg) def thread2(): try: time.sleep(10) win32gui.PostMessage(t.hwnd, win32con.WM_CLOSE, 0, 0) time.sleep(0.5) except Exception as e: pass ReStartableThread(target=thread2).start() win32gui.PumpMessages() try: win32gui.UnregisterClass(className, 0) except Exception as e: pass
def OnCommand(self, hwnd, msg, wparam, lparam): id = win32api.LOWORD(wparam) if id == 1023: self.SwitchWindow() #win32gui.DestroyWindow(self.hwnd) #win32gui.UnregisterClass(self.wc.lpszClassName,None) #import win32gui_dialog #win32api.MessageBox(0,"hello",win32con.MB_OK) elif id == 1024: config.deiconify() elif id == 1025: nid = (self.hwnd, 0) win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid) root.destroy() win32gui.DestroyWindow(self.hwnd) win32gui.UnregisterClass(self.wc.lpszClassName, None) #win32gui.PostQuitMessage(0) # Terminate the app. else: print("Unknown command -", id)
def deactivate(): restore_data = vim.vars.get('fullscreen_restoredata') win32gui.DestroyWindow(restore_data.get('bkgnd_wnd')) win32gui.UnregisterClass(restore_data.get('class_atom'), None) top_wnd = win32gui.FindWindow('Vim', None) win32api.SetWindowLong(top_wnd, win32con.GWL_STYLE, restore_data.get('style')) win32api.SetWindowLong(top_wnd, win32con.GWL_EXSTYLE, restore_data.get('exstyle')) win32gui.SetWindowPlacement(top_wnd, restore_data.get('window_placement')) if restore_data.get('window_placement')[1] == win32con.SW_SHOWMAXIMIZED: win32api.SendMessage(top_wnd, win32con.WM_SYSCOMMAND, win32con.SC_RESTORE, 0) win32api.SendMessage(top_wnd, win32con.WM_SYSCOMMAND, win32con.SC_MAXIMIZE, 0) vim.vars.update({'fullscreen_restoredata': {}}) vim.vars.update({'fullscreen_active': 0})
def OnDestroy(self, hwnd, msg, wparam, lparam): __LOG__.Trace( "OnDestroy Call +++++++++++++++++++++++++++++++++++++++++++++++++" , (logging.CRITICAL) ) #win32gui.DestroyWindow(self.hwnd) try: win32gui.UnregisterClass(self.cAtom, self.hinst) except: pass global SHUTDOWN if not SHUTDOWN : # User Close if self.busTh : if self.busTh.isAlive(): self.busTh.kill() self.busTh.join() __LOG__.Trace( "BUS THREAD END +++++++++++++++++++++++++++++++++++++++++++++++++" , (logging.CRITICAL) ) __LOG__.Trace( "OnDestroy END +++++++++++++++++++++++++++++++++++++++++++++++++" , (logging.CRITICAL) ) win32gui.PostQuitMessage(0)
def _WindowsShutdownBlocker(title='Python script'): """ Block Windows shutdown when you do something important. """ from ctypes import CFUNCTYPE, c_bool, c_uint, c_void_p, c_wchar_p, windll import win32con import win32gui def WndProc(hWnd, message, wParam, lParam): if message == win32con.WM_QUERYENDSESSION: return False else: return win32gui.DefWindowProc(hWnd, message, wParam, lParam) CALLBACK = CFUNCTYPE(c_bool, c_void_p, c_uint, c_void_p, c_void_p) wc = win32gui.WNDCLASS() wc.style = win32con.CS_GLOBALCLASS | win32con.CS_VREDRAW | win32con.CS_HREDRAW wc.lpfnWndProc = CALLBACK(WndProc) wc.hbrBackground = win32con.COLOR_WINDOW + 1 wc.lpszClassName = "block_shutdown_class" win32gui.RegisterClass(wc) hwnd = win32gui.CreateWindow(wc.lpszClassName, title, win32con.WS_OVERLAPPEDWINDOW, 50, 50, 100, 100, 0, 0, win32gui.GetForegroundWindow(), None) win32gui.ShowWindow(hwnd, win32con.SW_HIDE) windll.user32.ShutdownBlockReasonCreate.argtypes = [c_void_p, c_wchar_p] windll.user32.ShutdownBlockReasonCreate.restype = c_bool windll.user32.ShutdownBlockReasonCreate( hwnd, "Important work in processing, don't shutdown :-(") yield windll.user32.ShutdownBlockReasonDestroy.argtypes = [c_void_p] windll.user32.ShutdownBlockReasonDestroy.restype = c_bool windll.user32.ShutdownBlockReasonDestroy(hwnd) win32gui.DestroyWindow(hwnd) win32gui.UnregisterClass(wc.lpszClassName, None)
def __init__(self): global process_handler self.window_class_name = Config.WINDOW_CLASS_NAME # Register the Window class. window_class = win32gui.WNDCLASS() hInst = window_class.hInstance = win32gui.GetModuleHandle(None) window_class.lpszClassName = self.window_class_name window_class.lpfnWndProc = self.pyWndProcedure # could also specify a wndproc. classAtom = win32gui.RegisterClass(window_class) # Create the Window. style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU self.hWnd = win32gui.CreateWindow(classAtom, self.window_class_name, style, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, hInst, None) # init activate restarter loop send_initial_restarter_signals() # loop win32gui.UpdateWindow(self.hWnd) postQuitExitCode = win32gui.PumpMessages() # TODO: before quit need to do following # and this has to done immediately after PumpMessages is over # -> when we close console, then we may time-out before finishing timeout # print("postQuitExitCode", postQuitExitCode) win32gui.DestroyWindow(self.hWnd) win32gui.UnregisterClass(window_class.lpszClassName, hInst)
def __init__(self, title, msg, timeout=2): message_map = { win32con.WM_DESTROY: self.OnDestroy, } # Register the Window class. wc = win32gui.WNDCLASS() self.destroyed = False hinst = wc.hInstance = GetModuleHandle(None) wc.lpszClassName = "PythonTaskbar" wc.lpfnWndProc = message_map # could also specify a wndproc. class_atom = win32gui.RegisterClass(wc) # Create the Window. style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU self.hwnd = win32gui.CreateWindow(class_atom, "Taskbar", style, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, hinst, None) win32gui.UpdateWindow(self.hwnd) icon_path_name = ospath.abspath( ospath.join(syspath[0], "balloontip.ico")) icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE try: hicon = win32gui.LoadImage(hinst, icon_path_name, win32con.IMAGE_ICON, 0, 0, icon_flags) except: hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) flags = win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP nid = (self.hwnd, 0, flags, win32con.WM_USER + 20, hicon, "tooltip") win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid) win32gui.Shell_NotifyIcon( win32gui.NIM_MODIFY, (self.hwnd, 0, win32gui.NIF_INFO, win32con.WM_USER + 20, hicon, "Balloon tooltip", msg, 200, title)) sleep(timeout) win32gui.DestroyWindow(self.hwnd) win32gui.UnregisterClass(class_atom, hinst) self.destroyed = True