|
Using the IE Browser control in C#
After several years using Microsoft's browser control with COM and Visual C++ I
decided I would like to use it within C#. Luckily C# makes it's use even
easier.
There are a couple of ways of using the control one would be to create a .cs
file and hand code the declarations etc. for interacting with the browser
control. This has a major benefit in that you will not need the interop DLL's
and can ship/deploy and application with no extra DLL's for wrapping the
browser control. The down side is that you will need to write all the code and
keep it upto date.
For simplicity and ease of development this article will use the shdocvw.dll
and will let Visual Studio create the interop DLL for it automatically.
If you wish to add the web browser control to your toolbox do the following. On
the toolbox right mouse click on the toolbox section that you wish to insert
the browser control into. Select Add/Remove Items... and either browse to the
WinNT/System32 folder and select shdocvw.dll or from the COM Component tab
select the "Microsoft Web Browser".
Once added you'll see a icon of the globe with the name Microsoft Web Browser.
Now you can select this like any other control and drop it on your form.
If you wish to do anything with the HTML document or such like then you'll also
need to add a reference to your project to the MSHTML. Select the References
section of your project, right mouse click on it and choose Add Reference...
Now find the Microsoft HTML Object Library or select browse and find mshtml.tlb
(usually in the WinNT/System32 folder). This will add all the definitions and
declarations for many interfaces and constants etc. that are used with the
browser control.
Okay, now we've got a browser control on our form and references to the mshtml
object library so we can start doing things.
Here's a simple example of getting the html document and writing to it. It
should be noted that there's an issue with trying to write to the browser when
it current has no document in it. So I tend to ensure that in the constructor
of the form using the control I first navigate to about:blank - for example:
object nothing = null;
browser.Navigate("about:blank", ref nothing, ref nothing, ref nothing, ref
nothing);
Now to write to the DOM of the browser we first need to get at the document
within the browser. We do this by using the following:
string htmldoc = "<html><head></head><body>Hello
World</body></html>";
mshtml.IHTMLDocument2 html2;
html2 = (mshtml.IHTMLDocument2)browser.Document;
html2.writeln(new object[]{htmldoc});
html2.close();
We declare a variable of type IHTMLDocument2 and assign this from the browsers
document. QueryInterface-ing for the relevent interface is hidden from us
within the cast operation. Next we write something to the IHTMLDocument2
and we close the document to basically mean that when we next write we start a fresh
document. If you wished to append to this document later on, such as if you were outputting
event messages, then simply don't call the close method.
What else can we do ?
Well obviously we can do many things with the browser control. Here's a couple of further examples:
If we want to get a current browser (i.e. we wish to do something with an open instance of IE) we can do the following:
SHDocVw.IWebBrowser2 tmp = (SHDocVw.IWebBrowser2)Marshal.GetActiveObject("InternetExplorer.Application");
object nothing = null;
tmp.Navigate("http://www.google.co.uk", ref nothing, ref nothing, ref nothing, ref
nothing);
Maybe we would like to get the whole HTML document and then do something with this. We can achieve this by using the IHTMLDocument3
interface. For example:
mshtml.IHTMLDocument3 html3;
html3 = (mshtml.IHTMLDocument3)browser.Document;
string allhtml = html3.documentElement.outerHTML;
and I shall leave you to discover all the other things that can be done with the browser...
|