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.

activex.py 2.8KB

1 year ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """Support for ActiveX control hosting in Pythonwin.
  2. """
  3. import win32ui
  4. import win32uiole
  5. from . import window
  6. # XXX - we are still "classic style" classes in py2x, so we need can't yet
  7. # use 'type()' everywhere - revisit soon, as py2x will move to new-style too...
  8. try:
  9. from types import ClassType as new_type
  10. except ImportError:
  11. new_type = type # py3k
  12. class Control(window.Wnd):
  13. """An ActiveX control base class. A new class must be derived from both
  14. this class and the Events class. See the demos for more details.
  15. """
  16. def __init__(self):
  17. self.__dict__["_dispobj_"] = None
  18. window.Wnd.__init__(self)
  19. def _GetControlCLSID(self):
  20. return self.CLSID
  21. def _GetDispatchClass(self):
  22. return self.default_interface
  23. def _GetEventMap(self):
  24. return self.default_source._dispid_to_func_
  25. def CreateControl(self, windowTitle, style, rect, parent, id, lic_string=None):
  26. clsid = str(self._GetControlCLSID())
  27. self.__dict__["_obj_"] = win32ui.CreateControl(
  28. clsid, windowTitle, style, rect, parent, id, None, False, lic_string
  29. )
  30. klass = self._GetDispatchClass()
  31. dispobj = klass(win32uiole.GetIDispatchForWindow(self._obj_))
  32. self.HookOleEvents()
  33. self.__dict__["_dispobj_"] = dispobj
  34. def HookOleEvents(self):
  35. dict = self._GetEventMap()
  36. for dispid, methodName in dict.items():
  37. if hasattr(self, methodName):
  38. self._obj_.HookOleEvent(getattr(self, methodName), dispid)
  39. def __getattr__(self, attr):
  40. # Delegate attributes to the windows and the Dispatch object for this class
  41. try:
  42. return window.Wnd.__getattr__(self, attr)
  43. except AttributeError:
  44. pass
  45. return getattr(self._dispobj_, attr)
  46. def __setattr__(self, attr, value):
  47. if hasattr(self.__dict__, attr):
  48. self.__dict__[attr] = value
  49. return
  50. try:
  51. if self._dispobj_:
  52. self._dispobj_.__setattr__(attr, value)
  53. return
  54. except AttributeError:
  55. pass
  56. self.__dict__[attr] = value
  57. def MakeControlClass(controlClass, name=None):
  58. """Given a CoClass in a generated .py file, this function will return a Class
  59. object which can be used as an OCX control.
  60. This function is used when you do not want to handle any events from the OCX
  61. control. If you need events, then you should derive a class from both the
  62. activex.Control class and the CoClass
  63. """
  64. if name is None:
  65. name = controlClass.__name__
  66. return new_type("OCX" + name, (Control, controlClass), {})
  67. def MakeControlInstance(controlClass, name=None):
  68. """As for MakeControlClass(), but returns an instance of the class."""
  69. return MakeControlClass(controlClass, name)()