[python-win32] COM Events

Tim Golden tim.golden at viacom-outdoor.co.uk
Fri Jul 9 10:14:34 CEST 2004


| In VB one way to write the code would be something like this:
| 
| """
| Dim WithEvents gMyApp As XYZ.Application
| 
| Private Sub gMyApp_OnQuit()
| 	'create application specific OnQuit event handler here..
| End Sub
| """
| 
| Where the event handler HAS to be called '<object>_<event>'
| 
| I can obviously make sure that I have a function with the 
| correct name - but
| HOW do I do the WithEvents bit!!??

You want to use DispatchWithEvents. You use it like
the normal Dispatch, but pass in additionally a class
(*not* an instance) which contains methods corresponding
to the events you're interested in.

The following code fragment should illustrate what I mean
(from when I was playing around with Microsoft Agent):

<code>

class AgentEvents:
  def OnActivateInput (self, character_id):
    this_agent = active_agents.get (character_id)
    if this_agent: this_agent.on_activate_input ()

  def OnActiveClientChange (self, character_id, active):
    this_agent = active_agents.get (character_id)
    if this_agent: this_agent.on_activate_client_change (active)

  def OnAgentPropertyChange (self):
    pass

agent = win32com.client.DispatchWithEvents ("Agent.Control.2", AgentEvents)

</code>

TJG


________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________


More information about the Python-win32 mailing list