def refresh_icon(self): """ Refresh the icon being used by the application """ hinst = win32gui.GetModuleHandle(None) if os.path.isfile(self.icon): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE hicon = win32gui.LoadImage(hinst, self.icon, win32con.IMAGE_ICON, 0, 0, icon_flags) else: print("Can't find icon file - using default.") hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) if self.notify_id: message = win32gui.NIM_MODIFY else: message = win32gui.NIM_ADD self.notify_id = (self.hwnd, 0, win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP, win32con.WM_USER+20, hicon, self.hover_text) win32gui.Shell_NotifyIcon(message, self.notify_id)
def refresh_icon_handler(self): # Try and find a custom icon if not self.hwnd: return hinst = win32gui.GetModuleHandle(None) hicon = self.icon_cache.get(self.icon, None) if hicon is None: if os.path.isfile(self.icon): hicon = win32gui.LoadImage( hinst, self.icon, win32con.IMAGE_ICON, 0, 0, win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE) else: print "Can't find icon file - using default." hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) self.icon_cache[self.icon] = hicon if self.notify_id: message = win32gui.NIM_MODIFY else: message = win32gui.NIM_ADD self.notify_id = (self.hwnd, 0, win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP, win32con.WM_USER + 20, hicon, self.tooltip_text.encode(os_encoding)) try: win32gui.Shell_NotifyIcon(message, self.notify_id) except: if message == win32gui.NIM_ADD: self.notify_id = None raise
def run(self): self.initialize() while not self.terminate: win32gui.PumpWaitingMessages() self.doUpdates() sleep(0.100) win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, (self.hwnd, 0))
def refresh_icon(self): # Try and find a custom icon hinst = win32gui.GetModuleHandle(None) if os.path.isfile(self.icon): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE hicon = win32gui.LoadImage(hinst, self.icon, win32con.IMAGE_ICON, 0, 0, icon_flags) else: print ("Can't find icon file - using default.") hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) if self.notify_id: message = win32gui.NIM_MODIFY # NIM_MODIFY sends a message to modify an icon in the taskbar. else: message = win32gui.NIM_ADD # NIM_ADD sends a message to add an icon in the taskbar. # This is a NOTIFYICONDATA structure as defined here: https://msdn.microsoft.com/en-us/library/windows/desktop/bb773352(v=vs.85).aspx # The fourth argument sets uCallbackMessage = 'win32con.WM_USER+self.OFFSET' # uCallbackMessage: "The system uses this identifier to send notification messages to the window identified in hWnd." self.notify_id = (self.hwnd, 0, win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP, win32con.WM_USER+self.OFFSET, hicon, self.hover_text) win32gui.Shell_NotifyIcon(message, self.notify_id) # Registers the message 'win32con.WM_USER+self.OFFSET' to trigger the 'notify' function on mouse-event
def destroy(self, hwnd, msg, wparam, lparam): # called when closed if self.on_quit: self.on_quit(self) nid = (self.hwnd, 0) win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid) win32gui.PostQuitMessage(0) # Terminate the app.
def refresh_icon(self, recreate=False): # Try and find a custom icon hinst = win32gui.GetModuleHandle(None) if isinstance(self.icon, str): if os.path.isfile(self.icon): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE hicon = win32gui.LoadImage(hinst, self.icon, win32con.IMAGE_ICON, 0, 0, icon_flags) else: hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) else: hinst = win32api.GetModuleHandle(None) hicon = win32gui.LoadIcon(hinst, int(self.icon)) if not self.notify_id or recreate: message = win32gui.NIM_ADD else: message = win32gui.NIM_MODIFY self.notify_id = (self.hwnd, 0, win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP, win32con.WM_USER+20, hicon, self.hover_text) try: win32gui.Shell_NotifyIcon(message, self.notify_id) except: # just catch strange error pass
def __init__(self): message_map = { win32con.WM_DESTROY: self.OnDestroy, win32con.WM_COMMAND: self.OnCommand, win32con.WM_USER + 20: self.OnTaskbarNotify, } # Register the Window class. wc = gui.WNDCLASS() hinst = wc.hInstance = api.GetModuleHandle(None) wc.lpszClassName = "IdlerPopupHandler" wc.lpfnWndProc = message_map # could also specify a wndproc. classAtom = gui.RegisterClass(wc) self.classAtom = classAtom # Create the Window. style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU self.hwnd = gui.CreateWindow( classAtom, "Idling Notifications", style, \ 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, \ 0, 0, hinst, None) gui.UpdateWindow(self.hwnd) iconPathName = os.path.abspath(os.path.join(sys.prefix, "pyc.ico")) icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE try: hicon = gui.LoadImage(hinst, iconPathName, win32con.IMAGE_ICON, 0, 0, icon_flags) except: hicon = gui.LoadIcon(0, win32con.IDI_APPLICATION) flags = gui.NIF_ICON | gui.NIF_MESSAGE | gui.NIF_TIP nid = (self.hwnd, 0, flags, win32con.WM_USER + 20, hicon, "Idling Notifications") gui.Shell_NotifyIcon(gui.NIM_ADD, nid)
def destroy(self, hwnd, msg, wparam, lparam): #if self.on_quit: self.on_quit(self) nid = (self.hwnd, 0) if self.apiServer: self.apiServer.kill() win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid) win32gui.PostQuitMessage(0) # Terminate the app.
def refresh_icon(self): # Try and find a custom icon self.hinst = win32gui.GetModuleHandle(None) # 获取句柄 if os.path.isfile(self.icon): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE hicon = win32gui.LoadImage(self.hinst, # 载入图像 #句柄 self.icon, # icon win32con.IMAGE_ICON, # 类型 0, # cxDesired 参数,控制像素 0, # cyDesired icon_flags) # 载入图像大小 else: # print "Can't find icon file - using default." # 没有找到图标,使用默认图标 hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) if self.notify_id: message = win32gui.NIM_MODIFY # 修改状态去的图标 else: message = win32gui.NIM_ADD # 添加图标到状态区 self.notify_id = (self.hwnd, 0, win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP, win32con.WM_USER + 20, hicon, self.hover_text) win32gui.Shell_NotifyIcon(message, self.notify_id) # 添加、移除或改变任务栏图标
def refresh_icon(self): if self.notify_id: message = win32gui.NIM_MODIFY else: message = win32gui.NIM_ADD self.notify_id = (self.hwnd, 0, win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP, win32con.WM_USER + 20, self.icon, self.hover_text) win32gui.Shell_NotifyIcon(message, self.notify_id)
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 refresh_icon(self): # Try and find a custom icon hinst = win32gui.GetModuleHandle(None) if os.path.isfile(self.icon): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE hicon = win32gui.LoadImage(hinst, self.icon, win32con.IMAGE_ICON, 0, 0, icon_flags) else: hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) if self.error_on_no_icon: raise FileNotFoundError("Can't find icon file") else: print("Can't find icon file - set error_on_no_icon to True to recieve an error") if self.notify_id: message = win32gui.NIM_MODIFY else: message = win32gui.NIM_ADD self.notify_id = (self.hwnd, 0, win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP, win32con.WM_USER+20, hicon, self.hover_text) win32gui.Shell_NotifyIcon(message, self.notify_id)
def refresh_icon(self, recreate=False): """ Refresh the icon. To be called after updating the icon. :param recreate: Recreate the icon """ # Try and find a custom icon hinst = win32gui.GetModuleHandle(None) if isinstance(self.icon, str): if os.path.isfile(self.icon): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE hicon = win32gui.LoadImage(hinst, self.icon, win32con.IMAGE_ICON, 0, 0, icon_flags) else: hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) else: hinst = win32api.GetModuleHandle(None) hicon = win32gui.LoadIcon(hinst, int(self.icon)) if not self.notify_id or recreate: message = win32gui.NIM_ADD else: message = win32gui.NIM_MODIFY self.notify_id = (self.hwnd, 0, win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP, win32con.WM_USER + 20, hicon, self.hover_text) win32gui.Shell_NotifyIcon(message, self.notify_id)
def destroy(self, hwnd, msg, wparam, lparam): if self.on_quit: self.on_quit(self) nid = (self.hwnd, 0) win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid) win32gui.PostQuitMessage(0) # Terminate the app. # Using the included postgres database? if os.path.exists(os.path.join(settings.FREPPLE_HOME, '..', 'pgsql', 'bin', 'pg_ctl.exe')): # Check if the database is running. If so, stop it. os.environ['PATH'] = os.path.join(settings.FREPPLE_HOME, '..', 'pgsql', 'bin') + os.pathsep + os.environ['PATH'] status = call([ os.path.join(settings.FREPPLE_HOME, '..', 'pgsql', 'bin', 'pg_ctl.exe'), "--pgdata", os.path.join(settings.FREPPLE_LOGDIR, 'database'), "--silent", "status" ], stdin=DEVNULL, stdout=DEVNULL, stderr=DEVNULL, creationflags=CREATE_NO_WINDOW ) if not status: call([ os.path.join(settings.FREPPLE_HOME, '..', 'pgsql', 'bin', 'pg_ctl.exe'), "--pgdata", os.path.join(settings.FREPPLE_LOGDIR, 'database'), "--log", os.path.join(settings.FREPPLE_LOGDIR, 'database', 'server.log'), "-w", # Wait till it's down "stop" ], stdin=DEVNULL, stdout=DEVNULL, stderr=DEVNULL, creationflags=CREATE_NO_WINDOW )
def refresh_icon(self): # Try and find a custom icon hinst = win32gui.GetModuleHandle(None) if os.path.isfile(self.icon): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE self.hicon = win32gui.LoadImage(hinst, self.icon, win32con.IMAGE_ICON, 0, 0, icon_flags) else: print("Can't find icon file - using default.") self.hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) if self.notify_id: message = win32gui.NIM_MODIFY else: message = win32gui.NIM_ADD self.notify_id = (self.shell.main_frame.hwnd, 0, (win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP), win32con.WM_USER + 20, self.hicon, self.hover_text) win32gui.Shell_NotifyIcon(message, self.notify_id)
def sendnotification(self, title, msg): hicon = self.get_icon(self.icon) win32gui.Shell_NotifyIcon( win32gui.NIM_MODIFY, (self.hwnd, 0, win32gui.NIF_INFO, win32con.WM_USER + 20, hicon, "Balloon tooltip", msg, 200, title), )
def destroy(self, hwnd, msg, wparam, lparam): if self.on_quit: self.on_quit(self) nid = (self.hwnd, 0) try: win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid) except: pass win32gui.PostQuitMessage(0) # Terminate the app.
def notify(self, message, title="Ava Message"): balloon_id = (self.shell.main_frame.hwnd, 0, win32gui.NIF_INFO, win32con.WM_USER + 20, self.hicon, self.hover_text, title, 200, message) win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, balloon_id)
def refresh_icon(self): hicon = self.get_icon(self.icon) if self.notify_id: message = win32gui.NIM_MODIFY else: message = win32gui.NIM_ADD self.notify_id = (self.hwnd, 0, win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP, win32con.WM_USER + 20, hicon, self.hover_text) try: win32gui.Shell_NotifyIcon(message, self.notify_id) except: # Timeouts can occur after system comes out of standby/hibernate pass
def display_baloon(self, address): self.baloon_address = address game_description = "" game_type = "" with self.game_list_lock: (game_description, game_type) = self.format_game_text(address) my_notify = (self.hwnd, 0, win32gui.NIF_INFO, 0, self.icon, "", "New game detected, click to join.\n%s\nIP: %s%s" % (game_description, address[0], game_type), self.BALOON_TIMEOUT * 1000, "Quake announcement", 0x00000004) win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, my_notify)
def show_balloon_tip(self, title, message): _NOTIFYICONDATA = ( self.hwnd, # hWnd 0, # ID win32gui.NIF_INFO, # Flags 0, # CallbackMessage 0, # hIcon "", # Tip message, # Info 10000, # Timeout title, # InfoTitle 0, # InfoFlags ) win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, _NOTIFYICONDATA)
def destroy(self, hwnd, msg, wparam, lparam): if self.on_quit: self.on_quit(self) nid = (self.hwnd, 0) win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid) win32gui.PostQuitMessage(0) # Terminate the app. # Using the included postgres database? if os.path.exists( os.path.join(settings.FREPPLE_HOME, "..", "pgsql", "bin", "pg_ctl.exe") ): # Check if the database is running. If so, stop it. os.environ["PATH"] = ( os.path.join(settings.FREPPLE_HOME, "..", "pgsql", "bin") + os.pathsep + os.environ["PATH"] ) status = subprocess.call( [ os.path.join( settings.FREPPLE_HOME, "..", "pgsql", "bin", "pg_ctl.exe" ), "--pgdata", os.path.join(settings.FREPPLE_LOGDIR, "database"), "--silent", "status", ], stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, creationflags=CREATE_NO_WINDOW, ) if not status: subprocess.call( [ os.path.join( settings.FREPPLE_HOME, "..", "pgsql", "bin", "pg_ctl.exe" ), "--pgdata", os.path.join(settings.FREPPLE_LOGDIR, "database"), "--log", os.path.join(settings.FREPPLE_LOGDIR, "database", "server.log"), "-w", # Wait till it's down "stop", ], stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, creationflags=CREATE_NO_WINDOW, )
def refresh_icon(self): # Try and find a custom icon hinst = win32gui.GetModuleHandle(None) if isinstance(self.icon, str) and os.path.isfile(self.icon): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE hicon = win32gui.LoadImage(hinst, self.icon, win32con.IMAGE_ICON, 0, 0, icon_flags) else: hicon = win32gui.LoadIcon(hinst, int(self.icon)) if self.notify_id: message = win32gui.NIM_MODIFY else: message = win32gui.NIM_ADD self.notify_id = (self.hwnd, 0, win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP, win32con.WM_USER+20, hicon, self.hover_text) win32gui.Shell_NotifyIcon(message, self.notify_id)
def refresh_icon(self): # Try and find a custom icon hinst = win32gui.GetModuleHandle(None) if os.path.isfile(self.icon): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE hicon = win32gui.LoadImage(hinst, self.icon, win32con.IMAGE_ICON, 0, 0, icon_flags) else: raise Exception("winsystray: Can't find icon file.") hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) if self.notify_id: message = win32gui.NIM_MODIFY else: message = win32gui.NIM_ADD self.notify_id = (self.hwnd, 0, win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP, win32con.WM_USER + 20, hicon, self.hover_text) win32gui.Shell_NotifyIcon(message, self.notify_id)
def draw_notify_icon(self, title='', info='', hicon=None, fresh=False): # See https://docs.microsoft.com/en-us/windows/win32/api/shellapi/ns-shellapi-notifyicondataw NIIF_USER = 0x00000004 NIIF_NOSOUND = 0x00000010 NIIF_LARGE_ICON = 0x00000020 flag = win32gui.NIF_INFO if info else win32gui.NIF_MESSAGE notify_id = ( self.hwnd, 0, win32gui.NIF_ICON | win32gui.NIF_TIP | flag, win32con.WM_USER + 20, hicon if hicon else self.hicon, self.tips, info, 4, title, (NIIF_USER | NIIF_NOSOUND | NIIF_LARGE_ICON), ) win32gui.Shell_NotifyIcon( win32gui.NIM_ADD if fresh else win32gui.NIM_MODIFY, notify_id)
def destroy(self, hwnd, msg, wparam, lparam,kill_program=True): nid = (self.hwnd, 0) win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid) win32gui.PostQuitMessage(0) # Terminate the app. if self.on_quit and kill_program: self.on_quit()
def OnDestroy(self, hwnd, msg, wparam, lparam): nid = (self.main_frame.hwnd, 0) win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid) win32gui.PostQuitMessage(0) # Terminate the app.
def destory(sysTrayIcon): nid = (sysTrayIcon.hwnd, 0) win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid) win32gui.PostQuitMessage(0) # Terminate the app.
def on_destroy(self, hwnd, msg, wparam, lparam): if callable(self.on_quit): self.on_quit() nid = (self.hwnd, 0) win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid) win32gui.PostQuitMessage(0) # Terminate the app.
def hide_baloon(self): my_notify = (self.hwnd, 0, win32gui.NIF_INFO, 0, self.icon, "", "", self.BALOON_TIMEOUT * 1000, "", 0x00000004) win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, my_notify)