`
lovecontry
  • 浏览: 1035884 次
文章分类
社区版块
存档分类
最新评论

Use C# to access Microsoft Web Browser component programmatically

 
阅读更多

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:

<!--Footer Start-->
分享到:
评论

相关推荐

    Beginning ASP.NET 1.1 with Visual C# .NET 2003.pdf

    The purpose of this book is to teach you from scratch how to use ASP.NET to write Web pages and Web applications in which content can be programmatically tailored each time an individual client ...

    EO.WebBrowser_for_.NET_2018.2.53.0

    Turn any web page into an integral part of your application -- both visually and programmatically. You can execute JavaScript code and access all the JavaScript objects directly from your .NET code. ...

    Professional C# 3rd Edition

    The Relationship of C# to .NET 4 The Common Language Runtime 4 Advantages of Managed Code 4 A Closer Look at Intermediate Language 7 Support for Object Orientation and Interfaces 8 Distinct Value and ...

    The Definitive Guide to JSF in JavaEE8 2018

    Work with the different ways of mapping requests to JSF, make your application use extensionless URLs, and programmatically inspect which resources are present in your application Master the best ...

    How to do almost everything In active directory via C#

    When it comes to programmatically accessing Microsoft's Active Directory a lot of people seem to have quite a difficult time tying all the pieces together to accomplish exactly what they want to....

    XML and Web Technologies for Data Sciences with R

    It describes strategies and approaches for extracting data from HTML, XML, and JSON formats and how to programmatically access data from the Web. Along with these general skills, the authors ...

    SMDK for DotNet

    SMDK for .NET allows Microsoft® .NET Compact Framework developers to programmatically access the Symbol value-add features of the mobile devices. Please refer to the Readme....

    Data Visualization with Python and JavaScript

    , Get data programmatically, using scraping tools or web APIs, Clean and process data using Python's heavyweight data-processing libraries, Deliver data to a browser using a lightweight Python server ...

    Amazon Web Services in Action(Manning,2015)

    You'll learn how to automate your infrastructure by programmatically calling the AWS API to control every part of AWS. Next, you'll learn options and techniques for storing your data. You'll also ...

    The Definitive Guide to JSF in Java EE 8--2018

    Work with the different ways of mapping requests to JSF, make your application use extensionless URLs, and programmatically inspect which resources are present in your application Master the best ...

    Web Animation Using JavaScript - js网页动画-英文原版

    This book provides you with a technical foundation to implement animation in a way that’s both visually stunning and programmatically maintainable. Throughout, we consider the balance between ...

    Drupal.8.for.Absolute.Beginners.1430264667

    Drupal 8 for Absolute Beginners is your definitive guide to starting from scratch with Drupal even if you have little web knowledge. This book teaches you the basics of HTML, CSS, JavaScript, and ...

    Jsp中嵌入CrystalReport指南

    This document contains a series of tutorials that demonstrates how to use the Crystal Reports Java Viewer SDK provided with Borland® JBuilder X. The tutorials assume that you are familiar with JSP ...

    Google chrome browser 65.0.3325

    The CSS Paint API allows you to programmatically generate an image The Server Timing API allows web servers to provide performance timing information via HTTP headers The new CSS display: contents ...

    Python Network Programming Cookbook, 2nd Edition - 2017

    browser with various features such as submitting web forms, handling cookies, managing partial downloads, compressing data, and serving secure content over HTTPS. Chapter 5, Email Protocols, FTP, and ...

    Effective Amazon Machine Learning

    Furthermore, you will learn to leverage the Amazon Web Service (AWS) ecosystem for extended access to data sources, implement realtime predictions, and run Amazon Machine Learning projects via the ...

    VB.NET Developer's Guide(4574).pdf

    Exercise 10.2 Adding Web Controls to a Web Form 468 Code Behind 473 NOTE Web form controls not only detect browsers such as Internet Explorer and Netscape, but they also detect devices such as Palm ...

    C#DataGridView中的常用技巧

    Programmatically Resize Cells to Fit Content in the Windows Forms DataGridView Control Samples: DataGridView.AutoSizeColumns( DataGridViewAutoSizeColumnCriteria.HeaderAndDisplayedRows); ...

Global site tag (gtag.js) - Google Analytics