最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

UseC#toaccessMicrosoftWebBrowsercomponentprogrammati

来源:懂视网 责编:小采 时间:2020-11-09 07:19:00
文档

UseC#toaccessMicrosoftWebBrowsercomponentprogrammati

UseC#toaccessMicrosoftWebBrowsercomponentprogrammati: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
推荐度:
导读UseC#toaccessMicrosoftWebBrowsercomponentprogrammati: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

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:

  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)
    {
     object Zero = 0;
     object EmptyString = "";
     axWebBrowser1.Navigate(textBox1.Text,
     ref Zero, ref EmptyString, ref EmptyString, ref EmptyString);
    }
  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;
    using mshtml;
  15. 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);
     }
    }
  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.

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)
  • 声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文档

    UseC#toaccessMicrosoftWebBrowsercomponentprogrammati

    UseC#toaccessMicrosoftWebBrowsercomponentprogrammati: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 to web
    • 热门焦点

    最新推荐

    猜你喜欢

    热门推荐

    专题
    Top