How can I attach a .NET delegate to the mshtml event?

I am working on an extension (BHO) for Internet Explorer and am having difficulty attaching a .NET delegate to mshtml objects to detect DOM events.

I've tried using events posted using the IHTMLElementEvents2_Event, ... interfaces and this works, but only if I specify the correct type for the DOM element whose events I want to catch. I also need to specify the type of the element in code, and this method does not allow catching custom DOM events.

I also tried using the HtmlEventProxy class , which attaches IDispatch objects to DOM elements using the IHTMLElement2.attachEvent method, but that doesn't work for me at all. When I try to access the event object as described in this thread , I get a hang when accessing the document property of the DOM element.

So how can I attach event handlers to DOM elements without requiring a special case for every element type and every kind of event?

Thank.

0


source to share


1 answer


You can try this code:



[ComVisible(true)]
public class Foo
{
  public Foo(HtmlDocument doc) 
  {
    IHTMLDocument2 doc2 = (IHTMLDocument2)doc.DomDocument;
    doc2.onkeydown = this;
  }

  [System.Runtime.InteropServices.DispId(0)]
  public void EventHandler()
  {
    IHTMLWindow2 win2 = (IHTMLWindow2)_doc.Window.DomWindow;
    IHTMLEventObj e = win2.@event;

    if (e.keyCode == (int)Keys.F5)
    {
      // ...
    }
  }
}

      

+2


source







All Articles