Hooking mshtm Documentl Events and Mouse going dead

R

Rick Strahl [MVP]

I'm working on an app that's using the WebBrowser control.

I got the control working fine, hooking to the document object. But I've run
into a major issue with hooking the Document events. Whenever I hook any of
the HTMLDocumnetEvent2_Event events like this:

HTMLDocumentEvents2_Event DocEvents = this.Browser.Document as
HTMLDocumentEvents2_Event ;
DocEvents.oncontextmenu += new
HTMLDocumentEvents2_oncontextmenuEventHandler(Browser_ContextMenu);

the browser document becomes unresposinve. The events fire fine and the
Document otherwise works - links highlight, status events fire and I can
select linsk with the keyboard. But all mouse clicks are eaten.

It doesn't matter which event I hook - just to verify I used the onhelp
event instead of one that hooks mouse events. As soon as the event gets
hooked up the mouse clicks die. Take it out - all is well (well within
reason).

Any ideas?

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
 
P

Peter Bromberg [C# MVP]

Rick,

Nikit Zykov had a fine "mini book" that was originally published on C#
Today called "Programming Internet Explorer in C#". I am not sure you can
still get hold of it because of all the Wrox stuff in the last year or so,
but if you haven't seen it, I recommend it highly.

Zykov's implementation snippet (greatly simplified, but usable nonetheless)
follows:
===================================================
Create a handler class able to give us access to the DHTML event object
interface that we need in order to access to some supplemental information
about the event we're managing, like mouse click coordinates or the event
source object. You can get the event property of the window object attached
to the document. Extend your DHTMLDoc class by adding a new Event property
to it:

public IHTMLEventObj Event
{
get{ return doc2.parentWindow.@event; }
}

Now you can create a new fully functional DHTMLEventHandler class and a
corresponding delegate

public delegate void DHTMLEvent(IHTMLEventObj e);

public class DHTMLEventHandler
{
public DHTMLEventHandler(DHTMLDoc doc)
{
document=doc;
}

[DispId(0)]
public void Call()
{
Handler(document.Event);
}

public DHTMLEvent Handler;
DHTMLDoc document;
}

Now create a new DHTMLElement class to allow you to encapsulate the generic
DHTML element functionality.
The first bit will be event handling. In the code below, we implement the
onclick event template:

public class DHTMLElement
{
public DHTMLElement(DHTMLDoc d,object e)
{
if(!(e is IHTMLElement))throw new Exception("This is not a DHTML
element!");
doc=d;
elm=(IHTMLElement)e;
}

public event DHTMLEvent onClick
{
add
{
object old=elm.onclick;
DHTMLEventHandler h;

if(old.GetType()==typeof(DHTMLEventHandler))h=((DHTMLEventHandler)old);
else elm.onclick=h=new DHTMLEventHandler(doc);
h.Handler+=value;
}
remove
{
object old=elm.onclick;
if(old.GetType()==typeof(DHTMLEventHandler))
((DHTMLEventHandler)old).Handler-=value;
}
}

IHTMLElement elm;
DHTMLDoc doc;
}

You can do copy-and-paste to create other events handlers; the only
additional work you'll probably have to do concerns querying for special
interfaces for the events that don't concern all the DHTML element types
(like onclick).

Once this work is done, you can add an event handler to any DHTML element by
using only two lines of code, like with DHTML except that in our case, you
are sure it works and you can share one delegate type for all your events.
To give you an example, we'll create the following handler in our Form1
class:

private void DHTMLEvtClick(IHTMLEventObj e)
{
MessageBox.Show("Click detected at: "+e.x+"/"+e.y,"C# event handler");
}

To attach this to the event, just add these two lines to your
DocumentComplete handler:

DHTMLElement elm=new DHTMLElement(doc,doc["text1"]);
elm.onClick+=new DHTMLEvent(DHTMLEvtClick);

Hope this help!
Peter
 
R

Rick Strahl [MVP]

Thanks a bunch Peter. I picked up a download version of the book from
Amazon.
Damn I wish I would have found this a little earlier <g>...

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
----------------------------------
Making waves on the Web


Peter Bromberg said:
Rick,

Nikit Zykov had a fine "mini book" that was originally published on C#
Today called "Programming Internet Explorer in C#". I am not sure you can
still get hold of it because of all the Wrox stuff in the last year or so,
but if you haven't seen it, I recommend it highly.

Zykov's implementation snippet (greatly simplified, but usable nonetheless)
follows:
===================================================
Create a handler class able to give us access to the DHTML event object
interface that we need in order to access to some supplemental information
about the event we're managing, like mouse click coordinates or the event
source object. You can get the event property of the window object attached
to the document. Extend your DHTMLDoc class by adding a new Event property
to it:

public IHTMLEventObj Event
{
get{ return doc2.parentWindow.@event; }
}

Now you can create a new fully functional DHTMLEventHandler class and a
corresponding delegate

public delegate void DHTMLEvent(IHTMLEventObj e);

public class DHTMLEventHandler
{
public DHTMLEventHandler(DHTMLDoc doc)
{
document=doc;
}

[DispId(0)]
public void Call()
{
Handler(document.Event);
}

public DHTMLEvent Handler;
DHTMLDoc document;
}

Now create a new DHTMLElement class to allow you to encapsulate the generic
DHTML element functionality.
The first bit will be event handling. In the code below, we implement the
onclick event template:

public class DHTMLElement
{
public DHTMLElement(DHTMLDoc d,object e)
{
if(!(e is IHTMLElement))throw new Exception("This is not a DHTML
element!");
doc=d;
elm=(IHTMLElement)e;
}

public event DHTMLEvent onClick
{
add
{
object old=elm.onclick;
DHTMLEventHandler h;

if(old.GetType()==typeof(DHTMLEventHandler))h=((DHTMLEventHandler)old);
else elm.onclick=h=new DHTMLEventHandler(doc);
h.Handler+=value;
}
remove
{
object old=elm.onclick;
if(old.GetType()==typeof(DHTMLEventHandler))
((DHTMLEventHandler)old).Handler-=value;
}
}

IHTMLElement elm;
DHTMLDoc doc;
}

You can do copy-and-paste to create other events handlers; the only
additional work you'll probably have to do concerns querying for special
interfaces for the events that don't concern all the DHTML element types
(like onclick).

Once this work is done, you can add an event handler to any DHTML element by
using only two lines of code, like with DHTML except that in our case, you
are sure it works and you can share one delegate type for all your events.
To give you an example, we'll create the following handler in our Form1
class:

private void DHTMLEvtClick(IHTMLEventObj e)
{
MessageBox.Show("Click detected at: "+e.x+"/"+e.y,"C# event handler");
}

To attach this to the event, just add these two lines to your
DocumentComplete handler:

DHTMLElement elm=new DHTMLElement(doc,doc["text1"]);
elm.onClick+=new DHTMLEvent(DHTMLEvtClick);

Hope this help!
Peter




Rick Strahl said:
I'm working on an app that's using the WebBrowser control.

I got the control working fine, hooking to the document object. But I've run
into a major issue with hooking the Document events. Whenever I hook any of
the HTMLDocumnetEvent2_Event events like this:

HTMLDocumentEvents2_Event DocEvents = this.Browser.Document as
HTMLDocumentEvents2_Event ;
DocEvents.oncontextmenu += new
HTMLDocumentEvents2_oncontextmenuEventHandler(Browser_ContextMenu);

the browser document becomes unresposinve. The events fire fine and the
Document otherwise works - links highlight, status events fire and I can
select linsk with the keyboard. But all mouse clicks are eaten.

It doesn't matter which event I hook - just to verify I used the onhelp
event instead of one that hooks mouse events. As soon as the event gets
hooked up the mouse clicks die. Take it out - all is well (well within
reason).

Any ideas?

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
 
P

Peter Bromberg [C# MVP]

You bet. Some of your excellent code has certainly helped me.
Cheers.

Rick Strahl said:
Thanks a bunch Peter. I picked up a download version of the book from
Amazon.
Damn I wish I would have found this a little earlier <g>...

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
----------------------------------
Making waves on the Web


Peter Bromberg said:
Rick,

Nikit Zykov had a fine "mini book" that was originally published on C#
Today called "Programming Internet Explorer in C#". I am not sure you can
still get hold of it because of all the Wrox stuff in the last year or so,
but if you haven't seen it, I recommend it highly.

Zykov's implementation snippet (greatly simplified, but usable nonetheless)
follows:
===================================================
Create a handler class able to give us access to the DHTML event object
interface that we need in order to access to some supplemental information
about the event we're managing, like mouse click coordinates or the event
source object. You can get the event property of the window object attached
to the document. Extend your DHTMLDoc class by adding a new Event property
to it:

public IHTMLEventObj Event
{
get{ return doc2.parentWindow.@event; }
}

Now you can create a new fully functional DHTMLEventHandler class and a
corresponding delegate

public delegate void DHTMLEvent(IHTMLEventObj e);

public class DHTMLEventHandler
{
public DHTMLEventHandler(DHTMLDoc doc)
{
document=doc;
}

[DispId(0)]
public void Call()
{
Handler(document.Event);
}

public DHTMLEvent Handler;
DHTMLDoc document;
}

Now create a new DHTMLElement class to allow you to encapsulate the generic
DHTML element functionality.
The first bit will be event handling. In the code below, we implement the
onclick event template:

public class DHTMLElement
{
public DHTMLElement(DHTMLDoc d,object e)
{
if(!(e is IHTMLElement))throw new Exception("This is not a DHTML
element!");
doc=d;
elm=(IHTMLElement)e;
}

public event DHTMLEvent onClick
{
add
{
object old=elm.onclick;
DHTMLEventHandler h;

if(old.GetType()==typeof(DHTMLEventHandler))h=((DHTMLEventHandler)old);
else elm.onclick=h=new DHTMLEventHandler(doc);
h.Handler+=value;
}
remove
{
object old=elm.onclick;
if(old.GetType()==typeof(DHTMLEventHandler))
((DHTMLEventHandler)old).Handler-=value;
}
}

IHTMLElement elm;
DHTMLDoc doc;
}

You can do copy-and-paste to create other events handlers; the only
additional work you'll probably have to do concerns querying for special
interfaces for the events that don't concern all the DHTML element types
(like onclick).

Once this work is done, you can add an event handler to any DHTML
element
by
using only two lines of code, like with DHTML except that in our case, you
are sure it works and you can share one delegate type for all your events.
To give you an example, we'll create the following handler in our Form1
class:

private void DHTMLEvtClick(IHTMLEventObj e)
{
MessageBox.Show("Click detected at: "+e.x+"/"+e.y,"C# event handler");
}

To attach this to the event, just add these two lines to your
DocumentComplete handler:

DHTMLElement elm=new DHTMLElement(doc,doc["text1"]);
elm.onClick+=new DHTMLEvent(DHTMLEvtClick);

Hope this help!
Peter




Rick Strahl said:
I'm working on an app that's using the WebBrowser control.

I got the control working fine, hooking to the document object. But
I've
run
into a major issue with hooking the Document events. Whenever I hook
any
of
the HTMLDocumnetEvent2_Event events like this:

HTMLDocumentEvents2_Event DocEvents = this.Browser.Document as
HTMLDocumentEvents2_Event ;
DocEvents.oncontextmenu += new
HTMLDocumentEvents2_oncontextmenuEventHandler(Browser_ContextMenu);

the browser document becomes unresposinve. The events fire fine and the
Document otherwise works - links highlight, status events fire and I can
select linsk with the keyboard. But all mouse clicks are eaten.

It doesn't matter which event I hook - just to verify I used the onhelp
event instead of one that hooks mouse events. As soon as the event gets
hooked up the mouse clicks die. Take it out - all is well (well within
reason).

Any ideas?

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top