Twebbrowser and mshtml (1): Get the window object in Dom from twebbrowser

Source: Internet
Author: User
With the help of twebbrowser, you can make the software more beautiful and flexible. Many software have done this for a long time.

Although the HTML Dom content is complex (involving HTML, JavaScript, and CSS), it also belongs to the functional scope of twebbrowser.

When using twebbrowser, if mshtml is used together, it will be good.CodeTip; otherwise, it is difficult to write code.

Everything in the HTML Dom comes from a window object. In order to be consistent with the DOM in JS, this object is obtained first.

Twebbrowser is used to call the shdocvw. dll function and inherit from the tolecontrol class. It will use the shdocvw and olectrls units.

In addition, window is of the ihtmlwindow2 interface type, which is defined in the mshtml unit.

In this example:


Code file:
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, olectrls, shdocvw, stdctrls; Type tform1 = Class (tform) webbrowser1: twebbrowser; button1: tbutton; button2: tbutton; Procedure upload (Sender: tobject); Procedure button1click (Sender: tobject); Procedure upload (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} uses MSH TML; var window: ihtmlwindow2; {open a page} procedure tform1.button1click (Sender: tobject); begin webbrowser1.navigate ('HTTP: // del.cnblogs.com '); end; {Get and try window object} procedure tform1.button2click (Sender: tobject); begin window: = (webbrowser1.document as ihtmldocument2 ). parentwindow; parameter Doc ument. body. innerhtml: = '<HR> in case of a Delphi blog <HR>'; window. alert ('javascript prompt: Modification successful! '); End; {initialization} procedure tform1.formcreate (Sender: tobject); begin position: = poscreencenter; webbrowser1.align: = altop; button1.caption: = 'open'; button2.caption: = 'overwrite'; end.
 
   
 

Form file:

Object form1: tform1 left = 0 Top = 0 caption = 'form1' clientheight = 167 clientwidth = 318 color = clbtnface font. charset = default_charset font. color = clwindowtext font. height =-11 font. name = 'tahoma 'font. style = [] oldcreateorder = false oncreate = formcreate pixelsperinch = 96 textheight = 13 object webbrowser1: twebbrowser left = 8 Top = 8 width = 300 Height = 121 taborder = 0 controldata = {4c000000021f0000810c000000000000000000000000000000000000000000000000 0000000000008000000000000000000000000000000000000000000000000000000000000000} end object button1: tbutton left = 154 Top = 135 width = 75 Height = 25 caption = 'button1' taborder = 1 onclick = button1click end object button2: tbutton left = 235 top = 135 width = 75 Height = 25 caption = 'button2' taborder = 2 onclick = button2click endend
 
   
 

You can also obtain the window object from an external ie window:

 
Uses mshtml, comobj; var ie: variant; window: ihtmlwindow2; Procedure tform1.button1click (Sender: tobject); begin ie: = createoleobject ('internetexplorer. application '); IE. visible: = true; IE. navigate ('HTTP: // del.cnblogs.com '); end; Procedure tform1.button2click (Sender: tobject); begin window: = (idispatch(ie.doc ument) as ihtmldocument2 ). parentwindow; parameter Doc ument. body. innerhtml: = '<HR> in case of a Delphi blog <HR>'; end;

  

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.