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_demo.py 4.9KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # The start of a win32gui generic demo.
  2. # Feel free to contribute more demos back ;-)
  3. import math
  4. import random
  5. import time
  6. import win32api
  7. import win32con
  8. import win32gui
  9. def _MyCallback(hwnd, extra):
  10. hwnds, classes = extra
  11. hwnds.append(hwnd)
  12. classes[win32gui.GetClassName(hwnd)] = 1
  13. def TestEnumWindows():
  14. windows = []
  15. classes = {}
  16. win32gui.EnumWindows(_MyCallback, (windows, classes))
  17. print(
  18. "Enumerated a total of %d windows with %d classes"
  19. % (len(windows), len(classes))
  20. )
  21. if "tooltips_class32" not in classes:
  22. print("Hrmmmm - I'm very surprised to not find a 'tooltips_class32' class.")
  23. def OnPaint_1(hwnd, msg, wp, lp):
  24. dc, ps = win32gui.BeginPaint(hwnd)
  25. win32gui.SetGraphicsMode(dc, win32con.GM_ADVANCED)
  26. br = win32gui.CreateSolidBrush(win32api.RGB(255, 0, 0))
  27. win32gui.SelectObject(dc, br)
  28. angle = win32gui.GetWindowLong(hwnd, win32con.GWL_USERDATA)
  29. win32gui.SetWindowLong(hwnd, win32con.GWL_USERDATA, angle + 2)
  30. r_angle = angle * (math.pi / 180)
  31. win32gui.SetWorldTransform(
  32. dc,
  33. {
  34. "M11": math.cos(r_angle),
  35. "M12": math.sin(r_angle),
  36. "M21": math.sin(r_angle) * -1,
  37. "M22": math.cos(r_angle),
  38. "Dx": 250,
  39. "Dy": 250,
  40. },
  41. )
  42. win32gui.MoveToEx(dc, 250, 250)
  43. win32gui.BeginPath(dc)
  44. win32gui.Pie(dc, 10, 70, 200, 200, 350, 350, 75, 10)
  45. win32gui.Chord(dc, 200, 200, 850, 0, 350, 350, 75, 10)
  46. win32gui.LineTo(dc, 300, 300)
  47. win32gui.LineTo(dc, 100, 20)
  48. win32gui.LineTo(dc, 20, 100)
  49. win32gui.LineTo(dc, 400, 0)
  50. win32gui.LineTo(dc, 0, 400)
  51. win32gui.EndPath(dc)
  52. win32gui.StrokeAndFillPath(dc)
  53. win32gui.EndPaint(hwnd, ps)
  54. return 0
  55. wndproc_1 = {win32con.WM_PAINT: OnPaint_1}
  56. def OnPaint_2(hwnd, msg, wp, lp):
  57. dc, ps = win32gui.BeginPaint(hwnd)
  58. win32gui.SetGraphicsMode(dc, win32con.GM_ADVANCED)
  59. l, t, r, b = win32gui.GetClientRect(hwnd)
  60. for x in range(25):
  61. vertices = (
  62. {
  63. "x": int(random.random() * r),
  64. "y": int(random.random() * b),
  65. "Red": int(random.random() * 0xFF00),
  66. "Green": 0,
  67. "Blue": 0,
  68. "Alpha": 0,
  69. },
  70. {
  71. "x": int(random.random() * r),
  72. "y": int(random.random() * b),
  73. "Red": 0,
  74. "Green": int(random.random() * 0xFF00),
  75. "Blue": 0,
  76. "Alpha": 0,
  77. },
  78. {
  79. "x": int(random.random() * r),
  80. "y": int(random.random() * b),
  81. "Red": 0,
  82. "Green": 0,
  83. "Blue": int(random.random() * 0xFF00),
  84. "Alpha": 0,
  85. },
  86. )
  87. mesh = ((0, 1, 2),)
  88. win32gui.GradientFill(dc, vertices, mesh, win32con.GRADIENT_FILL_TRIANGLE)
  89. win32gui.EndPaint(hwnd, ps)
  90. return 0
  91. wndproc_2 = {win32con.WM_PAINT: OnPaint_2}
  92. def TestSetWorldTransform():
  93. wc = win32gui.WNDCLASS()
  94. wc.lpszClassName = "test_win32gui_1"
  95. wc.style = win32con.CS_GLOBALCLASS | win32con.CS_VREDRAW | win32con.CS_HREDRAW
  96. wc.hbrBackground = win32con.COLOR_WINDOW + 1
  97. wc.lpfnWndProc = wndproc_1
  98. class_atom = win32gui.RegisterClass(wc)
  99. hwnd = win32gui.CreateWindow(
  100. wc.lpszClassName,
  101. "Spin the Lobster!",
  102. win32con.WS_CAPTION | win32con.WS_VISIBLE,
  103. 100,
  104. 100,
  105. 900,
  106. 900,
  107. 0,
  108. 0,
  109. 0,
  110. None,
  111. )
  112. for x in range(500):
  113. win32gui.InvalidateRect(hwnd, None, True)
  114. win32gui.PumpWaitingMessages()
  115. time.sleep(0.01)
  116. win32gui.DestroyWindow(hwnd)
  117. win32gui.UnregisterClass(wc.lpszClassName, None)
  118. def TestGradientFill():
  119. wc = win32gui.WNDCLASS()
  120. wc.lpszClassName = "test_win32gui_2"
  121. wc.style = win32con.CS_GLOBALCLASS | win32con.CS_VREDRAW | win32con.CS_HREDRAW
  122. wc.hbrBackground = win32con.COLOR_WINDOW + 1
  123. wc.lpfnWndProc = wndproc_2
  124. class_atom = win32gui.RegisterClass(wc)
  125. hwnd = win32gui.CreateWindowEx(
  126. 0,
  127. class_atom,
  128. "Kaleidoscope",
  129. win32con.WS_CAPTION
  130. | win32con.WS_VISIBLE
  131. | win32con.WS_THICKFRAME
  132. | win32con.WS_SYSMENU,
  133. 100,
  134. 100,
  135. 900,
  136. 900,
  137. 0,
  138. 0,
  139. 0,
  140. None,
  141. )
  142. s = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
  143. win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, s | win32con.WS_EX_LAYERED)
  144. win32gui.SetLayeredWindowAttributes(hwnd, 0, 175, win32con.LWA_ALPHA)
  145. for x in range(30):
  146. win32gui.InvalidateRect(hwnd, None, True)
  147. win32gui.PumpWaitingMessages()
  148. time.sleep(0.3)
  149. win32gui.DestroyWindow(hwnd)
  150. win32gui.UnregisterClass(class_atom, None)
  151. print("Enumerating all windows...")
  152. TestEnumWindows()
  153. print("Testing drawing functions ...")
  154. TestSetWorldTransform()
  155. TestGradientFill()
  156. print("All tests done!")