flash.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # By Bradley Schatz
  2. # simple flash/python application demonstrating bidirectional
  3. # communicaion between flash and python. Click the sphere to see
  4. # behavior. Uses Bounce.swf from FlashBounce.zip, available from
  5. # http://pages.cpsc.ucalgary.ca/~saul/vb_examples/tutorial12/
  6. # Update to the path of the .swf file (note it could be a true URL)
  7. flash_url = "c:\\bounce.swf"
  8. import win32ui, win32con, win32api, regutil
  9. from pywin.mfc import window, activex
  10. from win32com.client import gencache
  11. import sys
  12. FlashModule = gencache.EnsureModule("{D27CDB6B-AE6D-11CF-96B8-444553540000}", 0, 1, 0)
  13. if FlashModule is None:
  14. raise ImportError("Flash does not appear to be installed.")
  15. class MyFlashComponent(activex.Control, FlashModule.ShockwaveFlash):
  16. def __init__(self):
  17. activex.Control.__init__(self)
  18. FlashModule.ShockwaveFlash.__init__(self)
  19. self.x = 50
  20. self.y = 50
  21. self.angle = 30
  22. self.started = 0
  23. def OnFSCommand(self, command, args):
  24. print("FSCommend" , command, args)
  25. self.x = self.x + 20
  26. self.y = self.y + 20
  27. self.angle = self.angle + 20
  28. if self.x > 200 or self.y > 200:
  29. self.x = 0
  30. self.y = 0
  31. if self.angle > 360:
  32. self.angle = 0
  33. self.SetVariable("xVal", self.x)
  34. self.SetVariable("yVal", self.y)
  35. self.SetVariable("angle", self.angle)
  36. self.TPlay("_root.mikeBall")
  37. def OnProgress(self, percentDone):
  38. print("PercentDone", percentDone)
  39. def OnReadyStateChange(self, newState):
  40. # 0=Loading, 1=Uninitialized, 2=Loaded, 3=Interactive, 4=Complete
  41. print("State", newState)
  42. class BrowserFrame(window.MDIChildWnd):
  43. def __init__(self, url = None):
  44. if url is None:
  45. self.url = regutil.GetRegisteredHelpFile("Main Python Documentation")
  46. else:
  47. self.url = url
  48. pass # Dont call base class doc/view version...
  49. def Create(self, title, rect = None, parent = None):
  50. style = win32con.WS_CHILD | win32con.WS_VISIBLE | win32con.WS_OVERLAPPEDWINDOW
  51. self._obj_ = win32ui.CreateMDIChild()
  52. self._obj_.AttachObject(self)
  53. self._obj_.CreateWindow(None, title, style, rect, parent)
  54. rect = self.GetClientRect()
  55. rect = (0,0,rect[2]-rect[0], rect[3]-rect[1])
  56. self.ocx = MyFlashComponent()
  57. self.ocx.CreateControl("Flash Player", win32con.WS_VISIBLE | win32con.WS_CHILD, rect, self, 1000)
  58. self.ocx.LoadMovie(0,flash_url)
  59. self.ocx.Play()
  60. self.HookMessage (self.OnSize, win32con.WM_SIZE)
  61. def OnSize (self, params):
  62. rect = self.GetClientRect()
  63. rect = (0,0,rect[2]-rect[0], rect[3]-rect[1])
  64. self.ocx.SetWindowPos(0, rect, 0)
  65. def Demo():
  66. url = None
  67. if len(sys.argv)>1:
  68. url = win32api.GetFullPathName(sys.argv[1])
  69. f = BrowserFrame(url)
  70. f.Create("Flash Player")
  71. if __name__=='__main__':
  72. Demo()