Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
Markdown Monster - The Markdown Editor for Windows

Handling mshtml Document Events without Mouse lockups


:P
On this page:

I’ve been working on a session I’m doing at the Dutch Conference To the Max conference for using the Web Browser control in .Net. For the most part the process of using this control is pretty straight forward – you import the ActiveX control and fire away at the browser events.


If you want to work with the document you can also import the Microsoft.mshtml library which is a 7meg  monster that gives you access to the entire HTML object model and event system. For the most part it works pretty well so you can do something like this quite easily:

 

HTMLDocument Doc = this.Browser.Document as mshtml.HTMLDocument;

string DocBody = Doc.body.outerHTML;

 

You can drill into the various collections, pick up selections and modify and fire away at the document model just fine.

 

However, I’ve had major issues with the document events. It’s easy enough to capture the events of the document like this:

 

HTMLDocumentEvents2_Event DocEvents = this.Browser.Document as HTMLDocumentEvents2_Event;

if (DocEvents == null)

      return false;

 

DocEvents.oncontextmenu += new

              HTMLDocumentEvents2_oncontextmenuEventHandler(Browser_ContextMenu);

 

All of this event information and the constants come as part of mshtml and it works to hook up the events. However, it comes with a major side effect – when you hook an event of the document in this fashion the document itself stops responding to mouse clicks. All other operations stay live, you can still tab to different areas and other events such as status bar events fire just fine. But you cannot click for instance on one of the links (you can click the mouse on the link then press enter, but you can’t click with your mouse). Obviously this is hardly an acceptable solution.

 

The workaround for this bug is to create a separate Event object that handles the event and then forwards it to a custom handler directly, by passing the Web Browser control. This concept is based on the fact that all IE Document events are fired exactly same way using an IHTMLEventObj object that is passed to an event handling method. All events such as OnMouseDown, OnContextMenu, OnFocus etc. all receive such a parameter which contains information such as mouse status, screen position and a return value property that you can set.

 

With this knowledge it’s possible to create a generic event object that receives these events and allows you to hook up your own handlers. This is a little more work than the built-in handlers but it works and keeps your document alive.

 

The Event Handler class and delegate  look like this:

///

/// Generic HTML DOM Event method handler.

///

public delegate void DHTMLEvent(IHTMLEventObj e);

 

///

/// Generic Event handler for HTML DOM objects.

/// Handles a basic event object which receives an IHTMLEventObj which

/// applies to all document events raised.

///

public class DHTMLEventHandler

{

      public DHTMLEvent Handler;

      HTMLDocument Document;

 

      public DHTMLEventHandler(HTMLDocument doc)

      {

            this.Document=doc;

      }

      [DispId(0)]

      public void Call()

      {

            Handler(Document.parentWindow.@event);

      }

}

 

Remember that the Document reloads on every hit, so in order to use the above handler all the time you have to re-hook it everytime the page is navigated. Typically this means that these sort of handler are hooked up as part of the Browser’s DocumentComplete handler.

 

The following hooks up the OnContextMenu event of the Document:

 

// *** Always raise the ContextMenuClicked event

// *** Using Custom Event Object  -   No Mouse Lockups

HTMLDocument doc = this.Document as HTMLDocument ;

DHTMLEventHandler Handler = new DHTMLEventHandler( doc );

Handler.Handler += new DHTMLEvent(this.Browser_ContextMenuStandardEvent);

doc.oncontextmenu = Handler;

 

The handler method then looks like this:

 

private void Browser_ContextMenuStandardEvent(mshtml.IHTMLEventObj e)

{

      MessageBox.Show("Context Menu Action (Event Object) Hooked");

      e.returnValue  = false;

}

 

Note that you can use the exact same signature and hookup code for any event of the HTML Document and any child elements in this same fashion. So if you want to capture a focus event for a DIV region inside of the document all you have to do is hook up the Element’s Onfocus event and assign the EventHandler to it the same way.

 

I hope this helps somebody out, because it took me quite some time to figure out this little tidbit. Actually I got a bit of help. The base idea came from an e-Book:

 

Programming Microsoft Internet Exporer in C# (Nikit Zykov – ebook):

http://www.amazon.com/exec/obidos/ASIN/B00007FYZP/westwindtechn-20/102-6053556-6633703

 

This $20 download e-book is a good read if you plan on using the browser control although it shows a very specific way of dealing with the control even though there are a number of approaches available. I simplified the code somewhat to make it more self-contained to just that small snippet above. The e-Book goes on to create a Document wrapper class that provides a bunch of common functionality.

 

I’m working on an article for this stuff, and I'll get that out when I get a little more time to clean up my notes.


The Voices of Reason


 

Clyde Davies
May 07, 2004

# re: Handling mshtml Document Events without Mouse lockups

I tried to get a copy of the book you mention but it is no longer available. Do you have any idea where I could find a copy?

Rick Strahl
May 09, 2004

# re: Handling mshtml Document Events without Mouse lockups

It should be available on Amazon as an E-Book. I got mine only a few weeks ago?

Clyde Davies
May 14, 2004

# re: Handling mshtml Document Events without Mouse lockups

Well, the link you post doesn't seem to work. I've tried looking for it elsewhere as well. Bugger: I could really make use of this book!

Rick Strahl
May 22, 2004

# re: Handling mshtml Document Events without Mouse lockups

Yeah, looks like the book is no longer available at least at Amazon. Search by the title and see if you can find it at a different book seller.

YK
June 17, 2004

# System.NotImplementedException occurs

Hi. This tutorial looks useful, and I tried using it however I got an error.
In the line below,
"doc.oncontextmenu = Handler;"
I get a System.NotImplementedException as follows. Do you have any idea what is going on?


System.NotImplementedException: Not implemented

at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at mshtml.HTMLDocumentClass.set_onclick(Object )

-YK

Rick Strahl
June 17, 2004

# re: Handling mshtml Document Events without Mouse lockups

Yk... probably an ancient version of Internet Explorer. The version must implement HtmlDocument2 at least, but I'm not sure with what version that happened (I think 5.5).

Lance Cramlet
June 26, 2004

# re: Handling mshtml Document Events without Mouse lockups

First off, your sample code was very helpful. Everything has been working fine until now. I'm trying to hook an onclick event that already has another event associated with it(simple javascript that spawns another window). My problem is that 'doc.onclick = Handler;' overwrites the event instead of appending it.

Any clue how to modify your handler code to support this?

-Lance

Rick Strahl
June 26, 2004

# re: Handling mshtml Document Events without Mouse lockups

I don't think you can do this because the IE event model doesn't allow for event chaining like delegates do...

Kulvinder Maingi
June 29, 2004

# re: Handling mshtml Document Events without Mouse lockups

For those who are looking for the e-book, I found the text in an article which is still available at www.asptoday.com. You'll have to subscribe though.

K

Alger
July 04, 2004

# re: Handling mshtml Document Events without Mouse lockups

Perhaps you could give a hint on how to use SHDocVw.InternetExplorer rather than the AxSHDocVw control? I'd like to see how to modify your Handler to connect to the IE.

-Alger

Rick Strahl
July 04, 2004

# re: Handling mshtml Document Events without Mouse lockups

The procedure should be the same once you have a reference to the document object. I haven't played with that so I'm not sure what object you need to import but since hte doc object references the same MSHTML interfaces the rest would be the same.

zmijasu
September 17, 2004

# re: Handling mshtml Document Events without Mouse lockups

I got the code working for the SHDocVw.InternetExplorer but It doesn't seem to be working the way it is supposed to, that is the mouse still locks up. I am able to chow my own context menu but I can't hide it when I click somewhere else on the screen because of the lock up. It works the same as with regular mshtml handlers. Any suggestions?

Thanks in advance.

zmijasu

Rick Strahl
September 17, 2004

# re: Handling mshtml Document Events without Mouse lockups

Make sure you don't have the old handlers still hooked up. It sounds like you might be overwriting the handler with the old style event hookup.

pam
September 23, 2004

# re: Handling mshtml Document Events without Mouse lockups

How can I catch changes of input textboxes on the web brower control?

Rick Strahl
September 23, 2004

# re: Handling mshtml Document Events without Mouse lockups

You have to use the sasme approach outlined above but assign to the events of the Text box object. I'm not sure offhand which event you'd need to hook but the approach is exactly the same.

Corey Innis
October 28, 2004

# re: Handling mshtml Document Events without Mouse lockups

Regarding Lance's desire to chain event handlers for onclick, I was able to do so by changing
e.returnValue = false;
to
e.returnValue = true;

I'm not sure how this would affect other events, but I imagine that you could tailor the return value on a per-event-type basis.

Thanks *very* much for this blog post... it's the only thing I've found so far (from many Google searches) that solved my problem.

Cheers,
Corey

Aaron
November 16, 2004

# re: Handling mshtml Document Events without Mouse lockups

I'm having an issue where my app that I wrote in XP doesn't work with Windows 2000. I don't seem to get the context menu event. I made sure that the computer had the lastest service pack. Anybody else had that issue?

By the way, this article rocks. I'll have to agree with Corey. Thanks.

Aaron
November 29, 2004

# re: Handling mshtml Document Events without Mouse lockups

Nevermind, looks like the problem was simply that the client computers didn't have a copy of Microsoft.mshtml.dll.

Peer Neumann
January 13, 2005

# re: Handling mshtml Document Events without Mouse lockups

This works well, but if you don't use an AxSHDocVw.WebBrowser but an SHDocVw.InternetExplorer the document's parentWindow is not accessable. You get an

System.InvalidCastException: No such interface supported.

Has anyony an idea howto get the Handler to work correctly!

My InternetExplorer Version is 6.0.2900...

I would like to have the Explorer opening a new Window that looks and feels like (and is)the InternetExplorer where I can capture some events.

Thank you for this article it is the only one I found in this topic.

Tomasz
January 27, 2005

# re: Handling mshtml Document Events without Mouse lockups

Everything above works perfectly well.
Unfortunately, in my case, when document contains iframe elements, all the events from within them are blocked.

The only event i can see is when the selection is changed to the iframe element. No further events come from iframes.

Have you possibly got any suggestions?

Dam
February 21, 2005

# re: Handling mshtml Document Events without Mouse lockups

In document element there is property 'parentWindow'. In window object there are also some events.

But in your case, there is collection 'frames' which keeps references to all frame elements on current document. Window object also has property 'document' so you can switch from 'window' object to 'document' object.

In order to get events from all frame elements you should do the following:

1. Assign your event handlers to the 'document' that you get from your 'axBrowser'.
2. Then go to the 'document.parentWindow' and assign event handlers to the window object that hosts document object.
3. Step through 'document.parentWindow.frames' collection and get window objects for all frames on page.
4. Assign event handlers to the 'window.document' object and 'window' object.
5. Repeat from step 3 to 5 for every window element that you get from 'frames' collection.

This is recursive process because each frame can host HTML document that has frames and in order to get events from all frames you have to assign event handlers to every frame.

Keep in mind that you have to do this procedure AFTER page loads. (axBrowser.DocumentComplete event)

Geraint Edwards
February 22, 2005

# re: Handling mshtml Document Events without Mouse lockups

I'm glad I found this - I'd been bashing my head against a brick wall since my mousedown event was blocking a click event on an anchor element!

Thanks.

Tomasz
March 01, 2005

# re: Handling mshtml Document Events without Mouse lockups

Thank you, Dam, for your suggestion. I followed your advice, however it didn't work anyway.
I mean I'm able to subscribe for the iframes events, but I'm still not able to catch the events
from the iframes.

Here's a piece of code i use to subscribe to the iframe events (steps 3 to 5)
I'm interested in only some of the document events, so i don't subscribe to the window events.
Of course i do call it from axBrowser.DocumentComplete event handler.


private void BindIFrameEvents (HtmlWindow2 window)
{
HtmlFramesCollection col = window.frames;
for (int i = 0; i < col.length; i++)
{
object index = i;
HtmlWindow2 win = (HtmlWindow2) col.item(ref index); // can't do otherwise
((mshtml.DispHTMLDocument)win.document).oncontextmenu = this; // let the method with [DispId(0)]
// to handle all the events
((mshtml.DispHTMLDocument)win.document).ondblclick = this;
this.BindIFrameEvents(win); // call recursively
}
}

Is there anything i do wrong there ?

Trupti
March 21, 2005

# re: Handling mshtml Document Events without Mouse lockups

Hi,
This is really helpful.
But i m facing the another problem.When i load the word document or excel document these hooked event doesnt work.
Is there any solution to my problem?
Thanks

wingnet
September 01, 2005

# memory leak

great code, it is very helpful.
I designed an IE band to track the click action on pages. the way of handling click event is just the method Rick showed me. It works, but I think this way could causes memory leak.
the symptom is that open a web page, and pop up new windows by clicking links in the page, so using memory increases, but it can be released by closing popup windows, only after the original page is closed, all memory could be released.
I guess the reason is the .net runtime and the COM object such as webcontrols hold reference of each other.
I don't know whether there is any good solutions.

thanks.

Chad
October 04, 2005

# re: Handling mshtml Document Events without Mouse lockups

Any Idea on how to do this for vb.net? Thanks a lot

Isaac Lee
November 04, 2005

# re: System.NotImplementedException occurs

For YK at #559,
I'm pretty sure you had your class DHTMLEventHandler as non public.
That class must be denoted "public" no matter where you put it.
I had the same problem and now it's fixed.
God bless you.

Gregg R
January 10, 2006

# re: Handling mshtml Document Events without Mouse lockups

VERY HELPFUL site... thank you very much Rick...I have been struggling with this for two days...

nsrajesh
January 24, 2006

# re: Handling mshtml Document Events without Mouse lockups

Friends
I am a newbie to C#.Net. Now i have todo a project that selects a dropdown listbox value and click a button. Here both of these things are placed in "iframe". As a newbie i cant able to find a solution for. So please help me. If you can please give me the full source code (from the begning of the function).

Thanks

Haris
February 03, 2006

# re: Handling mshtml Document Events without Mouse lockups

I am getting the same error as YK. System.NotImplementedException and I have all my classes as public. Any ideas ???

Oscar Núñez
March 14, 2006

# re: Handling mshtml Document Events without Mouse lockups

Hello, firs give you thanks because your example has been a big help for me.
But now I have a problem and haven't no idea for solve it, I explaint it.
I have a Web page and this page already have a function associated with the onsubmit...
how can I add my function at the existent, I want execute the two, the mine an the original.

Thanks in advance from Barcelona, Spain.

Thomas Geller
March 20, 2006

# re: Handling mshtml Document Events without Mouse lockups

To getting rid of the System.NotImplementedException mark the DHTMLEventHandler class as visible for Com:

[ComVisible(true)]
public class DHTMLEventHandler

Hooking events works but I'm not shure if this is useable since I override the default event handler. E.g. hooking into the documents onkeydown event leads to the result that no key is handled by the document itself (input fields etc.)


Matt Bush
April 05, 2006

# re: VB.NET equivalent appears not to work ... can you see why?

VB.NET equivalent appears not to work, can you suggest why this would be? As in the event handler routines are never fired.

Relevant code segments shown below:

'
' Generic HTML DOM Event method handler.
'
Public Delegate Sub DHTMLEvent(ByVal e As IHTMLEventObj)

'
' Generic Event handler for HTML DOM objects.
' Handles a basic event object which receives an IHTMLEventObj which
' applies to all document events raised.
'
<ComVisible(True)> _
Public Class DHTMLEventHandler

Public Handler As DHTMLEvent

Private Document As HTMLDocument

Public Sub New(ByVal doc As HTMLDocument)
MyBase.New()
Me.Document = doc
End Sub

<DispId(0)> _
Public Sub [Call]()
Handler(Document.parentWindow.event)
End Sub
End Class


Dim doc As HTMLDocument = CType(AxWebBrowser1.Document, HTMLDocument)

'
' OnKeyUp handler ...
'
Dim HandlerOnKeyUp As DHTMLEventHandler = New DHTMLEventHandler(doc)
HandlerOnKeyUp.Handler = New DHTMLEvent(AddressOf Me.DocumentEvents_onkeyup)
doc.onkeyup = HandlerOnKeyUp

'
' OnKeyUp handler ...
'
Dim HandlerOnContextMenu As DHTMLEventHandler = New DHTMLEventHandler(doc)
HandlerOnContextMenu.Handler = New DHTMLEvent(AddressOf Me.Browser_ContextMenuStandardEvent)
doc.oncontextmenu = HandlerOnContextMenu


Rick Strahl
April 06, 2006

# re: Handling mshtml Document Events without Mouse lockups

No, but I remember actually working on this a long while back for a customer who was also using VB, and I also couldn't get the VB events to hookup properly. I HAD TO use C# to make this work.

Nahla
April 16, 2006

# re: Handling mshtml Document Events without Mouse lockups

I tried to use the code but in (onClick) event
but it didn't work, here is the code

Handler.Handler += new DHTMLEvent(ClickEventHandler);
doc.onclick =Handler;

plzzz reply
thanx in advance

usman
April 21, 2006

# re: Handling mshtml Document Events without Mouse lockups

mshtml.HTMLDocument doc;
doc=(mshtml.HTMLDocument)this.axWebBrowser1.Document;
mshtml.HTMLDocumentEvents2_Event iEvent;
iEvent = (mshtml.HTMLDocumentEvents2_Event)doc;
iEvent.onclick+=new HTMLDocumentEvents2_onclickEventHandler(iEvent_onclick);
iEvent.onmouseover +=new HTMLDocumentEvents2_onmouseoverEventHandler(iEvent_onmouseover);

Leandro Koiti
May 31, 2006

# re: Handling mshtml Document Events without Mouse lockups

Well... I'm having a bit of trouble with a button... when I send a click message to the button it simply don't fire... if I remove the "UseSubmitBehavior" from the button the click message is sent and the onclick event is fired, but I just can't enable this... because the code must not do the post back... when I try to call the javascript right from the window it crashes...

Suparna
June 09, 2006

# re: Handling mshtml Document Events without Mouse lockups

Hi Rick, i tried ur code for handling mouse lockups but i'm unable to fire the event..
This is my code:
public delegate bool DHTMLEvent(IHTMLEventObj e);
public class DHTMLEventHandler
{
public DHTMLEvent Handler;
HTMLDocument Document;

public DHTMLEventHandler(HTMLDocument doc)
{
this.Document = doc;
}

[DispId(0)]
public void Call()
{
Handler(Document.parentWindow.@event);
}
}

HTMLDocument doc = (HTMLDocument)activeBrowserObj.Document;
DHTMLEventHandler Handler = new DHTMLEventHandler(doc);
Handler.Handler += new DHTMLEvent(ClickEventHandler);
((DispHTMLDocument)doc).onclick = Handler;

dbroome
June 12, 2006

# re: Handling mshtml Document Events without Mouse lockups

Anyone have a working sample app with this implemented? When I implement, it SEEMS to work, but then never passes any document events to my code.

STaylor
June 16, 2006

# re: Handling mshtml Document Events without Mouse lockups

Great post thanks, managed to get the VB version working for anyone interested under VS2005

Imports mshtml

Public Delegate Sub DHTMLEvent(ByVal e As IHTMLEventObj)

<Runtime.InteropServices.ComVisible(True)> _
Public Class DHTMLEventHandler
Public Handler As DHTMLEvent
Private Document As HTMLDocument

Public Sub New(ByVal doc As HTMLDocument)
Me.Document = doc
End Sub

<Runtime.InteropServices.DispId(0)> _
Public Sub [Call]()
Handler(Document.parentWindow.event)
End Sub
End Class

Private Sub Browser_DownloadBegin(ByVal sender As Object, ByVal e As System.EventArgs) Handles Browser.DownloadBegin

Dim doc As HTMLDocument = CType(Browser.Document, HTMLDocument)

If doc IsNot Nothing Then
Dim Handler As DHTMLEventHandler = New DHTMLEventHandler(doc)
Handler.Handler = AddressOf Me.Browser_ContextMenuStandardEvent

' add the events in here
doc.oncontextmenu = Handler
doc.onselectionchange = Handler
End If

End Sub

Private Sub Browser_ContextMenuStandardEvent(ByVal e As mshtml.IHTMLEventObj)
MessageBox.Show(String.Format("Action (Event Object) Hooked {0}", e.type))

Select Case e.type
Case "oncontextmenu"
e.returnValue = False

Case "onselectionchange"

End Select

End Sub

Tian Qiang Chen
July 07, 2006

# re: Handling mshtml Document Events without Mouse lockups

The sample code is quite good. It works in Visual Studio 2003.
But it does not work in Visual Studio 2005.
Please define the class DHTMLEventHandler as ComVisilbe(true).

[ComVisible(true)]
public class DHTMLEventHandler

It works.

Thanks Rick Strahl, the way is wonderful.

Majid
August 28, 2006

# Missing Event

I want to get mouse move event in axWebBrowser. I put both of the sample cods in my project but still if I get mouse position then mouse events don't transfare to the axWebBrowser and if I dont get the onmousemove event it works fine, Actualy I couldn't find any get event in your soulotion code so i confiused witch event is transfaring bye handle to onmousemove.

for first time I used this part of code:

HTMLDocument Doc = axWebBrowser1.Document as mshtml.HTMLDocument;
HTMLDocumentEvents2_Event DocEvents = axWebBrowser1.Document as HTMLDocumentEvents2_Event;
DocEvents.onmousemove += new HTMLDocumentEvents2_onmousemoveEventHandler(DocEvents_onmousemove);

then I added this code in below of it :

HTMLDocument doc = axWebBrowser1.Document as HTMLDocument;
DHTMLEventHandler Handler = new DHTMLEventHandler(doc);
Handler.Handler = new DHTMLEvent(DocEvents_onmousemove);
((DispHTMLDocument)doc).onmousemove = Handler;

I used second part of code aparently too.
I can get event but it couldn't transfare to the axWebBroser and it can't do any thing.

please help me.
thanks.

Satoshi Oseki
November 23, 2006

# re: Handling mshtml Document Events without Mouse lockups

Hi Rick,

You saved me, thank you!!

I was having the same problem using HTMLTextContainerEvents2
to hook up ondragenter, ondragover and ondrop events.
You code works just fine!!

Sergiu
December 25, 2006

# re: Handling mshtml Document Events without Mouse lockups

Thank you, it is really the solution to the my problem!!!!

Dan Morris
January 08, 2007

# re: Handling mshtml Document Events without Mouse lockups

My hero used to be Paul McCartney. Now my hero is Rick Strahl, and my other hero is Thomas Geller, who pointed out the ComVisible "mod" to Rick Strahl's original solution. This fixed problems that you weren't even trying to solve. You are my heroes.

-Dan

Greg
January 23, 2007

# re: Handling mshtml Document Events without Mouse lockups

Setting up the onclick handler for a button worked just fine, but when I try to register handlers for onkeydown/onkeyup/onkeypress for IHTMLInputElement elements, the callback routine doesn't seem to be triggered, even when my target HTML page is as simple as

<html>
<body>

<form>
First name:
<input type="text" name="firstname">
<br>
Last name:
<input type="text" name="lastname">
</form>

</body>
</html>

Harry
January 24, 2007

# re: Handling mshtml Document Events without Mouse lockups

Anyone get this working in 2005?
Getting:
Ambiguity between 'mshtml.DispHTMLDocument.oncontextmenu' and 'mshtml.HTMLDocumentEvents_Event.oncontextmenu'

Greg
January 24, 2007

# re: Handling mshtml Document Events without Mouse lockups

in which line are you getting that ambiguity? I think someone had that reported before when they used HTML<...> instead of IHTML<...> (i.e. event handler should be connected to the interface pointer, not the actual object)

Harry
January 25, 2007

# re: Handling mshtml Document Events without Mouse lockups

Im using this to get the instance:

static SHDocVw.WebBrowser browserInstance = null;
static SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();

foreach (SHDocVw.WebBrowser ie in shellWindows)
{

if (ie.HWND == The IE Handle I want)
{
browserInstance = ie;
break;
}
}
I get Doc from browserInstance.
New to this...not sure what you mean by IHTML.

Greg
January 28, 2007

# re: Handling mshtml Document Events without Mouse lockups

perhaps you can post the section of the code that's actually resulting in a warning?

elton
February 03, 2007

# re: Handling mshtml Document Events without Mouse lockups

Hi,
wondering how I can capture drag and drop event in the web browser control ? I can capture mouseup event but if I drag an item in then i can not get mouseup. any idea ?
thanks in advance !

Greg B
February 14, 2007

# re: Handling mshtml Document Events without Mouse lockups

On some web sites, when using the above code for InputText element handling, I often get an exception being thrown:

"COM object that has been separated from its underlying RCW cannot be used."

from the Call method, in the Handler(Document.parentWindow.@event) line

looks like the 'parentWindow' has thrown the 'InvalidCOMObjectException.

Ivo
February 16, 2007

# re: Handling mshtml Document Events without Mouse lockups

I am also getting the ambiguity, with line:
doc.oncontextmenu = Handler;

However, I cast the doc to mshtml.DispHTMLDocument:
((mshtml.DispHTMLDocument)doc).oncontextmenu = Handler;

since casting it to mshtml.HTMLDocumentEvents_Event does not compile in that it expects a hook (+= or -=).

Unfortunately, my events still do not fire. I have the following...
     mshtml.HTMLDocument doc = this.axWebBrowser1.Document as mshtml.HTMLDocument;

     DHTMLEventHandler Handler = new DHTMLEventHandler(doc);

     Handler.Handler += new DHTMLEvent(iEvent_onmouseup);

where the browser's document is accessed externally rather than internally (this.Document). Although I do not think that should be the problem...any help? =)
((mshtml.DispHTMLDocument)doc).oncontextmenu = Handler;

Ivo
February 16, 2007

# re: Handling mshtml Document Events without Mouse lockups

I am also getting the ambiguity, with line:
doc.oncontextmenu = Handler;

However, I cast the doc to mshtml.DispHTMLDocument:
((mshtml.DispHTMLDocument)doc).oncontextmenu = Handler;

since casting it to mshtml.HTMLDocumentEvents_Event does not compile in that it expects a hook (+= or -=).

Unfortunately, my events still do not fire. I have the following...
mshtml.HTMLDocument doc = this.axWebBrowser1.Document as mshtml.HTMLDocument;

DHTMLEventHandler Handler = new DHTMLEventHandler(doc);

Handler.Handler += new DHTMLEvent(Browser_ContextMenuStandardEvent);

((mshtml.DispHTMLDocument)doc).oncontextmenu = Handler;

where the browser's document is accessed externally rather than internally (this.Document). Although I do not think that should be the problem...any help? =)


***Please forgive the double post, a line from the code I have wandered to the bottom of the post...somehow...do_Ob

Ivo
February 16, 2007

# re: Handling mshtml Document Events without Mouse lockups

Upon further testing, the following is not executing:
((mshtml.DispHTMLDocument)doc).oncontextmenu = Handler;

It hits the line and then breaks out of the scope...do_Ob

Ivo
February 16, 2007

# re: Handling mshtml Document Events without Mouse lockups

Following Tian Qiang Chen's advice, it now works. =)

ramo
February 19, 2007

# re: Handling mshtml Document Events without Mouse lockups

I use SHDocVw.WebBrowser and IHTMLDocument2 structure.

public DHTMLEventHandler(IHTMLDocument2 doc)
{
this.Document = doc;
}

// Document(or Frame) event handlers, if this is not declared, you can not catch event.
DHTMLEventHandler HandlerFrame = new DHTMLEventHandler(document);
HandlerFrame.Handler += new DHTMLEvent(this.MouseClick);
document.onclick = HandlerFrame;

this is working successfully. But another Bho overridde older onclick. Any suggestion?

Diego Frata
April 23, 2007

# re: Handling mshtml Document Events without Mouse lockups

As I tried to implement the code on VS2005, I realized I cannot access the Document.parentWindow object, required to reach @event.

    public delegate void DHTMLEvent(mshtml.IHTMLEventObj e);

    [ComVisible(true)]
    public class DHTMLEventHandler
    {
        public DHTMLEvent Handler;
        mshtml.IHTMLDocument2 Document;

        public DHTMLEventHandler(mshtml.IHTMLDocument2 doc)
        {
            this.Document = doc;
        }

        [DispId(0)]
        public void Call()
        {
            Handler(Document.parentWindow.@event);
        }

    }


Above is the code extracted from the post. I've tried both with Interfaces and the class itself.


I read all the topics and can't find a solution. It always throws an Invalid cast exception when I try to reach parentWindow.

Jake
June 12, 2007

# re: Handling mshtml Document Events without Mouse lockups

I have got the code to work, however, I load a page I've created, and the handler only seems to ever work for the first page that is loaded. Everytime I navigate to a 2nd page, I am no longer getting my events thrown.

What is up?

wbInfo.Navigate(strURL)

Try
wbInfo.Refresh2()
Catch ex As Exception
' Do nothing; there is no real exception.
End Try

Dim doc As HTMLDocument = CType(wbInfo.Document, HTMLDocument)
'doc = CType(wbInfo.Document, HTMLDocument)

If Not (doc Is Nothing) Then
Dim Handler As DHTMLEventHandler = New DHTMLEventHandler(doc)
Handler.Handler = AddressOf wbInfo_DHTMLEventHandler

' add the events in here
doc.onclick = Handler
'doc.onselectionchange = Handler

End If ' Not (doc Is Nothing)

wbInfo is my browser control.

Please help, this is VERY frustrating! I tried moving the handler to NavigationComplete, DownloadComplete and DocumentComplete, but for some reason those events never get fired.

Thanks,

Jake

Jake
June 14, 2007

# re: Handling mshtml Document Events without Mouse lockups

This code does not work with IE7 I think.

Anyone else?

I tried it in VB & C#, but once I navigate to a 2nd page, the handler quits working.

Any ideas?

Jake

Rick Strahl
June 14, 2007

# re: Handling mshtml Document Events without Mouse lockups

Uhm you need to reattach the handler when you navigate to another page. These are document events and anytime you navigate the document changes.

Jake
June 14, 2007

# re: Handling mshtml Document Events without Mouse lockups

Rick,

Thanks for the reply. I don't know what I'm doing wrong, but I put the event handler in DocumentComplete & NavigateComplete to reattach the handler, but those Events are never triggered. My code to change my page is:

wb.Navigate(strURL)

Try
wb.Refresh2()
Catch
End Try

Now, the control will NOT navigate with the .Navigate call, but after I call .Refresh2, then it will change the page, however, it's not throwing any events, and if I don't trap the Refresh2 command, it gives me a invocation exception.

I'm not sure how to get the browser to navigate properly, but this all started with the IE7 update.

Perhaps there is code, or a sample, that someone has to get a standard Ax... Browser to navigate to a page created on the local disk?

I'm fairly sure that it's this problem with my navigation that is causing me problems... If I take out the .Refresh2() call, then it will not change the page... Since the handler only works the first time around, it makes me think this is related... Would it help if I posted my code for the webbrowser??

Thanks so much for your time,

Jake

Manish Verma
August 05, 2007

# re: Handling mshtml Document Events without Mouse lockups

This indeed is a great article. And for those looking for a similar solution to handling events within the Document (like onkeydown), try this;
    public delegate void DOMEvent(mshtml.IHTMLEventObj e);
    public class DOMEventHandler
    {
        public DOMEvent Handler;
        DispHTMLDocument Document;
        public DOMEventHandler(DispHTMLDocument doc)
        {
            this.Document = doc;
        }
        [DispId(0)]
        public void Call()
        {
            Handler(Document.parentWindow.@event);
        }
    }


and then we use the handler as;
mshtml.DispHTMLDocument dispDoc = webBrowser.Document as mshtml.DispHTMLDocument;
                DOMEventHandler onkeydownhandler = new DOMEventHandler(dispDoc);
                onkeydownhandler.Handler += new DOMEvent(docEvents_onkeydown);

                dispDoc.onkeydown = onkeydownhandler;

Dragomir
November 29, 2007

# re: Handling mshtml Document Events without Mouse lockups

Dude you rock!!!!!

Finally it works!!! It was a REAL pain in the a*s!!! Thanks for the blog!!!

mpava
November 30, 2007

# re: Handling mshtml Document Events without Mouse lockups

Thanks Rick Strahl and Manish Verma, you have solved to me a big problem, this is the only page on the web with the solution (2 days to work for nothing ;-( ). The last solution is the best for framework 2.0.

chongo2002
December 02, 2007

# re: Handling mshtml Document Events without Mouse lockups

Thank You! This was the most useful thing I have found in a long time! This is way simpler than everything I was trying to do and it actually works consistently! This solved all the problems I was having and no one else could figure out!

Stephan Lips
March 12, 2008

# InvalidCastException when attempting to access Document.parentWindow

I am using IE6 SP1, .NET 2.0 SP1, and experience an InvalidCastException ('specified cast is not valid) in all C# and VB.NET code samples tried for this line:
Handler(Document.parentWindow.event)
I saw a post reporting the same error above, but did not see any resolution. Like the above poster, I would like to have the Explorer opening a new Window that looks and feels like (and is)the InternetExplorer where I can capture some events.

Anybody else seen this, how did they get around it?

Great article and discussion, even quite some time after original publishing this was the only helpful source of information that numerous google searches came up with.

Thanks,

- Stephan

Jonathan
April 10, 2008

# re: Handling mshtml Document Events without Mouse lockups

Is there someone who are has been able to receive events from an iframe ??

Particulary onclick event.

I tried to subscribe using the technique desbribe in this article to all onclick events possibles. Which means, all frames (recursivly) document onclick & body onclick events.

I'm getting the onclick event of every thing outside the iframe, but not in.

If I subscribe directly to the event as :
AddHandler BROWSER.Document.onclick, AddressOf METHOD (VB)
BROWSER.Document.onclick += METHOD (C#)
then i get the mouse events are blocked until a wander else where and come couple of times then it will start to work, but ALL working. Which means, that the Iframe events are captured.

Someone has an idea one this one ?

Jonathan
April 24, 2008

# re: Handling mshtml Document Events without Mouse lockups

I found a solution myself (This was only tested on IE though):

The way to go is to have the DOM.External configured to an object of your own.

Then via javascript (which can be added on DocumentComplete) add :
function init(){
var IFrame = document.getElementById('R0');
IFrame.onreadystatechange = function() {
//Attach your events here to your external method. (if to external directly doesn't work, add a method which will call your external method).
};
}

//To detect full loading
function statechange() {if (document.readyState == "complete"){init();}}
document.onreadystatechange = statechange;

Jeff Muller
July 31, 2008

# re: Handling mshtml Document Events without Mouse lockups

I there a way to use this technique with say mshtml.HTMLDocument.attachEvent(...) to allow arbitrary javascript functions in your html to call handlers within your C# code.

I've been fighting with this a bit and I'm finding attachEvent to be fairly opaque.

Thanks in advance.

Abhi
August 17, 2008

# re: Handling mshtml Document Events without Mouse lockups

hey Rick (the Genius) i have read your article but when trying to implement
document.onkeydown += new HTMLDocumentEvents2_onkeydownEventHandler(Browser_OnKeyDown);

this following line gives me error - Cannot assign to keyword b'coz it's a group method??
i am working for the OnKeyDown Event handler .
just help me out in this

Cheers Rick

abhishek
August 28, 2008

# re: Handling mshtml Document Events without Mouse lockups

Can anyone Plz send me the source code for how to handle keyboard events .I am trying to hook the key events but rather then entering my text the system is replacing the character with my text.
Plz help
regards
Abhishek

Jack L
September 11, 2008

# re: Handling mshtml Document Events without Mouse lockups

RE: InvalidCastException

I believe it has something to do with threading. I viewed properties of the Document object with the thread that created the browser and it was fine. I then created a new thread and had it try to read the document and I got the 'InvalidCastException' trying to do the same thing.

Aaron
September 25, 2008

# re: Handling mshtml Document Events without Mouse lockups

Hi,
I am sorry if I am being a n00b. But if I wanted to handle multiple events (onclick, onmouseover etc.), how would I go about doing that?

Is the following code legit?

DHTMLEventHandler Handler_OnClick = new DHTMLEventHandler(doc);
Handler_OnClick.Handler += new DHTMLEvent(my_Handler_OnClick);

DHTMLEventHandler Handler_OnMouseOver = new DHTMLEventHandler(doc);
Handler_OnMouseOver.Handler += new DHTMLEvent(my_Handler_OnMouseOver);

Is there a way that we can use the same event handler object for different type of events?

Aaron.

Bob Pfeffer
November 19, 2008

# re: Handling mshtml Document Events without Mouse lockups

Hi,

This is in response to Stephan Lips. He is getting the Invalid Cast exception.

From some testing I did, Jack L. is correct. It is thread related. I found that the only thread I could reference the document.parentWindow property was from the Main thread (the first one created by the CLR). I am not sure about accessing other properties from other threads.

At first I tried attaching to the document from a thread I created. I used this thread as the only one I referenced the document properties. It was able to reference all properties EXCEPT the parentWindow. However even in this scenario, I was able to reference the parentWindow property from the main thread.

The trick is that the events caught by the handler described above are running on another thread it seems specifically created just to handle the event! If you reference the parentWindow property from this thread you get the Invalid Cast Exception. You have to get the code needed to handle the event running in the main thread. If it is windows form application, you can use the control.Invoke method. Otherwise you need to create some means of switching to the main thread to execute your event code.

I did this by creating a queue of items that has an instance of a delegate pointing to the event logic, any parameters, a return value, and an 'done' autoresetevent. The main thread sits in a loop waiting on an 'itemisqueued' autoresetevent. When an item is queued the 'itemisqueued' autoresetevent is set, and this thread waits for the 'done' autoresetevent to be set. The main thread then pulls the entry off the queue, invokes the routine pointed to by the delegate, saves any return value and then sets the 'done' event and waits for another item on the queue. Of course the main thread is only servicing this queue and not much else.

Mujtaba Panjwani
November 24, 2008

# re: Handling mshtml Document Events without Mouse lockups

I am facing two problems, one is Ambiguity problem in the base solution mentioned in the current blog and then "Not implemented" in the solution which works around "DispHTMLDocument" which was suggested by Manish Verma. I wanted to catch onActivate event of HTMLDocument.

The basic goal is to catch the active tab in IE7 so that a variable will be turned on for active tab and will be turned off for all other inactive tabs.

Please help me asap, it will be greatly appreciated.

Thanks in advance.

Mujtaba Panjwani

Dav
January 07, 2009

# re: Handling mshtml Document Events without Mouse lockups

Im finding the same locking issue when using the .net 3.5 webbrowser control for wpf and using the winforms webbrowser via the windowsFormsHost inside of wpf. Has anybody had any luck caputuring events from wither webbrowser control in wpf?

Ive tried the 2.0 code for making the generic event object, but I cant get to caputure any events.

Thanks

Visves
February 04, 2009

# re: Handling mshtml Document Events without Mouse lockups

I am confused by reading from top to bottom, why don't you create sample application and keep it as attachment. It will be easy for programmers to understand.

Brian Eith
February 26, 2009

# re: Handling mshtml Document Events without Mouse lockups

Been struggling with this using the WebBrowser control in WPF SP1. Thanks for the tip.

Here's the code that'll seems to work within WPF:

private void webBrowser_LoadCompleted(object sender, NavigationEventArgs e)
{
mshtml.HTMLDocument doc = webBrowser.Document as HTMLDocument;
if (doc != null)
{
DHTMLEventHandler Handler = new DHTMLEventHandler(doc);
Handler.Handler += new DHTMLEvent(DOMEventHandler);
((DispHTMLDocument)doc).oncontextmenu = Handler;
}
}

private void DOMEventHandler(IHTMLEventObj pEvtObj)
{
pEvtObj.returnValue = false;
}

Calina B
April 02, 2009

# re: Handling mshtml Document Events without Mouse lockups

thank you for your idea.
How can I have something similar for IHTMLElement2 ondragstart, ondrag, ondrop? The handler should be connected to the HTMLElement not the HTMLDocument, right?

Ameet
May 25, 2009

# re: Handling mshtml Document Events without Mouse lockups

Hi,
i Been Trying To handle the values for onkeydown and onkeypress events but cannot retrive these values as there is on the keycode value which can be retrived using the above code the problem is that is always gives the valu 65 either the value is "A" or "a" how to differentiate that I'm trying to make a keyboard driver using rick's code.
can any had faced this issue
need help
Ameet

Haque
June 17, 2009

# re: Handling mshtml Document Events without Mouse lockups

Hi,
This is a great blog. I'm hoping to resilve my issue ASAP.
I'm sorry to writen bad english.
Here is my C# code:

mshtml.IHTMLElement eHTML = null;                    
                    mshtml.IHTMLDocument2 html_doc = null;
                    html_doc = oIE.Document as mshtml.IHTMLDocument2;
                    DispHTMLDocument html = (DispHTMLDocument)html_doc;
                    eHTML = html.getElementById("textVal");
                    eHTML.innerText = "2";
                     mshtml.IHTMLElementCollection go = (mshtml.IHTMLElementCollection)Goihtml_doc.all;
                     mshtml.HTMLFormElement goForm = (mshtml.HTMLFormElement)go.item("frmLinks_1", 0);
                     goForm.submit();

I'm looking to put the dynamically Value internally into TextBox;
but its getting an error & not able to put the value into textBox.
Second Scenario that I need also:

How to execute Java Script function to clinking on
<a href='#' onClick='changePageID(3); return false;'>3</a>

link . Below is HTML code
 <div class='pagBG pagLH'>
<div class='flLeft font12 pagLH'>
<img ='http://static.naukimg.com/rstatic/images/bck_disable.gif' alt='' width='16' height='16' hspace='2' vspace='8' border='0' />
<img src='http://static.naukimg.com/rstatic/images/bck1_disable.gif' alt='' width='16' height='16' hspace='2' vspace='8' border='0' /></div>
<div class='flLeft font12 mainPadLR pagLH'><strong>1</strong>
 <a href='#' onClick='changePageID(2); return false;'>2</a> <a href='#' onClick='changePageID(3); return false;'>3</a> 
<a href='#' onClick='changePageID(4); return false;'>4</a> 
<a href='#' onClick='changePageID(5); return false;'>5</a> 
</div><div class='flLeft font12 pagLH'>...</div>
 <div class='flLeft font12 pagLH'> of 
<a href='#' onClick='changePageID(6); return false;'>6</a>
</div><div class='flLeft font12 pagLH'> <a href='#' onClick='changePageID(2); return false;'>
<img src='http://static.naukimg.com/rstatic/images/fwd1_enable.gif' alt='' width='16' height='16' hspace='2' vspace='8' border='0' /></a>
<a href='#' onClick='changePageID(6); return false;'>
<img src='http://static.naukimg.com/rstatic/images/fwd_enable.gif' alt='' width='16' height='16' hspace='2' vspace='8' border='0' /></a></div>
.

Al
September 04, 2009

# re: Handling mshtml Document Events without Mouse lockups

I am getting
Error 2 Ambiguity between 'mshtml.DispHTMLDocument.oncontextmenu' and 'mshtml.HTMLDocumentEvents_Event.oncontextmenu'

I am sing a BHO which unfortunately only allows a hook via ShDocVw
I tried to set it to System.Windows.Forms.WebBrowser but it then won't perform the onDocumentComplete method

So many people have written about this but I have not found a definitive answer

BlueRaja
November 17, 2009

# re: Handling mshtml Document Events without Mouse lockups

Anyone have any idea why using the plain events as described in the beginning of the article doesn't work? This seems like a pretty big bug for Microsoft to have simply released and ignored for 5+ years. Perhaps we're using the events incorrectly?

BlueRaja
November 21, 2009

# re: Handling mshtml Document Events without Mouse lockups

By the way:
for those of you who are getting

"Error 2 Ambiguity between 'mshtml.DispHTMLDocument.oncontextmenu' and 'mshtml.HTMLDocumentEvents_Event.oncontextmenu'"

To correct this, change
doc.oncontextmenu = Handler;
to
DispHTMLDocument dispDoc = doc;
dispDoc.oncontextmenu = Handler;

This is because the class HTMLDocument implements two interfaces, DispHTMLDocument and HTMLDocumentEvents_Event. Both interfaces have something named 'oncontextmenu' (or onmouseclick, or whatever) - one's an event, one's an object - so the compiler doesn't know which one you're trying to set. You want to set the one from DispHTMLDocument (the object).

As to why setting the object to a custom handler works, but setting the event doesn't, I have no idea.

Justin England
January 03, 2010

# re: Handling mshtml Document Events without Mouse lockups

Although this did not quite work for me, I am using a wpf application and .net 4, I did find an update on the microsoft forums that helped relate to this post.

http://social.msdn.microsoft.com/forums/en-US/ieextensiondevelopment/thread/e69909f0-d883-462d-be1d-792f016ef77e/

Jelle van Veen
May 10, 2010

# re: Handling mshtml Document Events without Mouse lockups

I have had a problem with an invalid cast exception for the IHTMLDocument2 in 2005 as well. My problem was i tried to cast it within a seperate thread than the main GUI-thread.

My Solution:

public void ExecuteJavascript(string javascript)
        {
            try
            {
                if (axWebBrowser1.InvokeRequired)
                    axWebBrowser1.Invoke(new delSendJavascript(sendJavascript), new object[] { javascript });
                else
                    sendJavascript(javascript);
                
                
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                // ACCESS DENIED?? Install IE7!
            }
        }
 
        private delegate void delSendJavascript(string javascript);
        private void sendJavascript(string javascript)
        {
            mshtml.IHTMLDocument2 d = ((mshtml.IHTMLDocument2)axWebBrowser1.Document);
            d.parentWindow.execScript(javascript, "JavaScript");
        } 

Sanket Shah
December 18, 2010

# re: Handling mshtml Document Events without Mouse lockups

Hi,
I have one sample project to automate Internet explorer in VB 6.0. The same thing when I am trying to do with .Net its just hangs my Internet explorer document. I am not able to type or click on any control on the page.

Here is the sample code block.

Imports SHDocVw
Imports mshtml
 
Public Class FrmRecorder
    Dim WithEvents IE As New SHDocVw.InternetExplorer
    Dim WithEvents HtmDoc As HTMLDocument
 
Private Sub FrmRecorder_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        IE.Visible = True
        IE.Navigate("www.google.com")
 
        Do While IE.Busy
            Application.DoEvents()
        Loop
 
    End Sub
 
    Private Function HtmDoc_onclick() As Boolean Handles HtmDoc.onclick
 
        MsgBox(HtmDoc.activeElement.tagName)
 
    End Function
 
    Private Sub IE_DocumentComplete(ByVal pDisp As Object, ByRef URL As Object) Handles IE.DocumentComplete
        HtmDoc = IE.Document
    End Sub
End Class


Can anybody please help me out in this issue?

I am in big trouble.
Thanks in advance.

Sanket Shah
December 21, 2010

# re: Handling mshtml Document Events without Mouse lockups

Please add one Microsoft.mshtml and IEFrame.dll reference.

Imports mshtml
Imports SHDocVw
 
Public Class frmRecorder
  Dim HtmDoc As mshtml.HTMLDocument
  Dim WithEvents Ie As New SHDocVw.InternetExplorer
 
  Private Sub Ie_DocumentComplete(ByVal pDisp As Object, ByRef URL As Object) Handles Ie.DocumentComplete
 
    HtmDoc = Ie.Document
    '' This is your declaration It doesn't resolved my issue
    'AddHandler HtmDoc.onclick, AddressOf HtmDoc_onclick
    '' I tried with below code
    AddHandler CType(HtmDoc, _
      mshtml.HTMLDocumentEvents2_Event).onclick, AddressOf HtmDoc_onclick
 
  End Sub
 
  'Private Function HtmDoc_onclick() As Boolean
  '  MsgBox(HtmDoc.activeElement.tagName)
  'End Function
 
  Private Function HtmDoc_onclick(ByVal e As mshtml.IHTMLEventObj) As Boolean
    MsgBox(HtmDoc.activeElement.tagName)
    MsgBox(e.srcElement.tagName.ToString())
 
  End Function
 
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Ie.Navigate("www.google.com")
    Ie.Visible = True
  End Sub
End Class



I need to get focusin and focusout events to check the changes user made in input controls at the time of recording.

RaviKanth Malipeddu
November 02, 2011

# re: Handling mshtml Document Events without Mouse lockups

Hi Rick,

I am working on VB.Net and I have used mshtml events. When I use mshtml events on Internet Explorer at Document level, all the input elements are locked and cursor doesnot appear in the Internet Explorer which disables the user to enter the data in the input fields.

I debuged the issue and found that when the code enters into the event the Thread Apartment state changes to MTA which was in STA at the start of the program, which disables us to access the elements inside the HTML Document which are present under FRAMES.

Do you have any idea about such issue?

Thanks and Regards,
Ravikanth

NOTT
April 12, 2012

# re: Handling mshtml Document Events without Mouse lockups

I have the following codes

mshtml.DispHTMLDocument doc = this._browser.Document.DomDocument as mshtml.DispHTMLDocument;
 
DHTMLEventHandler mouseMoveHandler = new DHTMLEventHandler(doc);
mouseMoveHandler.Handler += new DHTMLEvent(onmousemove_handler);
doc.onmousemove = mouseMoveHandler;


    ///
    /// Generic HTML DOM Event method handler.
    ///
    public delegate void DHTMLEvent(mshtml.IHTMLEventObj e);
 
    ///
    /// Generic Event handler for HTML DOM objects.
    /// Handles a basic event object which receives an IHTMLEventObj which
    /// applies to all document events raised.
    ///
    public class DHTMLEventHandler
    {
        public DHTMLEvent Handler;
        mshtml.DispHTMLDocument Document;
 
        public DHTMLEventHandler(mshtml.DispHTMLDocument doc)
        {
            this.Document = doc;
        }
        [DispId(0)]
        public void Call()
        {
            Handler(Document.parentWindow.@event);
        }
    }


However, the onmousemove_handler is not called at all. I have tried other events as well, but none got triggered either.

el dude
April 18, 2013

# re: Handling mshtml Document Events without Mouse lockups

BUMP!

I'm facing this challenge but in VB 2008.

It looks like whatever event I add a handler for, all other events become ignored.

A HTML page loads and I hook the onmouseover event. The handler writes everything I mouseover to a list (as an example I found on Microsoft does) but the browser does not respond to anything else (mouse clicks, double clicks, or keystrokes) BUT it does respond to mousewheel actions and the backspace key...

I see that some found success in other implementations... Has anyone found success in VB 2008?

Eyal
October 31, 2013

# re: Handling mshtml Document Events without Mouse lockups

The event is not being fired this way. Any idea what I might be doing wrong?

I created a sample project (C#, VS 2010) with the exact same code and I don't see the events ever being fired..

Novarson
October 15, 2015

# re: Handling mshtml Document Events without Mouse lockups

It didn't work when i used it as posted. I needed therefore some edits to conform it to my expectations. See !, this is the origin post :

HTMLDocument doc = this.Document as HTMLDocument ;
DHTMLEventHandler Handler = new DHTMLEventHandler( doc );
Handler.Handler += new DHTMLEvent(this.Browser_ContextMenuStandardEvent);
doc.oncontextmenu = Handler;

This code displays in my program an ambiguity range error. So i've just modified the Navigated void method

My edit is given below :

HTMLDocument doc = this.Webpano.Document as HTMLDocument;

DispHTMLDocument docx = (DispHTMLDocument)doc; # Added line ----------------------------------------------------

DHTMLEventHandler Handler = new DHTMLEventHandler(doc);
Handler.Handler += new DHTMLEvent(this.Browser_ContextMenuStandardEvent);
docx.oncontextmenu = Handler; # Modified line -------------------------------

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024