Delphi access to web interface via IE Window handle (IWebBrowser2)

Source: Internet
Author: User

The main use is the MSAA (Microsoft Active Accessibility) function: Objectfromlresult, which is defined in the dynamic link library Oleacc.dll.

Uses SHDocVw, MsHtml, ActiveX;

type
Tobjectfromlresult = function (lresult:lresult; const IID:TIID; Wparam:wparam; Out pobject): HRESULT; stdcall;

functionGetiefromhwnd (Whandle:hwnd; var ie:iwebbrowser2): HRESULT;
var
Hinst:hwnd;
lres:cardinal;
Msg:integer;
Pdoc:ihtmldocument2;
Objectfromlresult:tobjectfromlresult;
begin
Result: = S_FALSE;
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, 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;

To invoke the example, the following code quickly closes all open ie windows:

procedureTform1.button1click (Sender:tobject);
var
Hcurwindow, Hmainwnd, Htabwnd, Hcldwnd:hwnd;Window handle
WINCLSNAME:ARRAY[0..255] of Char;
Ie1:iwebbrowser2;
begin
Hcurwindow: = GetWindow (Handle,gw_hwndfirst);Gets the handle of the first window
While hcurwindow<>0 do
Begin
GetClassName (Hcurwindow, @WinClsName, 255);
If String (winclsname) = ' IEFrame ' Then
Begin
Hmainwnd: = Hcurwindow;
Hcldwnd: = Hcurwindow;
Htabwnd: = 0;
RepeatLoop Find all Tabs
Htabwnd: = FindWindowEx (Hmainwnd, Htabwnd, ' Frame Tab ', nil);
If Htabwnd <> 0 then Hcldwnd: = FindWindowEx (htabwnd, 0, ' Tabwindowclass ', nil);
If Hcldwnd <> 0 then Hcldwnd: = FindWindowEx (hcldwnd, 0, ' Shell docobject View ', nil);
If Hcldwnd <> 0 then Hcldwnd: = FindWindowEx (hcldwnd, 0, ' Internet explorer_server ', nil);
If Hcldwnd <> 0 Then if Getiefromhwnd (Hcldwnd, IE1) = S_OK ThenGet IWebBrowser2
Begin
IE1. Quit; //Close IE, can also perform other operations, hehe
End
Until Htabwnd = 0;
End
Hcurwindow:=getwindow (Hcurwindow,gw_hwndnext); //Get handle to the next window
End
End

Delphi access to web interface via IE Window handle (IWebBrowser2)

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.