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
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)
python类NIF_TIP的实例源码
def update_tray_icon(self):
try:
hIcon = win32gui.LoadIcon(
win32gui.GetModuleHandle(None), win32con.IDI_APPLICATION
)
except:
hIcon = win32gui.LoadIcon(None, win32con.IDI_APPLICATION)
if self.__NOTIFY_ID is None:
message = win32gui.NIM_ADD
else:
message = win32gui.NIM_MODIFY
self.__NOTIFY_ID = (
self.HWND,
0,
win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP,
self.MESSAGE,
hIcon,
self.tooltip
)
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:
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 _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 set_tooltip(self, tooltip):
""" Sets the tray icon tooltip. """
self._flags = self._flags | win32gui.NIF_TIP
self._tip = tooltip
win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, self._get_nid())
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], "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)
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:
# 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.
def new_icon(hdesk,desktop_name):
""" Runs as a thread on each desktop to create a new tray icon and handle its messages """
global id
id=id+1
hdesk.SetThreadDesktop()
## apparently the threads can't use same hinst, so each needs its own window class
windowclassname='PythonDesktopManager'+desktop_name
wc = win32gui.WNDCLASS()
wc.hInstance = win32api.GetModuleHandle(None)
wc.lpszClassName = windowclassname
wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW | win32con.CS_GLOBALCLASS
wc.hCursor = win32gui.LoadCursor( 0, win32con.IDC_ARROW )
wc.hbrBackground = win32con.COLOR_WINDOW
wc.lpfnWndProc = icon_wndproc
windowclass = win32gui.RegisterClass(wc)
style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
hwnd = win32gui.CreateWindow(windowclass, 'dm_'+desktop_name, win32con.WS_SYSMENU,
0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT,
0, 0, wc.hInstance, None)
win32gui.UpdateWindow(hwnd)
flags = win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP
notify_info = (hwnd, id, flags, win32con.WM_USER+20, hicon, 'Desktop Manager (%s)' %desktop_name)
window_info[hwnd]=notify_info
## wait for explorer to initialize system tray for new desktop
tray_found=0
while not tray_found:
try:
tray_found=win32gui.FindWindow("Shell_TrayWnd",None)
except win32gui.error:
traceback.print_exc
time.sleep(.5)
win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, notify_info)
win32gui.PumpMessages()
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], "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)
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:
# 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.
def new_icon(hdesk,desktop_name):
""" Runs as a thread on each desktop to create a new tray icon and handle its messages """
global id
id=id+1
hdesk.SetThreadDesktop()
## apparently the threads can't use same hinst, so each needs its own window class
windowclassname='PythonDesktopManager'+desktop_name
wc = win32gui.WNDCLASS()
wc.hInstance = win32api.GetModuleHandle(None)
wc.lpszClassName = windowclassname
wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW | win32con.CS_GLOBALCLASS
wc.hCursor = win32gui.LoadCursor( 0, win32con.IDC_ARROW )
wc.hbrBackground = win32con.COLOR_WINDOW
wc.lpfnWndProc = icon_wndproc
windowclass = win32gui.RegisterClass(wc)
style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
hwnd = win32gui.CreateWindow(windowclass, 'dm_'+desktop_name, win32con.WS_SYSMENU,
0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT,
0, 0, wc.hInstance, None)
win32gui.UpdateWindow(hwnd)
flags = win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP
notify_info = (hwnd, id, flags, win32con.WM_USER+20, hicon, 'Desktop Manager (%s)' %desktop_name)
window_info[hwnd]=notify_info
## wait for explorer to initialize system tray for new desktop
tray_found=0
while not tray_found:
try:
tray_found=win32gui.FindWindow("Shell_TrayWnd",None)
except win32gui.error:
traceback.print_exc
time.sleep(.5)
win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, notify_info)
win32gui.PumpMessages()
def balloon_tip(self, title, msg):
style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
hwnd = CreateWindow(self.classAtom, "Taskbar", style, 0, 0,
win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT,
0, 0, self.hinst, None)
UpdateWindow(hwnd)
hicon = LoadIcon(0, win32con.IDI_APPLICATION)
nid = (hwnd, 0, NIF_ICON | NIF_MESSAGE | NIF_TIP, win32con.WM_USER + 20, hicon, 'Tooltip')
Shell_NotifyIcon(NIM_ADD, nid)
nid = (hwnd, 0, NIF_INFO, win32con.WM_USER + 20, hicon, 'Balloon Tooltip', msg, 200, title, NIIF_INFO)
Shell_NotifyIcon(NIM_MODIFY, nid)
DestroyWindow(hwnd)
def __createIcon(self):
# try and use custom icon
if self.iconPath and os.path.isfile(self.iconPath):
hicon = self.__loadFromFile(self.iconPath)
else:
try:
fp = 'tmp.ico'
icFH = file(fp, 'wb')
if self.serverState == self.EnumServerState.STOPPED:
icFH.write(base64.b64decode(self.__getIconStopped()))
elif self.serverState == self.EnumServerState.RUNNING:
icFH.write(base64.b64decode(self.__getIconRunning()))
icFH.close()
hicon = self.__loadFromFile(fp)
os.unlink(fp)
except:
print("Can't load web2py icons - 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,
'web2py Framework',
)
try:
win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, nid)
except:
try:
win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid)
except win32api.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
def __createIcon(self):
# try and use custom icon
if self.iconPath and os.path.isfile(self.iconPath):
hicon = self.__loadFromFile(self.iconPath)
else:
try:
fp = 'tmp.ico'
icFH = file(fp, 'wb')
if self.serverState == self.EnumServerState.STOPPED:
icFH.write(base64.b64decode(self.__getIconStopped()))
elif self.serverState == self.EnumServerState.RUNNING:
icFH.write(base64.b64decode(self.__getIconRunning()))
icFH.close()
hicon = self.__loadFromFile(fp)
os.unlink(fp)
except:
print "Can't load web2py icons - 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,
'web2py Framework',
)
try:
win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, nid)
except:
try:
win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid)
except win32api.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
def __createIcon(self):
# try and use custom icon
if self.iconPath and os.path.isfile(self.iconPath):
hicon = self.__loadFromFile(self.iconPath)
else:
try:
fp = 'tmp.ico'
icFH = file(fp, 'wb')
if self.serverState == self.EnumServerState.STOPPED:
icFH.write(base64.b64decode(self.__getIconStopped()))
elif self.serverState == self.EnumServerState.RUNNING:
icFH.write(base64.b64decode(self.__getIconRunning()))
icFH.close()
hicon = self.__loadFromFile(fp)
os.unlink(fp)
except:
print "Can't load web2py icons - 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,
'web2py Framework',
)
try:
win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, nid)
except:
try:
win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid)
except win32api.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
def __createIcon(self):
# try and use custom icon
if self.iconPath and os.path.isfile(self.iconPath):
hicon = self.__loadFromFile(self.iconPath)
else:
try:
fp = 'tmp.ico'
icFH = file(fp, 'wb')
if self.serverState == self.EnumServerState.STOPPED:
icFH.write(base64.b64decode(self.__getIconStopped()))
elif self.serverState == self.EnumServerState.RUNNING:
icFH.write(base64.b64decode(self.__getIconRunning()))
icFH.close()
hicon = self.__loadFromFile(fp)
os.unlink(fp)
except:
print "Can't load web2py icons - 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,
'web2py Framework',
)
try:
win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, nid)
except:
try:
win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid)
except win32api.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
def __createIcon(self):
# try and use custom icon
if self.iconPath and os.path.isfile(self.iconPath):
hicon = self.__loadFromFile(self.iconPath)
else:
try:
fp = 'tmp.ico'
icFH = file(fp, 'wb')
if self.serverState == self.EnumServerState.STOPPED:
icFH.write(base64.b64decode(self.__getIconStopped()))
elif self.serverState == self.EnumServerState.RUNNING:
icFH.write(base64.b64decode(self.__getIconRunning()))
icFH.close()
hicon = self.__loadFromFile(fp)
os.unlink(fp)
except:
print("Can't load web2py icons - 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,
'web2py Framework',
)
try:
win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, nid)
except:
try:
win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid)
except win32api.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
def __createIcon(self):
# try and use custom icon
if self.iconPath and os.path.isfile(self.iconPath):
hicon = self.__loadFromFile(self.iconPath)
else:
try:
fp = 'tmp.ico'
icFH = file(fp, 'wb')
if self.serverState == self.EnumServerState.STOPPED:
icFH.write(base64.b64decode(self.__getIconStopped()))
elif self.serverState == self.EnumServerState.RUNNING:
icFH.write(base64.b64decode(self.__getIconRunning()))
icFH.close()
hicon = self.__loadFromFile(fp)
os.unlink(fp)
except:
print "Can't load web2py icons - 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,
'web2py Framework',
)
try:
win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, nid)
except:
try:
win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid)
except win32api.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
def __createIcon(self):
# try and use custom icon
if self.iconPath and os.path.isfile(self.iconPath):
hicon = self.__loadFromFile(self.iconPath)
else:
try:
fp = 'tmp.ico'
icFH = file(fp, 'wb')
if self.serverState == self.EnumServerState.STOPPED:
icFH.write(base64.b64decode(self.__getIconStopped()))
elif self.serverState == self.EnumServerState.RUNNING:
icFH.write(base64.b64decode(self.__getIconRunning()))
icFH.close()
hicon = self.__loadFromFile(fp)
os.unlink(fp)
except:
print "Can't load web2py icons - 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,
'web2py Framework',
)
try:
win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, nid)
except:
try:
win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid)
except win32api.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