Say you want to use the browser component to automate some form of web browser experience - for example, say you want to programmatically enter some value into a textbox and click a button using C# or VB.Net. To do this is actually quite simple (and, dare we say it, fun).
First, you simply add your browser component to your windows form.
Second, you register an event handler with the browser component for the DocumentCompleted event:
this.webBrowser1.Url = new Uri("http://www.google.com");
this.webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
Third, you fill in the event handler with something that looks like this:
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (!_clicked)
{
_clicked = true;
HtmlElement queryBox = this.webBrowser1.Document.All["q"];
queryBox.SetAttribute("value", "hello world");
HtmlElement goButton = this.webBrowser1.Document.All["btnG"];
goButton.InvokeMember("click");
}
return;
}
This will load the www.google.com homepage, fill in their submit text box and click the Search button.
In the golden days of MSHTML component reuse (before the niceness of .Net'ifying the browser DOM) you had to use a RCW (Runtime Callable Wrapper) around the MSHTML COM library. Everything is obviously implemented through IDispatch here (because you have to be able to do this stuff from VB6 or scripting environments), and so much of the invocation of methods (like click) was done by loosly picking the name for the method and building the stack frame through arrays, etc. The RCW made things a bit nicer for you (you didn't have to think about IDispatch), but you still had to think about COM.
It's been years since I've developed in COM and ATL and I'm glad I don't have to anymore.
Recommended reading:
