ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/learning/task-bar.py
Revision: 8
Committed: Sat May 5 04:21:19 2012 UTC (13 years, 10 months ago) by ninoborges
Content type: text/x-python
File size: 3125 byte(s)
Log Message:
Initial Import

File Contents

# Content
1 ## Creates a task-bar icon. Run from Python.exe to see the
2 ## messages printed.
3 ##EBorges
4 ##05.08.03
5
6
7 import win32api, win32con, win32gui
8
9 class Taskbar:
10 def __init__(self):
11 self.visible = 0
12 message_map = {
13 win32con.WM_DESTROY: self.onDestroy,
14 win32con.WM_USER+20 : self.onTaskbarNotify,
15 }
16 # Register the Window class.
17 wc = win32gui.WNDCLASS()
18 hinst = wc.hInstance = win32api.GetModuleHandle(None)
19 wc.lpszClassName = "PythonTaskbarDemo"
20 wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW;
21 wc.hCursor = win32gui.LoadCursor( 0, win32con.IDC_ARROW )
22 wc.hbrBackground = win32con.COLOR_WINDOW
23 wc.lpfnWndProc = message_map # could also specify a wndproc.
24 classAtom = win32gui.RegisterClass(wc)
25 # Create the Window.
26 style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
27 self.hwnd = win32gui.CreateWindow( classAtom, "Taskbar Demo", style, \
28 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, \
29 0, 0, hinst, None)
30 win32gui.UpdateWindow(self.hwnd)
31
32 def setIcon(self, hicon, tooltip=None):
33 self.hicon = hicon
34 self.tooltip = tooltip
35
36 def show(self):
37 """Display the taskbar icon"""
38 flags = win32gui.NIF_ICON | win32gui.NIF_MESSAGE
39 if self.tooltip is not None:
40 flags |= win32gui.NIF_TIP
41 nid = (self.hwnd, 0, flags, win32con.WM_USER+20, self.hicon, self.tooltip)
42 else:
43 nid = (self.hwnd, 0, flags, win32con.WM_USER+20, self.hicon)
44 if self.visible:
45 self.hide()
46 win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid)
47 self.visible = 1
48
49 def hide(self):
50 """Hide the taskbar icon"""
51 if self.visible:
52 nid = (self.hwnd, 0)
53 win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid)
54 self.visible = 0
55
56 def onDestroy(self, hwnd, msg, wparam, lparam):
57 self.hide()
58 win32gui.PostQuitMessage(0) # Terminate the app.
59
60 def onTaskbarNotify(self, hwnd, msg, wparam, lparam):
61 if lparam == win32con.WM_LBUTTONUP:
62 self.onClick()
63 elif lparam == win32con.WM_LBUTTONDBLCLK:
64 self.onDoubleClick()
65 return 1
66
67 def onClick(self):
68 """Override in subclassess"""
69 pass
70
71 def onDoubleClick(self):
72 """Override in subclassess"""
73 pass
74
75
76 if __name__=='__main__':
77 class DemoTaskbar(Taskbar):
78
79 def __init__(self):
80 Taskbar.__init__(self)
81 icon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
82 message = "Hey Fritz!"
83 self.setIcon(icon,message)
84 self.show()
85
86 def onClick(self):
87 print "you clicked"
88
89 def onDoubleClick(self):
90 print "you double clicked, bye!"
91 win32gui.PostQuitMessage(0)
92
93 t = DemoTaskbar()
94 win32gui.PumpMessages() # start demo