-
Notifications
You must be signed in to change notification settings - Fork 0
/
balloontip.py
65 lines (57 loc) · 2.11 KB
/
balloontip.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
#Code Belongs to : https://gist.github.com/brousch/6523559
import os
import sys
import time
import win32con
import win32gui
from win32api import GetModuleHandle
import threading
class WindowsBalloonTip:
def __init__(self, title, msg):
t = threading.Thread(target=self.DoEverything,args=[title,msg])
t.start()
def DoEverything(self, title='beep', msg='boop'):
message_map = {win32con.WM_DESTROY: self.OnDestroy, }
# Register the Window class.
wc = win32gui.WNDCLASS()
self.destroyed = False
hinst = wc.hInstance = GetModuleHandle(None)
wc.lpszClassName = "PythonTaskbar"
wc.lpfnWndProc = message_map # could also specify a wndproc.
class_atom = win32gui.RegisterClass(wc)
# Create the Window.
style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
self.hwnd = win32gui.CreateWindow(class_atom, "Taskbar", style,
0, 0, win32con.CW_USEDEFAULT,
win32con.CW_USEDEFAULT, 0, 0,
hinst, None)
win32gui.UpdateWindow(self.hwnd)
icon_path_name = os.path.abspath(os.path.join(sys.path[0],
"balloontip.ico"))
icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
try:
hicon = win32gui.LoadImage(hinst, icon_path_name,
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", msg, 200, title))
# self.show_balloon(title, msg)
time.sleep(10)
win32gui.DestroyWindow(self.hwnd)
win32gui.UnregisterClass(class_atom, hinst)
self.destroyed = True
def OnDestroy(self, hwnd, msg, wparam, lparam):
nid = (self.hwnd, 0)
win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid)
win32gui.PostQuitMessage(0) # Terminate the app.
def isDestroyed(self):
return self.destroyed
def balloon_tip(title, msg):
w = WindowsBalloonTip(title, msg)
return w