def destroy(self, hwnd, msg, wparam, lparam): logger.info('on destroy') nid = (self.hwnd, 0) try: win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid) except Exception as e: logger.info('exception while destroy icon. ignored.', exc_info=1) win32gui.PostQuitMessage(0) # Terminate the app. # 注销窗口类 tryTimes = 5 for i in range(tryTimes): try: win32gui.UnregisterClass(self.window_class_name, 0) break except Exception as e: if not hasattr(e, '__getitem__') or e[0] == 1412: # (1412, 'UnregisterClass', '\xc0\xe0\xc8\xd4\xd3\xd0\xb4\xf2\xbf\xaa\xb5\xc4\xb4\xb0\xbf\xda\xa1\xa3') # 类仍有打开的窗口。 # 这个情况一般是因为消息传递和窗口关闭的异步延迟 pass else: logger.info('exception when UnregisterClass, ignored.', exc_info=1) time.sleep(0.1)
def execute_menu_option(self, id): menu_action = self.menu_actions_by_id[id] if menu_action == self.QUIT: win32gui.DestroyWindow(self.hwnd) win32gui.UnregisterClass(self.classAtom, self.window_class.hInstance) else: menu_action(self)
def execute_menu_option(self, id): #print id try: menu_action = self.menu_actions_by_id[id] if menu_action == self.QUIT: app = wx.PySimpleApp() retCode = wx.MessageBox("Are you sure you want to quit?", "Quit",wx.YES_NO | wx.ICON_QUESTION) #print retCode #print "2 to quit" if retCode == 2: win32gui.DestroyWindow(self.hwnd) win32gui.UnregisterClass(self.classAtom,None) else: pass else: menu_action(self,self.atr[id]) except Exception: if id == 'quit': win32gui.DestroyWindow(self.hwnd) win32gui.UnregisterClass(self.classAtom,None)
def __init__( self, icon, hover_text, menu_options, on_quit=None, default_menu_index=None, window_class_name=None, ): self.icon = icon self.hover_text = hover_text self.on_quit = on_quit menu_options = menu_options + (('Quit', None, self.QUIT), ) self._next_action_id = self.FIRST_ID self.menu_actions_by_id = set() self.menu_options = self._add_ids_to_menu_options(list(menu_options)) self.menu_actions_by_id = dict(self.menu_actions_by_id) del self._next_action_id self.default_menu_index = (default_menu_index or 0) self.window_class_name = window_class_name or "SysTrayIconPy" message_map = { win32gui.RegisterWindowMessage("TaskbarCreated"): self.restart, win32con.WM_DESTROY: self.destroy, win32con.WM_COMMAND: self.command, win32con.WM_USER + 20: self.notify, } # Register the Window class. window_class = win32gui.WNDCLASS() hinst = window_class.hInstance = win32gui.GetModuleHandle(None) window_class.lpszClassName = self.window_class_name window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW window_class.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) window_class.hbrBackground = win32con.COLOR_WINDOW window_class.lpfnWndProc = message_map # 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, window_class.lpszClassName, style, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, hinst, None) win32gui.UpdateWindow(self.hwnd) self.notify_id = None self.refresh_icon() win32gui.PumpMessages() win32gui.UnregisterClass(window_class.lpszClassName, None)
def destroy_window(self): win32gui.DestroyWindow(self.hwnd) # 销毁窗口 win32gui.UnregisterClass(self.classAtom, self.hinst) # 注销
def Destroy(self): gui.DestroyWindow(self.hwnd) gui.UnregisterClass(self.classAtom, None)