Subclassing the WebBrowser Control in VB.Net 2.0; have full control over it, including mouse events

Category:
C#, VB.NET, ASP.NET
Type:
Snippets
Difficulty:
Advanced

Author: Anonymous

Version Compatibility: Visual Basic.NET

More information:
Microsoft doesn't want you to have too much control over their WebBrowser Control. This presents a problem when you want to capture events like mouse up, mouse down, mouse move, etc. In this particular Case, I am getting the selected text in a webbrowser control which is displaying an email that is in HTML format. The biggest problem is that you can't get a direct handle to the WebBrowser that you have embedded in your application because it's not available until you load a document, followed by the next biggest problem that the handle that you do get isn't the one you need. The way around this is to use the handle that Microsoft does provide and Walk the Window tree to get the one you need, but only after you load a document. When you reuse this snippet, notice that you are creating a separate class which should go after your form class if you want you designer to work. To test this, create a form application, place a webbrowser control called MailHTMLViewer on it, and create some kind of code on the load event of the form to load some HTML document into the WebBrowser. You are then going to Subclass that Control in the MailHTMLViewer_Navigated event. Whenever you run your app, when you select text and lift the left mouse button, you will see a message box with the text you selected.

Instructions: Copy the declarations and code below and paste directly into your VB project.

Declarations:

Code: