def _set_icon_win(self, filename): # ensure the window ico is ended by ico if not filename.endswith('.ico'): filename = '{}.ico'.format(filename.rsplit('.', 1)[0]) if not exists(filename): return False import win32api import win32gui import win32con hwnd = pygame.display.get_wm_info()['window'] icon_big = win32gui.LoadImage( None, filename, win32con.IMAGE_ICON, 48, 48, win32con.LR_LOADFROMFILE) icon_small = win32gui.LoadImage( None, filename, win32con.IMAGE_ICON, 16, 16, win32con.LR_LOADFROMFILE) win32api.SendMessage( hwnd, win32con.WM_SETICON, win32con.ICON_SMALL, icon_small) win32api.SendMessage( hwnd, win32con.WM_SETICON, win32con.ICON_BIG, icon_big) return True
def __init__(self, bitmapPath): win32gui.InitCommonControls() self.hinst = win32api.GetModuleHandle(None) # retreive width and height from bitmap file, because GetObject does not work for bitmaps :-( f = open(bitmapPath, 'rb') hdrfm = '<18xii' self.bmWidth, self.bmHeight = struct.unpack( hdrfm, f.read(struct.calcsize(hdrfm))) f.close() self.hSplash = win32gui.LoadImage( self.hinst, bitmapPath, win32con.IMAGE_BITMAP, 0, 0, win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE)
def refresh_icon(s, **data): hinst = win32gui.GetModuleHandle(None) if os.path.isfile(s.icon): # 尝试找到自定义图标 icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE hicon = win32gui.LoadImage(hinst, s.icon, win32con.IMAGE_ICON, 0, 0, icon_flags) else: # 找不到图标文件 - 使用默认值 hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) if s.notify_id: message = win32gui.NIM_MODIFY else: message = win32gui.NIM_ADD s.notify_id = (s.hwnd, 0, win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP, win32con.WM_USER + 20, hicon, s.hover_text) win32gui.Shell_NotifyIcon(message, s.notify_id)
def setUp(self): self.bmp_handle = None try: this_file = __file__ except NameError: this_file = sys.argv[0] this_dir = os.path.dirname(this_file) self.bmp_name = os.path.join(os.path.abspath(this_dir), "..", "Demos", "images", "smiley.bmp") self.failUnless(os.path.isfile(self.bmp_name), self.bmp_name) flags = win32con.LR_DEFAULTSIZE | win32con.LR_LOADFROMFILE self.bmp_handle = win32gui.LoadImage(0, self.bmp_name, win32con.IMAGE_BITMAP, 0, 0, flags) self.failUnless(self.bmp_handle, "Failed to get a bitmap handle")
def prep(self): ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON) ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON) hicon = win32gui.LoadImage(0, self.icon, win32con.IMAGE_ICON, \ ico_x, ico_y, win32con.LR_LOADFROMFILE) hdcBitmap = win32gui.CreateCompatibleDC(0) hdcScreen = win32gui.GetDC(0) hbm = win32gui.CreateCompatibleBitmap(hdcScreen, ico_x, ico_y) hbmOld = win32gui.SelectObject(hdcBitmap, hbm) brush = win32gui.GetSysColorBrush(win32con.COLOR_MENU) win32gui.FillRect(hdcBitmap, (0, 0, 16, 16), brush) win32gui.DrawIconEx(hdcBitmap, 0, 0, hicon, ico_x, ico_y, 0, 0, \ win32con.DI_NORMAL) win32gui.SelectObject(hdcBitmap, hbmOld) win32gui.DeleteDC(hdcBitmap) return hbm
def refresh_icon(self): 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 _DoCreateIcons(self): # Try and find a custom icon hinst = win32api.GetModuleHandle(None) iconPathName = os.path.abspath(os.path.join(os.path.split(sys.executable)[0],"img/TrayIcon.ico")) if not os.path.isfile(iconPathName): # Look in the current folder tree. iconPathName = "img/TrayIcon.ico" if os.path.isfile(iconPathName): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE hicon = win32gui.LoadImage(hinst, iconPathName, win32con.IMAGE_ICON, 0, 0, icon_flags) else: hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) flags = win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP nid = (self.hwnd, 0, flags, MainWindow.WM_TASKBAR_NOTIFY, hicon, "ClamWin Free Antivirus") win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid)
def main(): app = wx.App(0) hinst = win32gui.GetModuleHandle(None) try: hicon = win32gui.LoadImage(hinst, 1, win32con.IMAGE_ICON, 0, 0, win32con.LR_DEFAULTSIZE) except win32gui.error: hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) tooltip = os.path.basename(sys.executable) icon = SysTrayIcon() icon.Bind( wx.EVT_TASKBAR_LEFT_UP, lambda event: wx.MessageDialog( None, "Native system tray icon demo (Windows only)", "SysTrayIcon class", wx.OK | wx.ICON_INFORMATION).ShowModal()) icon.SetIcon(hicon, tooltip) win32gui.PumpMessages()
def LoadImage(self, iconPathName, fallback=FALLBACK_ICON): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE try: img_type = win32con.IMAGE_ICON if iconPathName.lower().split(".")[-1] in ("png", "bmp"): img_type = win32con.IMAGE_BITMAP icon_flags |= win32con.LR_CREATEDIBSECTION | win32con.LR_LOADTRANSPARENT log("LoadImage(%s) using image type=%s", iconPathName, { win32con.IMAGE_ICON: "ICON", win32con.IMAGE_BITMAP: "BITMAP" }.get(img_type)) v = win32gui.LoadImage(self.hinst, iconPathName, img_type, 0, 0, icon_flags) except Exception, e: log.error("Failed to load icon at %s: %s", iconPathName, e) v = fallback
def Init(self): rcp = self.window.manager.dialog_parser bmp_id = int(win32gui.GetWindowText(self.GetControl())) if bitmap_handles.has_key(bmp_id): handle = bitmap_handles[bmp_id] else: import resources mod_handle, mod_bmp, extra_flags = resources.GetImageParamsFromBitmapID( rcp, bmp_id) load_flags = extra_flags | win32con.LR_COLOR | win32con.LR_SHARED handle = win32gui.LoadImage(mod_handle, mod_bmp, win32con.IMAGE_BITMAP, 0, 0, load_flags) bitmap_handles[bmp_id] = handle win32gui.SendMessage(self.GetControl(), win32con.STM_SETIMAGE, win32con.IMAGE_BITMAP, handle)
def __init__(self): # 注册一个窗口类 self.wc = win32gui.WNDCLASS() self.hinst = self.wc.hInstance = win32gui.GetModuleHandle(None) self.wc.lpszClassName = "DMSTaskbar" self.wc.lpfnWndProc = self.wndProc self.classAtom = win32gui.RegisterClass(self.wc) style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU self.hwnd = win32gui.CreateWindow( self.classAtom, "DMSTaskbar", style, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, self.hinst, None) icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE hicon = win32gui.LoadImage(self.hinst, 'logo.ico', win32con.IMAGE_ICON, 0, 0, icon_flags) nid = (self.hwnd, 0, win32gui.NIF_ICON, win32con.WM_USER+20, hicon, "DMSTaskbar") win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid)
def load(img=os.path.join(os.path.dirname(__file__), "image.ico")): hdl = win32gui.GetForegroundWindow() #img = os.path.join(os.path.dirname(__file__), "image.ico") img = os.path.join(os.path.dirname(__file__), "logo.png") icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE hicon = win32gui.LoadImage(hdl, img, win32con.IMAGE_BITMAP, 100, 100, icon_flags) hdc = win32gui.GetDC(hdl) n = 1 while 1: if n <= 10: win32gui.DrawIcon(hdc, 100, 100, hicon) time.sleep(1) n += 1 else: break
def get_icon(self, path): hicon = self.icons.get(path) if hicon != None: return hicon # Try and find a custom icon hinst = win32gui.GetModuleHandle(None) if os.path.isfile(path): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE hicon = win32gui.LoadImage(hinst, path, win32con.IMAGE_ICON, 0, 0, icon_flags) else: print("Can't find icon file - using default.") hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) self.icons[path] = hicon return hicon
def __init__(self, title, msg): import os import sys import time import win32api import win32gui import win32con message_map = { win32con.WM_DESTROY: self.OnDestroy, } # Register the Window class. wc = win32gui.WNDCLASS() hinst = wc.hInstance = win32api.GetModuleHandle(None) # fucc from .random9 import Random wc.lpszClassName = "PythonTaskbar" + Random.string(100) wc.lpfnWndProc = message_map # could also specify a wndproc. classAtom = win32gui.RegisterClass(wc) # Create the Window. style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU self.hwnd = win32gui.CreateWindow(classAtom, "Taskbar", style, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, hinst, None) win32gui.UpdateWindow(self.hwnd) iconPathName = os.path.abspath( os.path.join(sys.path[0], "balloontip.ico")) icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE try: hicon = win32gui.LoadImage(hinst, iconPathName, 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", title, 200, msg)) time.sleep(60) win32gui.DestroyWindow(self.hwnd)
def __init__(self, parent, name, commands=None, icon_path=None): self.parent = parent self.name = name self.WM_NOTIFY = win32con.WM_USER + 20 wndproc = { win32con.WM_DESTROY: self.on_destroy, win32con.WM_COMMAND: self.on_command, self.WM_NOTIFY: self.on_tray_notify, } wc = win32gui.WNDCLASS() wc.hInstance = hinst = win32api.GetModuleHandle(None) wc.lpszClassName = name.title() wc.lpfnWndProc = wndproc try: class_atom = win32gui.RegisterClass(wc) except: pass self.hwnd = win32gui.CreateWindow(wc.lpszClassName, "", win32con.WS_POPUP, 0, 0, 1, 1, 0, 0, hinst, None) win32gui.UpdateWindow(self.hwnd) if icon_path is not None and os.path.isfile(icon_path): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE hicon = win32gui.LoadImage(None, icon_path, win32con.IMAGE_ICON, 0, 0, icon_flags) else: shell_dll = os.path.join(win32api.GetSystemDirectory(), "shell32.dll") large, small = win32gui.ExtractIconEx(shell_dll, 19, 1) #19 or 76 hicon = small[0] win32gui.DestroyIcon(large[0]) self.hicon = hicon flags = win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP | win32gui.NIF_INFO nid = (self.hwnd, 0, flags, self.WM_NOTIFY, self.hicon, self.name) win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid) self.next_command_id = 1000 self.commands = {} self.register_command('Exit', lambda: win32gui.DestroyWindow(self.hwnd)) if commands is not None: for n, f in commands[::-1]: self.register_command(n, f)
def _create_icons(self): # Try and find a custom icon hinst = win32api.GetModuleHandle(None) # @UndefinedVariable """ iconPathName = os.path.abspath(os.path.join( os.path.split(sys.executable)[0], "pyc.ico" )) if not os.path.isfile(iconPathName): # Look in DLLs dir, a-la py 2.5 iconPathName = os.path.abspath(os.path.join( os.path.split(sys.executable)[0], "DLLs", "pyc.ico" )) if not os.path.isfile(iconPathName): # Look in the source tree. iconPathName = os.path.abspath(os.path.join( os.path.split(sys.executable)[0], "..\\PC\\pyc.ico" )) if os.path.isfile(iconPathName): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE hicon = win32gui.LoadImage(hinst, iconPathName, win32con.IMAGE_ICON, 0, 0, icon_flags) else: print "Can't find a Python icon file - using default" hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) """ hicon = None if os.path.isfile(self.default_icon): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE try: hicon = win32gui.LoadImage(hinst, self.default_icon, win32con.IMAGE_ICON, 0, 0, icon_flags) except: logging.error("Can't load icon file - using default.") if not hicon: logging.error("Can't find icon file - using default.") hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) self.hicon = hicon flags = win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP nid = (self.hwnd, 0, flags, win32con.WM_USER + 20, hicon, self.hover_text) self.notify_id = nid try: win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid) except win32gui.error: # This is common when windows is starting, and this code is hit # before the taskbar has been created. # Keep running anyway - when explorer starts, we get the # TaskbarCreated message. logging.warning( "Failed to add the taskbar icon - is explorer running?")
def _createIcon(self): hinst = win32api.GetModuleHandle(None) iconPathName = "" if os.path.isfile(iconPathName): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE hicon = win32gui.LoadImage(hinst, iconPathName, win32con.IMAGE_ICON, 0, 0, icon_flags) else: print '未找到icon文件,使用默认' 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, "NetKeeper Service") try: win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid) except win32gui.error: print "Failed to add the taskbar icon - is explorer running?"
def prep_menu_icon(self, icon): # first load the icon ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON) ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON) hicon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, ico_x, ico_y, win32con.LR_LOADFROMFILE) hdcBitmap = win32gui.CreateCompatibleDC(None) hdcScreen = win32gui.GetDC(None) hbm = win32gui.CreateCompatibleBitmap(hdcScreen, ico_x, ico_y) hbmOld = win32gui.SelectObject(hdcBitmap, hbm) # fill the background brush = win32gui.GetSysColorBrush(win32con.COLOR_MENU) win32gui.FillRect(hdcBitmap, (0, 0, 16, 16), brush) # draw the icon win32gui.DrawIconEx(hdcBitmap, 0, 0, hicon, ico_x, ico_y, 0, 0, win32con.DI_NORMAL) win32gui.SelectObject(hdcBitmap, hbmOld) # no need to free the brush win32gui.DeleteDC(hdcBitmap) return hbm
def _DoCreateIcons(self): # Try and find a custom icon hinst = win32api.GetModuleHandle(None) iconPathName = os.path.abspath(".\\main.ico" ) if os.path.isfile(iconPathName): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE hicon = win32gui.LoadImage(hinst, iconPathName, win32con.IMAGE_ICON, 0, 0, icon_flags) else: hicon = win32gui.ExtractIcon(hinst, sys.executable, 0) flags = win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP nid = (self.hwnd, 0, flags, win32con.WM_USER+20, hicon, "HTPC Thermostat") try: win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid) except win32gui.error: # This is common when windows is starting, and this code is hit # before the taskbar has been created. log.debug("Failed to add the taskbar icon - is explorer running?")
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 OnPaint(self, hWnd, msg, wparam, lparam): dc, ps = win32gui.BeginPaint(hWnd) wndrect = win32gui.GetClientRect(hWnd) wndwidth = wndrect[2] - wndrect[0] wndheight = wndrect[3] - wndrect[1] if os.path.isfile(FILENAME): hBitmap = win32gui.LoadImage(0, FILENAME, win32con.IMAGE_BITMAP, 0, 0, win32con.LR_LOADFROMFILE) hdcBitmap = win32gui.CreateCompatibleDC(dc) hOldBitmap = win32gui.SelectObject(hdcBitmap, hBitmap) bminfo = win32gui.GetObject(hBitmap) win32gui.SetStretchBltMode(dc, win32con.COLORONCOLOR) win32gui.StretchBlt(dc, 0, 0, wndwidth, wndheight, hdcBitmap, 0, 0, bminfo.bmWidth, bminfo.bmHeight, win32con.SRCCOPY) win32gui.SelectObject(hdcBitmap, hOldBitmap) win32gui.EndPaint(hWnd, ps) return 0
def __init__(self, wp, imgfile, menu): # Create window procedure wp.update({ win32con.WM_DESTROY: self.__destroy, win32con.WM_COMMAND: self.command, self.MSG_NOTIFY: self.__notify, }) # Window class wc = win32gui.WNDCLASS() wc.hInstance = win32api.GetModuleHandle(None) wc.lpszClassName = 'TrayIcon' wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW wc.hCursor = win32api.LoadCursor(0, win32con.IDC_ARROW) wc.hbrBackground = win32con.COLOR_WINDOW wc.lpfnWndProc = wp # Register window class win32gui.RegisterClass(wc) # Create Window self.hwnd = win32gui.CreateWindow( wc.lpszClassName, '', win32con.WS_OVERLAPPED | win32con.WS_SYSMENU, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, wc.hInstance, None) win32gui.UpdateWindow(self.hwnd) # Create icon self.hicon = win32gui.LoadImage( wc.hInstance, imgfile, win32con.IMAGE_ICON, 0, 0, win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE) # Add icon into task bar flag = win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP nid = (self.hwnd, 0, flag, self.MSG_NOTIFY, self.hicon, self.TITLE) win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid) # Create popup menu self.menu = win32gui.CreatePopupMenu() for text, item in menu: self.menu = TrayIcon.create_menu(self.menu, item, text) super().__init__()
def prep_menu_icon(self, icon): # First load the icon. ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON) ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON) hIcon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, ico_x, ico_y, win32con.LR_LOADFROMFILE) hwndDC = win32gui.GetWindowDC(self.hwnd) dc = win32ui.CreateDCFromHandle(hwndDC) memDC = dc.CreateCompatibleDC() iconBitmap = win32ui.CreateBitmap() iconBitmap.CreateCompatibleBitmap(dc, ico_x, ico_y) oldBmp = memDC.SelectObject(iconBitmap) brush = win32gui.GetSysColorBrush(win32con.COLOR_MENU) win32gui.FillRect(memDC.GetSafeHdc(), (0, 0, ico_x, ico_y), brush) win32gui.DrawIconEx(memDC.GetSafeHdc(), 0, 0, hIcon, ico_x, ico_y, 0, 0, win32con.DI_NORMAL) memDC.SelectObject(oldBmp) memDC.DeleteDC() win32gui.ReleaseDC(self.hwnd, hwndDC) return iconBitmap.GetHandle()
def _DoCreateIcons(self): # Try and find a custom icon hinst = win32api.GetModuleHandle(None) icons = glob.glob("*.ico") if len(icons) > 0: iconPathName = os.path.abspath(os.path.join(os.getcwd(), icons[0])) else: iconPathName = os.path.abspath( os.path.join(os.path.split(sys.executable)[0], "pyc.ico")) #sys.executalbe为python解释程序路径 if not os.path.isfile(iconPathName): #如果系统ico文件不存在 # Look in DLLs dir, a-la py 2.5 iconPathName = os.path.abspath( os.path.join( os.path.split(sys.executable)[0], "DLLs", "pyc.ico")) if not os.path.isfile(iconPathName): # Look in the source tree. iconPathName = os.path.abspath( os.path.join( os.path.split(sys.executable)[0], "..\\PC\\pyc.ico")) if os.path.isfile(iconPathName): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE #ico标识,从文件载入和默认大小 hicon = win32gui.LoadImage(hinst, iconPathName, win32con.IMAGE_ICON, 0, 0, icon_flags) #载入.ico文件 '''handle = LoadImage(hinst,name,type,cx,cy,fuload)''' else: print("Can't find a Python icon file - using default") hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) flags = win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP | win32gui.NIF_INFO #定义托盘图标的样式 nid = (self.hwnd, 0, flags, win32con.WM_USER + 20, hicon, "安全生产计时器", "安全生产计时器系统托盘图标") #最后一个选项猪已经跑。。”是气泡提示内容 try: win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid) #增加系统托盘图标 except win32gui.error: # This is common when windows is starting, and this code is hit # before the taskbar has been created. print("Failed to add the taskbar icon - is explorer running?")
def _DoCreateIcons(self): hinst = win32api.GetModuleHandle(None) iconPathName = os.path.join(os.getcwd(), 'CapsOFF.ico') # set path to a tray icon icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE #checks if a choosen path contains an icon try: hicon = win32gui.LoadImage(hinst, iconPathName, win32con.IMAGE_ICON, 0, 0, icon_flags) except: hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) # sets some flags flags = win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP # creates tuple, consisting of # 1) our program's window's handler # 2) zero (?) # 3) our flags # 4) WM_USER constant + 20 # 5) handle of loaded icon # 6) Name of tray icon when mouse pointing on it nid = (self.hwnd, 0, flags, win32con.WM_USER + 20, hicon, "Kick Ass Prog") try: # implements our settings and sets (win32gui.NIM_ADD) # our window (nid)in tray win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid) win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, (self.hwnd, 0, win32gui.NIF_INFO, \ win32con.WM_USER+20, hicon, "Notification", \ 'Double-click on me to close', 200, 'Info')) except win32gui.error: # This is common when windows is starting, and this code is hit # before the taskbar has been created. print("Failed to add the taskbar icon - is explorer running?") # but keep running anyway - when explorer starts, we get the # TaskbarCreated message. Thread(target=self.show_Caps_state).start()
def create_icon(self): # Try and find a custom icon hinst = win32api.GetModuleHandle(None) iconPathName = os.path.abspath( os.path.dirname(os.path.realpath(__file__)) + "\\images\\pyc.ico") if os.path.isfile(iconPathName): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE hicon = win32gui.LoadImage( hinst, iconPathName, win32con.IMAGE_ICON, 0, 0, icon_flags) else: print("Can't find a Python icon file - using default") 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, "Tracker") try: win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid) except win32gui.error: # This is common when windows is starting, and this code is hit # before the taskbar has been created. print("Failed to add the taskbar icon - is explorer running?")
def _DoCreateIcons(self): # Try and find a custom icon hinst = win32api.GetModuleHandle(None) #iconPathName = os.path.realpath("icon.ico") iconPathName = os.path.realpath("money.ico") #iconPathName = os.path.abspath(os.path.join( os.path.split(sys.executable)[0], "pyc.ico" )) #if os.path.isfile(iconPathName): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE hicon = win32gui.LoadImage(hinst, iconPathName, win32con.IMAGE_ICON, 0, 0, icon_flags) flags = win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP nid = (self.hwnd, 0, flags, win32con.WM_USER+20, hicon, u"Döviz Kurları") try: 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",u"Kurları görüntülemek için çift tıklayın\nAyarlar için sağ tıklayın",200,u"Döviz Kurları")) #--Kurlar güncellenip yeni bir tooltip çıkacak-- except win32gui.error: # This is common when windows is starting, and this code is hit # before the taskbar has been created. print "Failed to add the taskbar icon - is explorer running?"
def prep_menu_icon(icon): # 首先加载图标 ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON) ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON) hicon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, ico_x, ico_y, win32con.LR_LOADFROMFILE) hdc_bitmap = win32gui.CreateCompatibleDC(0) hdc_screen = win32gui.GetDC(0) hbm = win32gui.CreateCompatibleBitmap(hdc_screen, ico_x, ico_y) hbm_old = win32gui.SelectObject(hdc_bitmap, hbm) # 填满背景 brush = win32gui.GetSysColorBrush(win32con.COLOR_MENU) win32gui.FillRect(hdc_bitmap, (0, 0, 16, 16), brush) # "GetSysColorBrush返回缓存的画笔而不是分配新的画笔" # - 暗示没有DeleteObject # 画出图标 win32gui.DrawIconEx(hdc_bitmap, 0, 0, hicon, ico_x, ico_y, 0, 0, win32con.DI_NORMAL) win32gui.SelectObject(hdc_bitmap, hbm_old) win32gui.DeleteDC(hdc_bitmap) return hbm
def __init__(self, hwnd): self._hwnd = hwnd self._id = 0 self._flags = win32gui.NIF_MESSAGE | win32gui.NIF_ICON self._callbackmessage = WM_TRAYMESSAGE path = os.path.join(os.path.dirname(__file__), '..', '..', 'icons', 'hicolor', '16x16', 'apps', 'gpodder.ico') icon_path = os.path.abspath(path) try: self._hicon = win32gui.LoadImage(None, icon_path, 1, 0, 0, 0x50) except pywintypes.error as e: logger.warn("Couldn't load gpodder icon for tray") self._hicon = win32gui.LoadIcon(0, IDI_APPLICATION) self._tip = '' self._info = '' self._timeout = 0 self._infotitle = '' self._infoflags = win32gui.NIIF_NONE win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, self.notify_config_data)
def _DoCreateIcons(self, iconame='httpserver.ico'): # Try and find a custom icon hinst = win32api.GetModuleHandle(None) iconPathName = os.path.join( os.path.split(os.path.abspath(sys.argv[0]))[0], iconame) if os.path.isfile(iconPathName): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE hicon = win32gui.LoadImage(hinst, iconPathName, win32con.IMAGE_ICON, 0, 0, icon_flags) else: 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, "Python Demo") try: win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid) except win32gui.error: print( f"Failed to add the taskbar icon - is explorer running? {''.join(traceback.format_exception(*sys.exc_info()))}" )