Abstract: This article describes how to use Microsoft Web Browser controls and Microsoft Document Object Model (DOM) (Document Object Model) to access each element in the Web with programming.
To access the element of the web page, you must first reference the Web Browser control to apply its properties, objects, methods, and events. By calling the NaviGate method, you can open the page with Web Browser. However, you must use the program to access all the downloads after the document is all downloaded. : LINK collection, Image collection, etc., these collections will return to the IHTMLELEMEMENTCOLLECTION object. This article describes the example of the LINK collection, returns all links you specify the URL page.
Below is a simple process of achieving this feature:
1. Open Microsoft Visual Studio .NET, create a new Visual C # Windows Application (Windows Application), the default name is Form1.
2. In the Solution Explorer (Solution Browser), use the Right click on the References folder and select the Add Reference to select the Add Reference dialog box.
3, click the .NET tab, double-click the Microsoft.mshtml component, then click the OK button.
4, open the Toolbox, click Right-click on any tool item, select Customize Toolbox, this point, the custom toolbar dialog is open.
5. Click the COM Components tab to select the checkbox before Microsoft Web Browser selection. Point OK button. At this point, the Web Browser control called Explorer is added to the toolbox.
6. Select the Explorer control, add it to the Form1 form, named the default AxWebBrowser1.
7. Add Textbox on the browser control, add listbox below the Browser control, the name is the default TextBox1 and ListBox1, the TextBox1's Text Property is set to: http://lucky_love.www1.dotNetPlayground.com/; behind TextBox Add button, Text is changed to "Browse Page", named button1. At this point, the page will be in the following:
8, double-click Button1, enter the following code in the Button1_Click event:
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);} 9, switching window Body to design mode, select the Browser control, click "Event" icon on the Properties window, then the Web Browser Event dialog opens, double-click the Document Complete event, enter the following code in the event processing of AxWebBrowser1_DocumentComplete:
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);}}
10. Enter the top of the Form1.cs file:
USING MSHTML;
11, press F5, run the form, click the "Browse Page" button, will see the following results, is it very cool :)