Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

win32gui_taskbar.py 5.1KB

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