Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / March 2008

Tip: Looking for answers? Try searching our database.

How to handle document events for the WebBrowser control

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
GoldTree@nospam.nospam - 04 Mar 2008 00:43 GMT
Hi,

I have a BHO (Browser Helpers Object) application that is intended to catch
the ContextMenu events.

I followed the article: "How to handle document events in a Visual C# .NET
application" http://support.microsoft.com/kb/312777

The code below will get event fired. But what happens is, if I add an event
handler for ANY of these types of events, the browser becomes completely
disabled. I am unable to select text or click on html links to go to another
page.  Even if I just add one event handler for onclickEventHandler for
example, the same thing happens.  It breaks the browser and there is no
obvious reason that I can figure out.

   public class BHO:IObjectWithSite
   {
       WebBrowser webBrowser;
       HTMLDocument doc1;
       MessageForm msgForm = null;

       public void OnDocumentComplete(object pDisp, ref object URL)
       {
           if (! (URL as string).Contains("http://localhost/")) return;

           if (msgForm == null)
           {
               msgForm = new MessageForm();
               msgForm.Show();
           }

           try
           {
               msgForm.textBox1.AppendText(URL as string + "\n");

               doc1 = webBrowser.Document as HTMLDocument;

      mshtml.HTMLDocumentEvents2_Event iEvent;
      iEvent = (mshtml.HTMLDocumentEvents2_Event) doc1;
      iEvent.oncontextmenu += new
mshtml.HTMLDocumentEvents2_oncontextmenuEventHandler(ContextEventHandler);
           }
           catch(Exception ex)
           {
               msgForm.textBox1.AppendText(ex.Message);
           }
       }

       private bool ContextEventHandler(mshtml.IHTMLEventObj e)
 {
           msgForm.textBox1.AppendText("\n");
           msgForm.textBox1.AppendText( e.type + ":" +
e.srcElement.tagName);
           return true;
 }
}

Can someone help? Thanks in advance.

GoldTree
Steven Cheng - 04 Mar 2008 09:08 GMT
Hi GoldTree,

Regarding on this issue, I noticed that you've posted a similar BHO thread
recent days, I've replied you with some info about customizing context
menu, here is my reply in that thread:

#BHO & context menu customization
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1159435&SiteID=1

Also, I've performed some further research, here is some other information
found so far:

========================
For all IE versions up to IE6.0, the best we can do is replace the context
menu by sinking document events, cancellling the oncontextmenu event and
drawing our own context menu. The CreateMenu(), Append/InsertMenuItem() and
TrackPopupMenu() Win32 API calls are required to do this from C++.

One can override the context menu and display a custom one by implementing
IDocHostUIHandler interface in the host application. One problem is that
even if you build the same menu as IE's, there are no CMDID's to invoke
default implementation of IE for all of those commands using
IOleCommandTarget.

1. With IE 4.x one can subclass IE's window(one customer did this in
IDocHostUIHandler::OnContextMenu) and watch for the WM_INITPOPUPMENU
message and wait for the WM_COMMAND message sent when the user selects a
menu item. But with IE 5 onwards, IE calls TrackPopupMenu, gets the command
ID of the item selected and invokes the appropriate functionality using
IOleCommandTarget without generating a WM_COMMAND message. So developers
can no longer rely on this technique from IE 5 onwards.

2. On some occasions, developers may be doing this to prevent the user from
invoking certain menu items (eg: View Source) In this case, it's easier to
disable the menu item. Subclass the webbrowser control window, watch for
WM_INITPOPUPMENU. You will get the HMenu from this which you can
use to go disable/grey out items using the EnableMenuItem or
SetMenuItemInfo methods. Remember to return 0 after processing this message.

* Note that there is still no way to prevent script on the page from
invoking the same commands.
* For menu items that have accelerator keys, you can watch for these
keystrokes in IDocHostUIHandler::TranslateAccelerator.

Some customers tried to watch for WM_MENUSELECT messages and figure out the
last one moused over to determine which was selected. Since there are a lot
of scenarios that will need to be taken care of eg: user dismiised the menu
by clicking outside the context menu, pressed escape or selected using
mouse or keyboard, strongly advise them against using this technique

==================================

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

is this a new problem or also the same one with that?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
>From: <GoldTree@nospam.nospam>
>Subject: How to handle document events for the WebBrowser control
[quoted text clipped - 60 lines]
>
>GoldTree
GoldTree@home.com - 05 Mar 2008 17:31 GMT
Hi Steven,

Thanks for your reponse.

1. I like to do this in C#.

2. What is the problem with my code? Why is the other event becomes dead?
(My code follows MSDN KB312777.)

3 There were some post reported this. There seems to be a solution in this
thread:

http://groups.google.com/group/microsoft.public.dotnet.framework.clr/browse_thre
ad/thread/29c030d6faeca214/1d5d8345ef3d7039?hl=en&lnk=st&q=event+mouse+dead+#1d5
d8345ef3d7039


But I do not get the full picture of the solution.

4. There is a solution suggested at this link

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_20661377.html

(scroll to the bottom) Is this a recommended solution?

5. I read the link you provided. But there is one comment that "The drawback
is, that only one extension at the time can do this trick."

Regards,

GoldTree

> Hi GoldTree,
>
[quoted text clipped - 128 lines]
> >
> >GoldTree
Steven Cheng - 10 Mar 2008 05:33 GMT
Hi GoldTree,

Thanks for your reply.
Regarding on the further description, I've performed some reseach and so
far haven't found any new info. I'm planning find some other resources to
help you on this issue. Would you send me a mail via the following address?

"stcheng"+"@"+"microsoft.com"

Best regards,

Steven Cheng
Microsoft MSDN Online Support Lead

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we

can improve the support we provide to you. Please feel free to let my
manager know what you think of

the level of service provided. You can send feedback directly to my manager
at: msdnmg@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>From: =?Utf-8?B?R29sZFRyZWVAaG9tZS5jb20=?=
<GoldTreehomecom@discussions.microsoft.com>
>References:  <O#c8RDZfIHA.4144@TK2MSFTNGP05.phx.gbl>
<H76OKddfIHA.6844@TK2MSFTNGHUB02.phx.gbl>
>Subject: RE: How to handle document events for the WebBrowser control
>Date: Wed, 5 Mar 2008 09:31:02 -0800

>Hi Steven,
>
[quoted text clipped - 7 lines]
>3 There were some post reported this. There seems to be a solution in this
>thread:

http://groups.google.com/group/microsoft.public.dotnet.framework.clr/browse_
thread/thread/29c030d6faeca214/1d5d8345ef3d7039?hl=en&lnk=st&q=event+mouse+d
ead+#1d5d8345ef3d7039

>But I do not get the full picture of the solution.
>
[quoted text clipped - 144 lines]
>> >
>> >GoldTree

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.