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.

iebutton.py 6.9KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. # -*- coding: latin-1 -*-
  2. # PyWin32 Internet Explorer Button
  3. #
  4. # written by Leonard Ritter (paniq@gmx.net)
  5. # and Robert Förtsch (info@robert-foertsch.com)
  6. """
  7. This sample implements a simple IE Button COM server
  8. with access to the IWebBrowser2 interface.
  9. To demonstrate:
  10. * Execute this script to register the server.
  11. * Open Pythonwin's Tools -> Trace Collector Debugging Tool, so you can
  12. see the output of 'print' statements in this demo.
  13. * Open a new IE instance. The toolbar should have a new "scissors" icon,
  14. with tooltip text "IE Button" - this is our new button - click it.
  15. * Switch back to the Pythonwin window - you should see:
  16. IOleCommandTarget::Exec called.
  17. This is the button being clicked. Extending this to do something more
  18. useful is left as an exercise.
  19. Contribtions to this sample to make it a little "friendlier" welcome!
  20. """
  21. # imports section
  22. import pythoncom
  23. import win32api
  24. import win32com
  25. import win32com.server.register
  26. from win32com import universal
  27. from win32com.client import Dispatch, DispatchWithEvents, constants, gencache, getevents
  28. # This demo uses 'print' - use win32traceutil to see it if we have no
  29. # console.
  30. try:
  31. win32api.GetConsoleTitle()
  32. except win32api.error:
  33. import win32traceutil
  34. import array
  35. from win32com.axcontrol import axcontrol
  36. # ensure we know the ms internet controls typelib so we have access to IWebBrowser2 later on
  37. win32com.client.gencache.EnsureModule("{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}", 0, 1, 1)
  38. #
  39. IObjectWithSite_methods = ["SetSite", "GetSite"]
  40. IOleCommandTarget_methods = ["Exec", "QueryStatus"]
  41. _iebutton_methods_ = IOleCommandTarget_methods + IObjectWithSite_methods
  42. _iebutton_com_interfaces_ = [
  43. axcontrol.IID_IOleCommandTarget,
  44. axcontrol.IID_IObjectWithSite, # IObjectWithSite
  45. ]
  46. class Stub:
  47. """
  48. this class serves as a method stub,
  49. outputting debug info whenever the object
  50. is being called.
  51. """
  52. def __init__(self, name):
  53. self.name = name
  54. def __call__(self, *args):
  55. print("STUB: ", self.name, args)
  56. class IEButton:
  57. """
  58. The actual COM server class
  59. """
  60. _com_interfaces_ = _iebutton_com_interfaces_
  61. _public_methods_ = _iebutton_methods_
  62. _reg_clsctx_ = pythoncom.CLSCTX_INPROC_SERVER
  63. _button_text_ = "IE Button"
  64. _tool_tip_ = "An example implementation for an IE Button."
  65. _icon_ = ""
  66. _hot_icon_ = ""
  67. def __init__(self):
  68. # put stubs for non-implemented methods
  69. for method in self._public_methods_:
  70. if not hasattr(self, method):
  71. print("providing default stub for %s" % method)
  72. setattr(self, method, Stub(method))
  73. def QueryStatus(self, pguidCmdGroup, prgCmds, cmdtextf):
  74. # 'cmdtextf' is the 'cmdtextf' element from the OLECMDTEXT structure,
  75. # or None if a NULL pointer was passed.
  76. result = []
  77. for id, flags in prgCmds:
  78. flags |= axcontrol.OLECMDF_SUPPORTED | axcontrol.OLECMDF_ENABLED
  79. result.append((id, flags))
  80. if cmdtextf is None:
  81. cmdtext = None # must return None if nothing requested.
  82. # IE never seems to want any text - this code is here for
  83. # demo purposes only
  84. elif cmdtextf == axcontrol.OLECMDTEXTF_NAME:
  85. cmdtext = "IEButton Name"
  86. else:
  87. cmdtext = "IEButton State"
  88. return result, cmdtext
  89. def Exec(self, pguidCmdGroup, nCmdID, nCmdExecOpt, pvaIn):
  90. print(pguidCmdGroup, nCmdID, nCmdExecOpt, pvaIn)
  91. print("IOleCommandTarget::Exec called.")
  92. # self.webbrowser.ShowBrowserBar(GUID_IETOOLBAR, not is_ietoolbar_visible())
  93. def SetSite(self, unknown):
  94. if unknown:
  95. # first get a command target
  96. cmdtarget = unknown.QueryInterface(axcontrol.IID_IOleCommandTarget)
  97. # then travel over to a service provider
  98. serviceprovider = cmdtarget.QueryInterface(pythoncom.IID_IServiceProvider)
  99. # finally ask for the internet explorer application, returned as a dispatch object
  100. self.webbrowser = win32com.client.Dispatch(
  101. serviceprovider.QueryService(
  102. "{0002DF05-0000-0000-C000-000000000046}", pythoncom.IID_IDispatch
  103. )
  104. )
  105. else:
  106. # lose all references
  107. self.webbrowser = None
  108. def GetClassID(self):
  109. return self._reg_clsid_
  110. def register(classobj):
  111. import winreg
  112. subKeyCLSID = (
  113. "SOFTWARE\\Microsoft\\Internet Explorer\\Extensions\\%38s"
  114. % classobj._reg_clsid_
  115. )
  116. try:
  117. hKey = winreg.CreateKey(winreg.HKEY_LOCAL_MACHINE, subKeyCLSID)
  118. subKey = winreg.SetValueEx(
  119. hKey, "ButtonText", 0, winreg.REG_SZ, classobj._button_text_
  120. )
  121. winreg.SetValueEx(
  122. hKey, "ClsidExtension", 0, winreg.REG_SZ, classobj._reg_clsid_
  123. ) # reg value for calling COM object
  124. winreg.SetValueEx(
  125. hKey, "CLSID", 0, winreg.REG_SZ, "{1FBA04EE-3024-11D2-8F1F-0000F87ABD16}"
  126. ) # CLSID for button that sends command to COM object
  127. winreg.SetValueEx(hKey, "Default Visible", 0, winreg.REG_SZ, "Yes")
  128. winreg.SetValueEx(hKey, "ToolTip", 0, winreg.REG_SZ, classobj._tool_tip_)
  129. winreg.SetValueEx(hKey, "Icon", 0, winreg.REG_SZ, classobj._icon_)
  130. winreg.SetValueEx(hKey, "HotIcon", 0, winreg.REG_SZ, classobj._hot_icon_)
  131. except WindowsError:
  132. print("Couldn't set standard toolbar reg keys.")
  133. else:
  134. print("Set standard toolbar reg keys.")
  135. def unregister(classobj):
  136. import winreg
  137. subKeyCLSID = (
  138. "SOFTWARE\\Microsoft\\Internet Explorer\\Extensions\\%38s"
  139. % classobj._reg_clsid_
  140. )
  141. try:
  142. hKey = winreg.CreateKey(winreg.HKEY_LOCAL_MACHINE, subKeyCLSID)
  143. subKey = winreg.DeleteValue(hKey, "ButtonText")
  144. winreg.DeleteValue(hKey, "ClsidExtension") # for calling COM object
  145. winreg.DeleteValue(hKey, "CLSID")
  146. winreg.DeleteValue(hKey, "Default Visible")
  147. winreg.DeleteValue(hKey, "ToolTip")
  148. winreg.DeleteValue(hKey, "Icon")
  149. winreg.DeleteValue(hKey, "HotIcon")
  150. winreg.DeleteKey(winreg.HKEY_LOCAL_MACHINE, subKeyCLSID)
  151. except WindowsError:
  152. print("Couldn't delete Standard toolbar regkey.")
  153. else:
  154. print("Deleted Standard toolbar regkey.")
  155. #
  156. # test implementation
  157. #
  158. class PyWin32InternetExplorerButton(IEButton):
  159. _reg_clsid_ = "{104B66A9-9E68-49D1-A3F5-94754BE9E0E6}"
  160. _reg_progid_ = "PyWin32.IEButton"
  161. _reg_desc_ = "Test Button"
  162. _button_text_ = "IE Button"
  163. _tool_tip_ = "An example implementation for an IE Button."
  164. _icon_ = ""
  165. _hot_icon_ = _icon_
  166. def DllRegisterServer():
  167. register(PyWin32InternetExplorerButton)
  168. def DllUnregisterServer():
  169. unregister(PyWin32InternetExplorerButton)
  170. if __name__ == "__main__":
  171. win32com.server.register.UseCommandLine(
  172. PyWin32InternetExplorerButton,
  173. finalize_register=DllRegisterServer,
  174. finalize_unregister=DllUnregisterServer,
  175. )