win32gui_taskbar.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # Creates a task-bar icon. Run from Python.exe to see the
  2. # messages printed.
  3. import win32api, win32gui
  4. import win32con, winerror
  5. import sys, os
  6. class MainWindow:
  7. def __init__(self):
  8. msg_TaskbarRestart = win32gui.RegisterWindowMessage("TaskbarCreated");
  9. message_map = {
  10. msg_TaskbarRestart: self.OnRestart,
  11. win32con.WM_DESTROY: self.OnDestroy,
  12. win32con.WM_COMMAND: self.OnCommand,
  13. win32con.WM_USER+20 : self.OnTaskbarNotify,
  14. }
  15. # Register the Window class.
  16. wc = win32gui.WNDCLASS()
  17. hinst = wc.hInstance = win32api.GetModuleHandle(None)
  18. wc.lpszClassName = "PythonTaskbarDemo"
  19. wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW;
  20. wc.hCursor = win32api.LoadCursor( 0, win32con.IDC_ARROW )
  21. wc.hbrBackground = win32con.COLOR_WINDOW
  22. wc.lpfnWndProc = message_map # could also specify a wndproc.
  23. # Don't blow up if class already registered to make testing easier
  24. try:
  25. classAtom = win32gui.RegisterClass(wc)
  26. except win32gui.error as err_info:
  27. if err_info.winerror!=winerror.ERROR_CLASS_ALREADY_EXISTS:
  28. raise
  29. # Create the Window.
  30. style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
  31. self.hwnd = win32gui.CreateWindow( wc.lpszClassName, "Taskbar Demo", style, \
  32. 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, \
  33. 0, 0, hinst, None)
  34. win32gui.UpdateWindow(self.hwnd)
  35. self._DoCreateIcons()
  36. def _DoCreateIcons(self):
  37. # Try and find a custom icon
  38. hinst = win32api.GetModuleHandle(None)
  39. iconPathName = os.path.abspath(os.path.join( os.path.split(sys.executable)[0], "pyc.ico" ))
  40. if not os.path.isfile(iconPathName):
  41. # Look in DLLs dir, a-la py 2.5
  42. iconPathName = os.path.abspath(os.path.join( os.path.split(sys.executable)[0], "DLLs", "pyc.ico" ))
  43. if not os.path.isfile(iconPathName):
  44. # Look in the source tree.
  45. iconPathName = os.path.abspath(os.path.join( os.path.split(sys.executable)[0], "..\\PC\\pyc.ico" ))
  46. if os.path.isfile(iconPathName):
  47. icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
  48. hicon = win32gui.LoadImage(hinst, iconPathName, win32con.IMAGE_ICON, 0, 0, icon_flags)
  49. else:
  50. print("Can't find a Python icon file - using default")
  51. hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
  52. flags = win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP
  53. nid = (self.hwnd, 0, flags, win32con.WM_USER+20, hicon, "Python Demo")
  54. try:
  55. win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid)
  56. except win32gui.error:
  57. # This is common when windows is starting, and this code is hit
  58. # before the taskbar has been created.
  59. print("Failed to add the taskbar icon - is explorer running?")
  60. # but keep running anyway - when explorer starts, we get the
  61. # TaskbarCreated message.
  62. def OnRestart(self, hwnd, msg, wparam, lparam):
  63. self._DoCreateIcons()
  64. def OnDestroy(self, hwnd, msg, wparam, lparam):
  65. nid = (self.hwnd, 0)
  66. win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid)
  67. win32gui.PostQuitMessage(0) # Terminate the app.
  68. def OnTaskbarNotify(self, hwnd, msg, wparam, lparam):
  69. if lparam==win32con.WM_LBUTTONUP:
  70. print("You clicked me.")
  71. elif lparam==win32con.WM_LBUTTONDBLCLK:
  72. print("You double-clicked me - goodbye")
  73. win32gui.DestroyWindow(self.hwnd)
  74. elif lparam==win32con.WM_RBUTTONUP:
  75. print("You right clicked me.")
  76. menu = win32gui.CreatePopupMenu()
  77. win32gui.AppendMenu( menu, win32con.MF_STRING, 1023, "Display Dialog")
  78. win32gui.AppendMenu( menu, win32con.MF_STRING, 1024, "Say Hello")
  79. win32gui.AppendMenu( menu, win32con.MF_STRING, 1025, "Exit program" )
  80. pos = win32gui.GetCursorPos()
  81. # See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/menus_0hdi.asp
  82. win32gui.SetForegroundWindow(self.hwnd)
  83. win32gui.TrackPopupMenu(menu, win32con.TPM_LEFTALIGN, pos[0], pos[1], 0, self.hwnd, None)
  84. win32gui.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0)
  85. return 1
  86. def OnCommand(self, hwnd, msg, wparam, lparam):
  87. id = win32api.LOWORD(wparam)
  88. if id == 1023:
  89. import win32gui_dialog
  90. win32gui_dialog.DemoModal()
  91. elif id == 1024:
  92. print("Hello")
  93. elif id == 1025:
  94. print("Goodbye")
  95. win32gui.DestroyWindow(self.hwnd)
  96. else:
  97. print("Unknown command -", id)
  98. def main():
  99. w=MainWindow()
  100. win32gui.PumpMessages()
  101. if __name__=='__main__':
  102. main()