-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSysTrayIcon.py
151 lines (143 loc) · 6.92 KB
/
SysTrayIcon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import win32api, win32con, win32gui_struct, win32gui
import os
class SysTrayIcon(object):
'''SysTrayIcon类用于显示任务栏图标'''
QUIT = 'QUIT'
SPECIAL_ACTIONS = [QUIT]
FIRST_ID = 5320
def __init__(s, icon, hover_text, menu_options, on_quit, wh=1, tk_window=None, default_menu_index=None,
window_class_name=None, bs=None, tk_window_small=None):
'''
icon 需要显示的图标文件路径
hover_text 鼠标停留在图标上方时显示的文字
menu_options 右键菜单,格式: (('a', None, callback), ('b', None, (('b1', None, callback),)))
on_quit 传递退出函数,在执行退出时一并运行
tk_window 传递Tk窗口,s.root,用于单击图标显示窗口
default_menu_index 不显示的右键菜单序号
window_class_name 窗口类名
'''
s.bs = bs
s.a = tk_window_small
s.wh = wh
s.icon = icon
s.hover_text = hover_text
s.on_quit = on_quit
s.root = tk_window
menu_options = (('退出', None, s.QUIT),)
s._next_action_id = s.FIRST_ID
s.menu_actions_by_id = set()
s.menu_options = s._add_ids_to_menu_options(list(menu_options))
s.menu_actions_by_id = dict(s.menu_actions_by_id)
del s._next_action_id
s.default_menu_index = (default_menu_index or 0)
s.window_class_name = window_class_name or "SysTrayIconPy"
message_map = {win32gui.RegisterWindowMessage("TaskbarCreated"): s.restart,
win32con.WM_DESTROY: s.destroy,
win32con.WM_COMMAND: s.command,
win32con.WM_USER + 20: s.notify, }
# 注册窗口类
wc = win32gui.WNDCLASS()
wc.hInstance = win32gui.GetModuleHandle(None)
wc.lpszClassName = s.window_class_name
wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW;
wc.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
wc.hbrBackground = win32con.COLOR_WINDOW
wc.lpfnWndProc = message_map # 也可以指定wndproc.
s.classAtom = win32gui.RegisterClass(wc)
def activation(s):
hinst = win32gui.GetModuleHandle(None) # 创建窗口。
style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
s.hwnd = win32gui.CreateWindow(s.classAtom,
s.window_class_name,
style,
0, 0,
win32con.CW_USEDEFAULT,
win32con.CW_USEDEFAULT,
0, 0, hinst, None)
win32gui.UpdateWindow(s.hwnd)
s.notify_id = None
s.refresh(title='退入后台!', msg='右键退出', time=180)
win32gui.PumpMessages()
def refresh(s, title='', msg='', time=180):
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, # 句柄、托盘图标ID
win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP | win32gui.NIF_INFO,
# 托盘图标可以使用的功能的标识
win32con.WM_USER + 20, hicon, s.hover_text, # 回调消息ID、托盘图标句柄、图标字符串
msg, time, title, # 提示内容、提示显示时间、提示标题
win32gui.NIIF_INFO # 提示用到的图标
)
win32gui.Shell_NotifyIcon(message, s.notify_id)
def show_menu(s):
'''显示右键菜单'''
menu = win32gui.CreatePopupMenu()
s.create_menu(menu, s.menu_options)
pos = win32gui.GetCursorPos()
win32gui.SetForegroundWindow(s.hwnd)
win32gui.TrackPopupMenu(menu,
win32con.TPM_LEFTALIGN,
pos[0],
pos[1],
0,
s.hwnd,
None)
win32gui.PostMessage(s.hwnd, win32con.WM_NULL, 0, 0)
def _add_ids_to_menu_options(s, menu_options):
result = []
for menu_option in menu_options:
option_text, option_icon, option_action = menu_option
if callable(option_action) or option_action in s.SPECIAL_ACTIONS:
s.menu_actions_by_id.add((s._next_action_id, option_action))
result.append(menu_option + (s._next_action_id,))
else:
result.append((option_text,
option_icon,
s._add_ids_to_menu_options(option_action),
s._next_action_id))
s._next_action_id += 1
return result
def restart(s, hwnd, msg, wparam, lparam):
s.refresh()
def destroy(s, hwnd=None, msg=None, wparam=None, lparam=None, exit=1):
nid = (s.hwnd, 0)
win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid)
win32gui.PostQuitMessage(0) # 终止应用程序。
if exit and s.on_quit:
s.on_quit() # 需要传递自身过去时用 s.on_quit(s)
else:
if s.wh == 0:
s.a.destroy()
s.wh = 1
s.bs = 'big'
s.root.deiconify() # 显示tk窗口
def notify(s, hwnd, msg, wparam, lparam):
'''鼠标事件'''
if lparam == win32con.WM_LBUTTONDBLCLK: # 双击左键
pass
elif lparam == win32con.WM_RBUTTONUP: # 右键弹起
s.show_menu()
elif lparam == win32con.WM_LBUTTONUP: # 左键弹起
s.destroy(exit=0)
return True
def create_menu(s, menu, menu_options):
for option_text, option_icon, option_action, option_id in menu_options[::-1]:
if option_id in s.menu_actions_by_id:
item, extras = win32gui_struct.PackMENUITEMINFO(text=option_text,
hbmpItem=option_icon,
wID=option_id)
win32gui.InsertMenuItem(menu, 0, 1, item)
def command(s, hwnd, msg, wparam, lparam):
id = win32gui.LOWORD(wparam)
s.execute_menu_option(id)
def execute_menu_option(s, id):
win32gui.DestroyWindow(s.hwnd)