Access dhtml dom objects from C)

Source: Internet
Author: User

Abstract:This article describes how to use the Microsoft Web browser control and Microsoft Document Object Model (DOM) (Document Object Model) to access every element of the Web by programming.

To access web page elements, you must first reference the web browser control to apply its attributes, objects, methods, and events. By calling the navigate method, you can use web browser to open the page. But useProgramTo access the file, you must wait until all the documents have been downloaded. That is, the complete event of the document occurs. Then, you can convert the document attribute of the Web browser object to the ihtmldocument2 interface object, in this way, you can access the collection of this object, such as the link collection and image collection. These sets will return ihtmlelementcollection objects. This article uses the link set as an example to illustrate how to return all links to the URL page you specified.

The following is a simple process to implement this function:

1. Open Microsoft Visual Studio. NET and create a new Visual C # windows application. The default name is form1.

2. In Solution Explorer, right-click the references folder and select Add reference, the add reference dialog box is displayed.

3. Click the. NET tab, double-click the Microsoft. mshtml component, and click OK.

4. Open toolbox, right-click any tool item, and select customize toolbox. The custom Toolbar Dialog Box is displayed.

5. Click the COM components tab to select the check box before Microsoft Web browser. Click OK. The Web browser control named explorer is added to the toolbox.

6. Select the explorer control and add it to the form1 form. The default name is axwebbrowser1.

7. add textbox on the browser control and ListBox under the browser control. The default textbox1 and listbox1 are respectively named. The text attribute of textbox1 is set to http: // lucky_elove.www1.dotnetplayground.com/; add a button after the textbox, and change text to "Browse page" with the name button1. The page is displayed as follows:

8. Double-click button1 and enterCode:

<xmp> Private void button#click (Object sender, system. eventargs e) <br/>{< br/> Object zero = 0; <br/> Object emptystring = ""; <br/> axwebbrowser1.navigate (textbox1.text, ref zero, ref emptystring, ref emptystring, ref emptystring); <br/>}< br/> </xmp>

9. Switch the form to the design mode, select the browser control, and click the "event" icon in the Properties window. The web browser event dialog box is displayed. Double-click the document complete event, in the event processing process of axwebbrowser1_documentcomplete, enter the following code:

<xmp> <Br/> private void axwebbrowser1_documentcomplete (Object sender, <br/> axshdocvw. dwebbrowserevents2_documentcompleteevent e) <br/>{< br/> ihtmldocument2 htmldocument = (ihtmldocument2) axwebbrowser1.document; <br/> ihtmlelementcollection links = htmldocument. links; </P> <p> listbox1.items. clear (); </P> <p> foreach (htmlanchorelementclass El in links) <br/>{< br/> listbox1.items. add (El. outerhtml); <br/>}< br/> </xmp>

10. At the top of the form1.cs file, enter:

<xmp> Using mshtml; </xmp>

11. Press F5 to run the form and click "Browse page". The following result will be displayed. Is it nice :)

SRC: http://blog.csdn.net/derny/archive/2004/08/30/89276.aspx

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.