qt8gt0bxhw|20009F4EEE83|RyanMain|subtext_Content|Text|0xfbfffa0000000000e000000001000d00
It can be a powerful thing to display dynamic HTML in your C# applications. It can give your applications a modern look and feel and can make displaying data in non-standard ways easy with some simple markup. We have the web browser ActiveX control that wraps up what we know as Internet Explorer. While I don't want to get into the in's and out's of using the web browser in your applications, I do want to demonstrate a few things that will make the use of the web browser easier so you can integrate it seamlessly into your applications.
Some common requests I see in regards to using the web browser control is the ability to set the HTML to display in the browser without writing it to a file and navigating to it, printing the contents of the browser, and other items related to manipulating it's contents. Let's take a quick look at those.
Setting the HTML without writing to a file
Many times when the web browser control is used, the HTML content that the developer wishes to display will be written to a text/html file and then Navigate is used to navigate to the file, such as the following:
object empty = System.Reflection.Missing.Value;
axWebBrowser1.Navigate(@"c:\myfile.htm", ref empty, ref empty, ref empty, ref empty);
The problem with that is, of course, that you need to write to a file. This brings in other possible issues such as ensuring you write the file to a location where the user has permissions to do so. A better route is to not write to a file at all. Instead, set the HTML directly in the browser. To accomplish this we need to get a reference to the browser's internal HTML document and manipulate it using the Document Object Model. We need to add a reference to the “Microsoft HTML Object Library” (mshtml) to get to the interfaces & classes that we will use to get to the document's DOM. Once you add the reference, you can get to the document and do whatever you want. However, in order to get to the document, you will have to navigate to about:blank before it can be accessed. If you do not first load or navigate to something then the document will be null. So first of all let's load about:blank to create a blank document.
object empty = System.Reflection.Missing.Value;
axWebBrowser1.Navigate("about:blank", ref empty, ref empty, ref empty, ref empty);
Now that we've done that we can get a reference to the document's DOM via the IHTMLDocument interface and write to it.
// create an IHTMLDocument2
mshtml.IHTMLDocument2 doc = axWebBrowser1.Document as mshtml.IHTMLDocument2;
// write to the doc
doc.clear();
doc.writeln("This is my text...");
doc.close();
Not too bad. We can write whatever markup we want to the document such as HTML, links, CSS, or even JavaScript.
Printing the Browser content
With a reference to the document, we can do whatever we want with it. That would include methods in the DOM such as execCommand. We can use that to print the browser's HTML.
mshtml.IHTMLDocument2 doc = axWebBrowser1.Document as mshtml.IHTMLDocument2;
doc.execCommand("Print", true, 0);
Printing is only the beginning of what you can do here. Take a look at all the possible commands you can send via execCommand here. It really opens up what you can get to when you look at what you have available. I'll outline in another post how to disable the right-click menu of the browser (by default you'll get the standard IE right-click menu) as well as how to take this a step further in integrating the power of C# with dynamic HTML in your applications.