Using the web browser control in your C # applications |
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
When 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 navigateSomethingThen 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 IHTMLDocument2mshtml.IHTMLDocument2 doc = axWebBrowser1.Document as mshtml.IHTMLDocument2;// write to the docdoc.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 wocould 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.
Download Sample project using the browser