Use C# to access Microsoft Web Browser component programmati
Jun 07, 2016 pm 03:50 PMC# 使用微软网页浏览器控件 译文见: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 programmat
C#使用微软网页浏览器控件
译文见: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 its Navigate method, and you must then wait for the documentation complete event. You obtain the document by casting the Web browser Document property to an IHTMLDocument2 interface object. You can query this object for its collections, such as its link or image collections, which are returned as IHTMLElementCollection objects.
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 new Visual C# Windows Application project named DOM.
The form name defaults to Form1.
- In Solution Explorer, right-click the References folder and select Add Reference.
The Add Reference dialog box opens.
- Click on the .NET tab and double-click the component named Microsoft.mshtml.
- Click OK.
References to the methods, events, and properties of the Microsoft DOM are added to the project.
- Open the Toolbox, right-click any tool, and choose Customize Toolbox.
The Customize Toolbox dialog box opens.
- Click on the COM Components tab and check Microsoft Web Browser.
The Web browser control labeled Explorer is added to the Toolbox components.
- Select the Explorer component and click the open form.
A Web browser component named axWebBrowser1 is added to the form.
- Add a TextBox component above the browser component and a ListBox component below it, accepting the default names of textBox1 and listBox1.
- Add a Button component to the right of listBox1. Change the Text property to "Submit," and accept the default name of button1.
The resulting form should look similar to the following screen shot:
- Double-click on button1.
The button1_Click method is added to the project.
- Replace the body of the button1_Click method with the following bold code:
private void button1_Click(object sender, System.EventArgs e) { <code><strong> object Zero = 0;</strong></code> <code><strong> object EmptyString = "";</strong></code> <code><strong> <span>axWebBrowser1</span>.Navigate(textBox1.Text,</strong></code> <code><strong> ref Zero, ref EmptyString, ref EmptyString, ref EmptyString);</strong></code> }
Salin selepas log masuk - Return to the form designer, select the browser component, and click the Events icon in the Properties window.
A list of Web browser events appears.
- Double-click the Document Complete event.
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; <strong>using mshtml;</strong>
Salin selepas log masuk - Replace the body of the axWebBrowser1_DocumentComplete event handler with the following code:
private void <span>axWebBrowser1</span>_DocumentComplete( object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e) { <code><strong> IHTMLDocument2 HTMLDocument = </strong></code> <code><strong> (IHTMLDocument2) <span>axWebBrowser1</span>.Document;</strong></code> <code><strong> IHTMLElementCollection links = HTMLDocument.links;</strong></code> <code><strong> listBox1.Items.Clear();</strong></code> <code><strong> foreach (HTMLAnchorElementClass el in links)</strong></code> <code><strong> {</strong></code> <code><strong> listBox1.Items.Add(el.outerHTML);</strong></code> <code><strong> }</strong></code> }
Salin selepas log masuk - 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 click Submit.
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)
- IHTMLDocument2 interface (http://msdn.microsoft.com/workshop/browser/mshtml/reference/ifaces/document2/document2.asp)

Artikel Panas

Alat panas Tag

Artikel Panas

Tag artikel panas

Notepad++7.3.1
Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina
Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1
Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6
Alat pembangunan web visual

SublimeText3 versi Mac
Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Topik panas

Bagaimana untuk melumpuhkan aplikasi latar belakang dalam Windows 11_Windows 11 tutorial untuk melumpuhkan aplikasi latar belakang

Tidak boleh membenarkan akses kepada kamera dan mikrofon dalam iPhone

Cara membaca pelan pelaksanaan prosedur tersimpan Oracle

Bagaimanakah mekanisme refleksi Java mengubah suai tingkah laku kelas?

Bagaimana untuk merentas domain iframe dalam vue
