-
Notifications
You must be signed in to change notification settings - Fork 4
/
trayicon.py
244 lines (210 loc) · 9.52 KB
/
trayicon.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import os
import win32api
import win32con
import win32gui_struct
import win32gui
import os
class SysTrayIcon(object):
QUIT = 'QUIT'
SPECIAL_ACTIONS = [QUIT]
FIRST_ID = 1314
def __init__(s,
top,
icon,
hover_text,
menu_options,
on_quit=None,
default_menu_index=None,
window_class_name=None, ):
s.top=top
s.icon = icon
s.hover_text = hover_text
s.on_quit = on_quit
menu_options = 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.refresh_icon,
win32con.WM_DESTROY: s.destroy,
win32con.WM_COMMAND: s.command,
win32con.WM_USER + 20: s.notify, }
# 注册窗口类。
window_class = win32gui.WNDCLASS()
window_class.hInstance = win32gui.GetModuleHandle(None)
window_class.lpszClassName = s.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 # 也可以指定wndproc.
s.classAtom = win32gui.RegisterClass(window_class)
def show_icon(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_icon()
win32gui.PumpMessages()
def show_menu(s):
menu = win32gui.CreatePopupMenu()
s.create_menu(menu, s.menu_options)
# win32gui.SetMenuDefaultItem(menu, 1000, 0)
pos = win32gui.GetCursorPos()
# See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/menus_0hdi.asp
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 destroy(s, hwnd, msg, wparam, lparam):
if s.on_quit: s.on_quit(s) # 运行传递的on_quit
nid = (s.hwnd, 0)
win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid)
win32gui.PostQuitMessage(0) # 退出托盘图标
def notify(s, hwnd, msg, wparam, lparam):
if lparam == win32con.WM_LBUTTONDBLCLK: # 双击左键
pass # s.execute_menu_option(s.default_menu_index + s.FIRST_ID)
elif lparam == win32con.WM_RBUTTONUP: # 单击右键
s.show_menu()
elif lparam == win32con.WM_LBUTTONUP: # 单击左键
nid = (s.hwnd, 0)
win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid)
win32gui.PostQuitMessage(0) # 退出托盘图标
s.top.update()#更新父窗体
s.top.deiconify()#恢复隐形窗体
return True
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 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 create_menu(s, menu, menu_options):
for option_text, option_icon, option_action, option_id in menu_options[::-1]:
if option_icon:
option_icon = s.prep_menu_icon(option_icon)
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)
else:
submenu = win32gui.CreatePopupMenu()
s.create_menu(submenu, option_action)
item, extras = win32gui_struct.PackMENUITEMINFO(text=option_text,
hbmpItem=option_icon,
hSubMenu=submenu)
win32gui.InsertMenuItem(menu, 0, 1, item)
def prep_menu_icon(s, 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(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)
# "GetSysColorBrush返回缓存的画笔而不是分配新的画笔。"
# - 暗示没有DeleteObject
# 画出图标
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 command(s, hwnd, msg, wparam, lparam):
id = win32gui.LOWORD(wparam)
s.execute_menu_option(id)
def execute_menu_option(s, id):
menu_action = s.menu_actions_by_id[id]
if menu_action == s.QUIT:
win32gui.DestroyWindow(s.hwnd)
else:
menu_action(s)
class _Main:
def main(self):
######################### tkinter界面设定 #####################################
import tkinter as tk
window = tk.Tk()
window.title('智能对话小助手')
window.geometry('850x560')
window.resizable(0, 0)
canvas = tk.Canvas(window, width=850, height=600)
canvas.pack()
# window.mainloop()
########################### 开始托盘程序嵌入 #####################################
self.root = window
icons = os.getcwd() + r'\robot.ico'
print(icons)
hover_text = "签到工具" # 悬浮于图标上方时的提示
menu_options = ()
self.sysTrayIcon = SysTrayIcon(icons, hover_text, menu_options, on_quit=self.exit, default_menu_index=1)
self.root.bind("<Unmap>", lambda event: self.Unmap() if self.root.state() == 'iconic' else False)
self.root.protocol('WM_DELETE_WINDOW', self.exit)
self.root.resizable(0, 0)
self.root.mainloop()
def switch_icon(s, _sysTrayIcon, icons='D:\\2.ico'):
_sysTrayIcon.icon = icons
_sysTrayIcon.refresh_icon()
# 点击右键菜单项目会传递SysTrayIcon自身给引用的函数,所以这里的_sysTrayIcon = s.sysTrayIcon
def Unmap(s):
s.root.withdraw()
s.sysTrayIcon.show_icon()
def exit(s, _sysTrayIcon=None):
s.root.destroy()
print('exit...')
if __name__ == '__main__':
Main = _Main()
Main.main()