C # use Microsoft Web browser controls
See: http://blog.csdn.net/Felomeng/archive/2007/05/17/1613495.aspx
Summary:This walkthrough demonstrates how to use the Microsoft Web browser control and the Microsoft Document Object Model (DOM) to programmatically access the elements of any web page. (3 pages)
To access the DOM programmatically, you import both the web browser component and references to the methods, properties, and events of the DOM into your C # project. you direct the web browser to a URL by calling itsNavigateMethod, and you must then wait for the documentation complete event. you obtain the document by casting the web browserDocumentProperty toIhtmldocument2Interface object. You can query this object for its collections, such as its link or image collened, which are returnedIhtmlelementcollectionObjects.
In this walkthrough, you will use the web browser and Dom to obtain and display all anchors found in a Web page.
To access the DOM programmatically
- Create a newVisual C # Windows ApplicationProject named Dom.
The form name defaults to form1.
- In Solution Explorer, right-click the references folder and selectAdd reference.
TheAdd referenceDialog box opens.
- Click on. NetTab and double-click the component named Microsoft. mshtml.
- ClickOK.
References to the methods, events, and properties of the Microsoft Dom are added to the project.
- Open the toolbox, right-click any tool, and chooseCustomize toolbox.
TheCustomize toolboxDialog box opens.
- Click on the COM components tab and check Microsoft Web browser.
The Web browser control labeledExplorerIs added to the Toolbox components.
- SelectExplorerComponent and click the open form.
A Web browser component named axwebbrowser1 is added to the form.
- AddTextboxComponent above the browser component andListBoxComponent below it, accepting the default names of textbox1 and listbox1.
- AddButtonComponent to the right of listbox1. changeTextProperty to "Submit," and accept the default name of button1.
The resulting form shoshould look similar to the following screen shot:
- Double-click on button1.
TheButton#clickMethod is added to the project.
- Replace the body ofButton#clickMethod with the following bold code:
private void button1_Click(object sender, System.EventArgs e){ object Zero = 0; object EmptyString = ""; axWebBrowser1.Navigate(textBox1.Text, ref Zero, ref EmptyString, ref EmptyString, ref EmptyString);}
- Return to the Form Designer, select the browser component, and clickEventsIcon in the Properties window.
A list of web browser events appears.
- Double-clickDocument completeEvent.
The axwebbrowser1_documentcomplete event handler is added to the project.
- Add the following bold line of code to the beginning of the file form1.cs:
using System.Data;using mshtml;
- Replace the body of the axwebbrowser1_documentcomplete event handler with the following code:
private void axWebBrowser1_DocumentComplete( object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e){ IHTMLDocument2 HTMLDocument = (IHTMLDocument2) axWebBrowser1.Document; IHTMLElementCollection links = HTMLDocument.links; listBox1.Items.Clear(); foreach (HTMLAnchorElementClass el in links) { listBox1.Items.Add(el.outerHTML); }}
- Press F5 to build and run the project.
The form1 application appears.
- Type a URL-such as www.msn.com-into the text box and clickSubmit.
The web page is displayed in the browser, and a list of its anchors appears in the list box, as shown in the following screen shot.
For more information, see the following topics in the msdn Library:
- Dynamic HTML (http://msdn.microsoft.com/library/en-us/iisref/html/psdk/asp/eadg39v0.asp)
- Ihtmldocument2Http://msdn.microsoft.com/workshop/browser/mshtml/reference/ifaces/document2/document2.asp)