| |
Hosting SHDocVw.DLL in a Windows Forms app
Hosting the Internet Explorer ActiveX control in a Windows Forms application is quite easy. The key is to use the .NET ActiveX Control to Windows Forms Assembly Generator, AxImp.exe, which is distributed as part of the Framework SDK. Here's the steps I used to build a simple browser.
- Use the wizard to create a simple Windows Forms project. I selected C# and called mine MySimpleBrowser.
- In a VS.NET command window, navigate to the project directory. Then run this command:
aximp %systemroot%\system32\shdocvw.dll This will create two files in the project directory: SHDocVw.dll and AxShDocVw.dll
- Back in the VS.NET project, add references to these two DLLs.
- Open up a code window for the form. Add a variable to the form class to hold a reference to the browser:
private AxSHDocVw.AxWebBrowser ieBrowser; and a local variable to the InitializeComponent method (you'll have to reveal the Windows Form Designer generated code, which is hidden behind a plus sign by default):
this.ieBrowser = new AxSHDocVw.AxWebBrowser(); Also in InitializeComponent, look for a call to this.Controls.AddRange. If you have added any controls to the form, you should find it near the bottom of the method. If you don't find it, add this line at the end of the method:
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.ieBrowser}); If you do find it, add a reference to your browser control to the array initializer that follows the constructor call, so it looks something like this:
this.Controls.AddRange(new System.Windows.Forms.Control[] { this.textBox1, this.ieBrowser});
- Build the project. If you have followed the above instructions, it will succeed.
- Switch to design view. You will see a new blank control that represents your browser window. You can move it and size it on your form. I made mine fill the form, which I also enlarged, except for a small area at the top, where I will put a textbox to specify the URL to navigate to. I also used the properties page to set the anchor property to "Top, Bottom, Left, Right", so the browser window will expand and contract when I expand and contract the form window.
- Add a textbox to allow the user to specify a URL. You'll probably want to clear the Text property, which by default contains the default name of the textbox, "textBox1."
- Add a KeyDown handler to the textbox to look for the enter key. When this key is pressed, your code will direct the browser control to navigate to the URL in the textbox. My handler looks like this:
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { object dummy = null; ieBrowser.Navigate(textBox1.Text,ref dummy,ref dummy,ref dummy,ref dummy); } }
That's all there is to it. Build the project and run the resulting program, and you have a simple web browser.
If you want your code to manipulate the currently displayed document through its DOM, this is easy to do, too. Just add a reference to the Microsoft.mshtml assembly, and create a variable of type Microsoft.mshtml.HTMLDocumentClass, like so (this code assumes you've included "using Microsoft.mshtml;":
HTMLDocumentClass doc = (HTMLDocumentClass)ieBrowser.Document; I've used this technique to insert programmatically generated HTML into the browser window, using something along these lines:
HTMLBodyClass bodyElem = (HTMLBodyClass)doc.body; bodyElem.innerHTML = mystr;
As an alternative, I've created a temporary file, filled it with HTML generated on the fly, then navigated the browser to it. Here's a snippet that does that:
string fileName = Path.GetTempFileName(); FileStream f = new FileStream(fileName, FileMode.Open); StreamWriter s = new StreamWriter(f); s.Write(mystr); s.Close(); File.Move(fileName,fileName+".html"); object dummy = null; ieBrowser.Navigate(@"file://" + fileName,ref dummy,ref dummy,ref dummy,ref dummy); With either of these techniques, the browser must be initialized. To do this, I added this code to my form load-event handler:
object dummy = null; ieBrowser.Navigate("about:blank",ref dummy,ref dummy,ref dummy,ref dummy);
The browser control is a window into a very complex collection of code. These are techniques that have worked for me, but my no means are they guaranteed to be the best way to do what I'm trying to do. If you know a better way, I'd be thrilled to hear about it.
|
© Copyright 2003 Jim Klopfenstein.
Last update: 2/10/2003; 8:43:52 AM.
|
|
|