[Delphi] how to access webbrowser in Internet Explorer

Source: Internet
Author: User

The following is an example of how to obtain the HTML of its webpage element from Internet Explorer.Source code:

Const
Rspsimpleservice = 1;
Rspunregisterservice = 0;

Type
Tobjectfromlresult = function (lresult: lresult; const IID: tiid; wparam: wparam; out pobject): hresult; stdcall;

Function getiefromhwnd (whandle: hwnd; var ie: iwebbrowser2): hresult;
VaR
Hinst: hwnd;
Lres: Cardinal;
MSG: integer;
Pdoc: ihtmldocument2;
Objectfromlresult: tobjectfromlresult;
Begin
Hinst: = loadlibrary ('oleacc. dll ');
@ Objectfromlresult: = getprocaddress (hinst, 'objectfromlresult ');
If @ objectfromlresult <> nil then
Begin
Try
MSG: = registerwindowmessage ('wm _ html_getobject ');
Sendmessagetimeout (whandle, MSG, 0, 0, smto_abortifhung, 1000, lres );
Result: = objectfromlresult (lres, ihtmldocument2, 0, pdoc );
If result = s_ OK then
(Pdoc. parentwindow as iserviceprovider). queryservice (iwebbrowserapp, iwebbrowser2, ie );
Finally
Freelibrary (hinst );
End;
End;
End;

Procedure tform1.button1click (Sender: tobject );
VaR
ALL: ihtmlelementcollection;
Htmlelement: ihtmlelement;
I: integer;
Document: ihtmldocument2;
IE: iwebbrowser2;
WND: hwnd;
Wndchild: hwnd;
Begin
WND: = findwindow ('ieframework', nil );
If WND = 0 then
Begin
Messagedlg ('no running instance of Internet Explorer! ', Mterror, [mbok], 0 );
End;
// Walk shell docobject view-> Internet assumer_server
Wndchild: = findjavaswex (WND, 0, 'Shell docobject view', nil );
If wndchild <> 0 then
Begin
Wndchild: = find1_wex (wndchild, 0, 'Internet assumer_server ', nil );
If wndchild <> 0 then
Begin
Getiefromhwnd (wndchild, ie); // get iwebbrowser2 from handle
Document: = ie. Document as ihtmldocument2;
If assigned (document) then
Begin
ALL: = Document. All;
For I: = 0 to all. Length-1 do
Begin
Htmlelement: = all. Item (I, 0) as ihtmlelement;
Sourceview. lines. Add (inttostr (I) + ''+ htmlelement. innerhtml );
End;
End;
End;
End;
End;

Related Article

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.