Use C # To access Microsoft Web browser component programmatically

Source: Internet
Author: User

C # use Microsoft Web browser controls

See: 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 itsNavigateMethod, and you must then wait for the documentation complete event. you obtain the document by casting the web browserDocumentProperty toIhtmldocument2Interface object. You can query this object for its collections, such as its link or image collened, which are returnedIhtmlelementcollectionObjects.

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 newVisual C # Windows ApplicationProject named Dom.

    The form name defaults to form1.

  2. In Solution Explorer, right-click the references folder and selectAdd reference.

    TheAdd referenceDialog box opens.

  3. Click on. NetTab and double-click the component named Microsoft. mshtml.
  4. ClickOK.

    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 chooseCustomize toolbox.

    TheCustomize toolboxDialog box opens.

  6. Click on the COM components tab and check Microsoft Web browser.

    The Web browser control labeledExplorerIs added to the Toolbox components.

  7. SelectExplorerComponent and click the open form.

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

  8. AddTextboxComponent above the browser component andListBoxComponent below it, accepting the default names of textbox1 and listbox1.
  9. AddButtonComponent to the right of listbox1. changeTextProperty to "Submit," and accept the default name of button1.

    The resulting form shoshould look similar to the following screen shot:

     

  10. Double-click on button1.

    TheButton#clickMethod is added to the project.

  11. Replace the body ofButton#clickMethod 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 clickEventsIcon in the Properties window.

    A list of web browser events appears.

  13. Double-clickDocument completeEvent.

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

    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)
  • Ihtmldocument2Http://msdn.microsoft.com/workshop/browser/mshtml/reference/ifaces/document2/document2.asp)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.