首頁 資料庫 mysql教程 Use C# to access Microsoft Web Browser component programmati

Use C# to access Microsoft Web Browser component programmati

Jun 07, 2016 pm 03:50 PM
access microsoft use web

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

  1. Create a new Visual C# Windows Application project named DOM.

    The form name defaults to Form1.

  2. In Solution Explorer, right-click the References folder and select Add Reference.

    The Add Reference dialog box opens.

  3. Click on the .NET tab and double-click the component named Microsoft.mshtml.
  4. Click OK.

    References to the methods, events, and properties of the Microsoft DOM are added to the project.

  5. Open the Toolbox, right-click any tool, and choose Customize Toolbox.

    The Customize Toolbox dialog box opens.

  6. Click on the COM Components tab and check Microsoft Web Browser.

    The Web browser control labeled Explorer is added to the Toolbox components.

  7. Select the Explorer component and click the open form.

    A Web browser component named axWebBrowser1 is added to the form.

  8. Add a TextBox component above the browser component and a ListBox component below it, accepting the default names of textBox1 and listBox1.
  9. 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:

    Use C# to access Microsoft Web Browser component programmati 

  10. Double-click on button1.

    The button1_Click method is added to the project.

  11. 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>
    }
    登入後複製
  12. 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.

  13. Double-click the Document Complete event.

    The axWebBrowser1_DocumentComplete event handler is added to the project.

  14. Add the following bold line of code to the beginning of the file Form1.cs:
    using System.Data;
    <strong>using mshtml;</strong>
    登入後複製
  15. 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>
    }
    登入後複製
  16. Press F5 to build and run the project.

    The Form1 application appears.

  17. 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.

    Use C# to access Microsoft Web Browser component programmati 

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)


 

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Windows11怎麼停用後台應用程式_Windows11停用後台應用程式教學 Windows11怎麼停用後台應用程式_Windows11停用後台應用程式教學 May 07, 2024 pm 04:20 PM

Windows11怎麼停用後台應用程式_Windows11停用後台應用程式教學

deepseek怎麼轉換pdf deepseek怎麼轉換pdf Feb 19, 2025 pm 05:24 PM

deepseek怎麼轉換pdf

java裡面的dao是什麼意思 java裡面的dao是什麼意思 Apr 21, 2024 am 02:08 AM

java裡面的dao是什麼意思

無法允許存取 iPhone 中的相機和麥克風 無法允許存取 iPhone 中的相機和麥克風 Apr 23, 2024 am 11:13 AM

無法允許存取 iPhone 中的相機和麥克風

field在java中是什麼意思 field在java中是什麼意思 Apr 25, 2024 pm 10:18 PM

field在java中是什麼意思

Java反射機制如何修改類別的行為? Java反射機制如何修改類別的行為? May 03, 2024 pm 06:15 PM

Java反射機制如何修改類別的行為?

vue中iframe跨域的方法 vue中iframe跨域的方法 May 02, 2024 pm 10:48 PM

vue中iframe跨域的方法

記憶體長什麼樣 記憶體長什麼樣 Apr 21, 2024 pm 01:01 PM

記憶體長什麼樣

See all articles