Use of webbrowser component and mshtml in Delphi-convert from http://www.hackpig.cn/post/294.html

Source: Internet
Author: User

Mshtml is a COM component of Microsoft. It encapsulates all elements and their attributes in the HTML language and can access all elements of a specified Webpage through the standard interface provided by mshtml.

The mshtml object model consists of some objects and sets. html at the root describes a window that opens a page, including a series of sets and objects. Such as the frames set, history, location, Navigator, document, Vi-sum, and event object. The document object is used to describe the actual webpage in the customer window. It consists of a series of attributes, methods, objects, and sets. The All set contains all tag elements in the webpage. Its main methods and attributes include:

(1) length (length): indicates the number of tags that appear. You can refer to the set of tags as a one-dimensional array starting from 0. The order of tags is arranged in the webpage position;

(2) Tags (TAG): used to filter the set of given tags, such as Doc. al1.tags (p) to get all the segment tags P;

(3) item: used to select an element in the set, such as object. item (0) gets 1st elements of the Set, and object. item (I) obtains the I + 1 element.

In addition, ihtmlelement is a common set object that represents the set of specified tags in the webpage. Through this set object, you can obtain the content of specific tags on the webpage. ihtmlelement has four main attributes:

(1) innertext: the text between the start mark and the end mark;

(2) innerhtml: Text and HTML between the start tag and the end tag;

(3) outertext: Object text;

(4) outerhtml: Object text and HTML.

Note: before use, add the mshtml unit.

Demo form submission

Procedure tform1.button1click (Sender: tobject );
VaR
DOC: ihtmldocument2;
Input: olevariant;
Userinputelement, pwdinputelement: ihtmlinputelement;
Begin
DOC: descriwebbrowser1.doc ument as ihtmldocument2;
Userinputelement: = (Doc. All. Item ('user' (that is, the name of the user name control in the webpage), 0) as ihtmlinputelement );
Userinputelement. Value: = edit1.text; (that is, what you want to input to the webpage)
Pwdinputelement: = (Doc. All. Item ('Password', 0) as ihtmlinputelement );
Pwdinputelement. Value: = edit2.text;
Input: = Doc. All. Item ('submit ', 0 );
Input. Click;
End;

When the data submission button does not have the name attribute, use the following method:

Procedure tform1.button1click (Sender: tobject );
VaR
DOC: ihtmldocument2;
Form: ithmlformelement;
Userinputelement, pwdinputelement: ihtmlinputelement;
Begin
DOC: descriwebbrowser1.doc ument as ihtmldocument2;
Userinputelement: = (Doc. All. Item ('user' (that is, the name of the user name control in the webpage), 0) as ihtmlinputelement );
Userinputelement. Value: = edit1.text; (that is, what you want to input to the webpage)
Pwdinputelement: = (Doc. All. Item ('Password', 0) as ihtmlinputelement );
Pwdinputelement: = edit2.text;
Form: = (Doc. All. Item ('login _ Form', 0) as ihtmlformelement ):
Form. Submit;
End;

The logon button is usually the default carriage return button on the webpage.CodeTo replace the previous button

Read the content of a webpage

Open a webpage when creating a form

 

Webbrowser1.navigate ('HTTP: // www.baidu.com ')

Procedure tform1.button1click (Sender: tobject );

Begin

Memo1.lines.add(ihtmldocument2(webbrowser1.doc ument). Body. outerhtml );

End;

 

In this way, memo1 displaysWww.baidu.comHtml

Use the webbrowser1 control to browse the content of a TXT file and display it in HTML format.

Add the following code to the ondocumentcomplete event in the webbrowser1 control:

Procedure tform1.webbrowser1documentcomplete (Sender: tobject;

Const Pdisp: idispatch; var URL: olevariant );

VaR

Webdoc: htmldocument;

Webbody: htmlbody;

Begin

Webdoc: Invalid webbrowser1.doc ument as htmldocument;

Webbody: = webdoc. Body as htmlbody;

Webbody. insertadjacenthtml ('beforeend', '<form method = "Post" Action = ""> ');

Webbody. insertadjacenthtml ('beforeend', 'password :');

Webbody. insertadjacenthtml ('beforeend', '<input type = "password"> ');

Webbody. insertadjacenthtml ('beforeend', '<input type = "Submit" value = "login"> ');

Webbody. insertadjacenthtml ('beforeend ','');

Webbody. insertadjacenthtml ('beforeend', '</form> ');

End;

 

 

When reading, the webbrowser control must completely open a webpage,ProgramThe Code is as follows:

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.