win32gui_demo.py 4.2 KB

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