Question

I want my BHO to listen to onmousedown event of some element in a certain webpage. I have all the code that find the specific element, and in msdn its says that I need to use the get_onmousedown event. I came up with this code.

 CComQIPtr<IHTMLElement> someElement;
 VARIANT mouse_eve;
 someElement->get_onmousedown(&mouse_eve);

The question is, how do I tell it to run some function when this event occurs?

Was it helpful?

Solution

v - VARIANT of type VT_DISPATCH that specifies the IDispatch interface of an object with a default method that is invoked when the event occurs.

Event handlers in this context are COM instances that implement IDispatch - so you need to pass a pointer to an IDispatch that your event handler object implements:

CComQIPtr<IDispatch> spDisp(spMyHandler); // something like that
someElement->put_onmousedown(CComVariant(spDisp));

Note: put_ instead of get_ - you want to register a handler.

On this, IDispatch::Invoke() gets called with:

  • wFlags containing DISPATCH_METHOD ("a method is getting invoked")
  • dispIdMember being 0 / DISPID_VALUE ("the default method")

Put together this should become something like:

HRESULT MyHandler::Invoke(DISPID dispIdMember, REFIID, LCID, WORD wFlags, 
                          DISPPARAMS*, VARIANT*, EXCEPINFO*, UINT*)
{
    // ...

    if((wFlags & DISPATCH_METHOD) && (dispIdMember == DISPID_VALUE)) 
    {
        // ...
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top