By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,826 Members | 2,384 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,826 IT Pros & Developers. It's quick & easy.

Hooking mshtm Documentl Events and Mouse going dead

P: n/a
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(Brow ser_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/
----------------------------------
Making waves on the Web

Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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=((DH TMLEventHandler)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 [MVP]" <ri********@hotmail.com> wrote in message
news:Oa**************@TK2MSFTNGP10.phx.gbl...
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(Brow ser_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/
----------------------------------
Making waves on the Web

Nov 16 '05 #2

P: n/a
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 [C# MVP]" <pb*******@yahoo.com> wrote in message
news:Oa**************@TK2MSFTNGP10.phx.gbl...
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=((DH TMLEventHandler)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 [MVP]" <ri********@hotmail.com> wrote in message
news:Oa**************@TK2MSFTNGP10.phx.gbl...
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(Brow ser_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/
----------------------------------
Making waves on the Web


Nov 16 '05 #3

P: n/a
You bet. Some of your excellent code has certainly helped me.
Cheers.

"Rick Strahl [MVP]" <ri********@hotmail.com> wrote in message
news:uk**************@tk2msftngp13.phx.gbl...
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 [C# MVP]" <pb*******@yahoo.com> wrote in message
news:Oa**************@TK2MSFTNGP10.phx.gbl...
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=((DH TMLEventHandler)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 [MVP]" <ri********@hotmail.com> wrote in message
news:Oa**************@TK2MSFTNGP10.phx.gbl...
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(Brow ser_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/
----------------------------------
Making waves on the Web



Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.