时间:2021-07-01 10:21:17 帮助过:40人阅读
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 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
The form name defaults to Form1.
The Add Reference dialog box opens.
References to the methods, events, and properties of the Microsoft DOM are added to the project.
The Customize Toolbox dialog box opens.
The Web browser control labeled Explorer is added to the Toolbox components.
A Web browser component named axWebBrowser1 is added to the form.
The resulting form should look similar to the following screen shot:
The button1_Click method is added to the project.
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);
}
A list of Web browser events appears.
The axWebBrowser1_DocumentComplete event handler is added to the project.
using System.Data; using mshtml;
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);
}
}
The Form1 application appears.
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: