Click here to Skip to main content
13,513,296 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


30 bookmarked
Posted 22 Oct 2008

Remove Microsoft.mshtml dependency

, 22 Oct 2008
Rate this:
Please Sign up or sign in to vote.
A simple approach to remove the Microsoft.mshtml dependency commonly needed with the WebBrowser control.


I am currently working on a program called Jonathan (hosted at CodePlex) that will enable you to study a library of Spiritual Texts. After looking at many different ways of displaying texts to the user, I finally decided to use the WebBrowser control to load dynamically created HTML documents. To get everything I needed out of the WebBrowser, I was forced add a reference to Microsoft.mshtml.dll in my project. This was not that big of a deal (aside from the size), until I found out that MSHTML did not work the same on all PCs. Thus I was forced to come up with a way to remove my dependencies on Microsoft.mshtml.dll.


I tried numerous ideas on how to get rid of all the references to MSHTML I had (believe me, there was a lot!), but nothing fully worked. The main way I tried was to just do without the code, or try to implement it in a different way. As I was doing this, I was looking through the methods and properties in the WebBrowser's Document object (ah, the joys of intellesense), and I stumbled upon the InvokeScript method, thank God! We can all relax now. Although I knew it was there, I never really tried to use it, because I was letting my HTML document call all the needed scripts. By the way, the only way to let your HTML document call JavaScript code on its own is to load the document via the DOM, which needs MSHTML (see below).

By using the InvokeScript method, we can call any JavaScript/VBScript method in our WebBrowser's Document. I should also point out that most of the things you need MSHTML for can be done in a scripting language like JavaScript. So it hit me, why not I convert all of my MSHTML code to JavaScript and call them via the InvokeScript method when needed. And that, boys and girls, is how I solved my problem!

Using the code

Basically, I had to find a way to change all my C# code into JavaScript, which wasn't too hard. I started off by using a StringBuilder object to create my HTML documents, and appended all the JavaScript data I needed at the beginning.

StringBuilder sb = new StringBuilder();
sb.Append("<style type=\"text/css\">");
// ... Style Sheet Info ...

// Here is where the real magic begins...
sb.Append("<script type=\"text/javascript\">");
// ...
sb.Append("function getTextSelection()");
sb.Append("var tRange = document.selection.createRange();");
sb.Append("var txt = tRange.text;");
sb.Append("return txt;");
// ...
sb.Append("</head><body onload=\"resizeJonathan();\" style" + 
   "=\" background: White; color: Black; font-family: \"" +
   this.Font.Name + "\", Arial, sans-serif; font-size:" + 
   Convert.ToString(this.Font.Size + 4) + ";\">");

After that, I get all my data from a database and append the body of the HTML document.

Displaying the HTML Document

Now once the HTML document has been created in memory, we need to give it to the WebBrowser to display. Below are the old and new methods of doing this:

// Old way using Microsoft.mshtml...
pubilc void LoadHTML(string html)
    // If you have a lot of JavaScript in your HTML document,
    // and you do not mind using mshtml, then this is the way for you.
    // All of your JavaScript functions will be called and operate as in IE.
    mshtml.IHTMLDocument2 doc = 

// New way NO mshtml...
public void LoadHTML(string html)
    // This is the way to load HTML into the WebBrowser without
    // using mshtml, but you will have to call your JavaScript
    // methods in your App's code by using the InvokeScript method.

Getting the Selection

Here is how you can get the WebBrowser's selection with MSHTML:

public string GetSelection()
    mshtml.IHTMLDocument2 doc = 
    mshtml.IHTMLSelectionObject sel = doc.selection;
    mshtml.IHTMLTxtRange range = (mshtml.IHTMLTxtRange)sel.createRange();
    if (range.text == null)
        return "";
        return range.text.Trim();

And without:

// Java Script Code in your HTML Document...
function getTextSelection
    var tRange = document.selection.createRange();
    var txt = tRange.text;
    if(txt == null)
        return "";
        return txt;

// Code in your app...
public string GetSelection()
    return this._WebMain.Document.InvokeScript(

Finding Text

This is how we originally found text in the WebBrowser with MSHTML:

public bool FindNext(string text)
    IHTMLDocument2 doc = (IHTMLDocument2)this._WebMain.Document.DomDocument;
    IHTMLSelectionObject sel = (IHTMLSelectionObject)doc.selection;
    IHTMLTxtRange rng = (IHTMLTxtRange)sel.createRange();
    // collapse the current selection so we start
    // from the end of the previous range
    if (rng.findText(text, 1000000000, 0))
        return true;
        return false;

And without:

// JavaScript Code inside HTML Document...
var tRange = null;
function findString(strToFind)
    var strFound;
    if (tRange != null)
        strFound = tRange.findText(strToFind);
        if (strFound);
    if (tRange == null || strFound == 0)
        strFound = tRange.findText(strToFind);
        if (strFound);

// Code in you app...
public void FindNext (string txtToFind)
        new object[] { txtToFind });

Points of Interest

An easy way to manage all of your added functions is to just create a new class extended from the WebBrowser class, and add all of your new code to that.

There is also a Resize method in the supplied project's code. This is used to dynamically resize the HTML document's divisions (div tags). I needed an HTML table that had static column headers. To do this, you basically create two div tags, one with a header table, and the other with a data table. The Resize function in JavaScript will size the divisions properly, but it needs to be called whenever your browser fires the Resize event.

In the supplied project, there is a method (expandSelection) that will select the word the user clicks with the right mouse button. This is a handy little feature when you want to quickly search for or define a particular word. When this function is called, it will mess up the ActiveElement in the WebBrowser's Document property. I need the ActiveElement because in my HTML document, it has several key pieces of information needed to do some routine jobs (look at my HTML code in the supplied project, the TR tags have a passage attribute that I need to determine where we are). So to get around this, I saved the HtmlElement that was in the ActiveElement property on the MouseDown event. By the time I got around to looking at the ActiveElement, it would have changed to (usually) the whole document. If this is the case, then I just go and grab the MouseDownElement I saved during the MouseDown event. This is not supplied in the given code, but if you require further assistance on it, look at the source code for Jonathan at the CodePlex website, or drop me a line.


  • 10/22/08 - Initial release of article.


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Software Developer Halliburton
United States United States

You may also be interested in...

Comments and Discussions

GeneralNo need to write the functions Pin
jessehogan29-Nov-10 12:33
memberjessehogan29-Nov-10 12:33 
GeneralCopyLocal Pin
skyline9220-Jun-09 13:35
memberskyline9220-Jun-09 13:35 
Generalinteresting article Pin
BillWoodruff26-Oct-08 9:47
memberBillWoodruff26-Oct-08 9:47 
GeneralInteresting solution Pin
JMinadeo24-Oct-08 4:50
memberJMinadeo24-Oct-08 4:50 
AnswerRe: Interesting solution Pin
Jpmon124-Oct-08 8:07
memberJpmon124-Oct-08 8:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180417.1 | Last Updated 22 Oct 2008
Article Copyright 2008 by Jpmon1
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid