加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Delphi通过IE窗口句柄获取网页接口(IWebBrowser2)

发布时间:2020-12-15 10:02:15 所属栏目:大数据 来源:网络整理
导读:主要用到的是MSAA(Microsoft Active Accessibility) 函数:ObjectFromLResult,该函数在动态链接库 oleacc.dll 中定义。 uses SHDocVw,MsHtml,ActiveX; type ? TObjectFromLResult = function(LRESULT: lResult; const IID: TIID; WPARAM: wParam; out pObje

主要用到的是MSAA(Microsoft Active Accessibility) 函数:ObjectFromLResult,该函数在动态链接库 oleacc.dll 中定义。

uses SHDocVw,MsHtml,ActiveX;

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
??
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,SMTO_ABORTIFHUNG,1000,lRes);
????? Result := ObjectFromLresult(lRes,IHTMLDocument2,pDoc);
????? if Result = S_OK then
??????? (pDoc.parentWindow as IServiceprovider).QueryService(IWebbrowserApp,IWebbrowser2,IE);
??? finally
????? FreeLibrary(hInst);
??? end;
? end;
end;

调用例子,以下代码快速关闭所有打开的IE窗口:

procedure?TForm1.Button1Click(Sender: TObject);
var
??? hCurWindow,hMainWnd,hTabWnd,hCldWnd:HWnd;?//窗口句柄
??? WinClsName:array[0..255] of char;
??? IE1: IWebbrowser2;
begin
??? hCurWindow := GetWindow(Handle,GW_HWNDFirst);??//获取第一个窗口的句柄
??? while hCurWindow<>0 do
??? begin
?????? GetClassName(hCurWindow,@WinClsName,255);
?????? if String(WinClsName) = 'IEFrame' then
?????? begin
???????? hMainWnd := hCurWindow;
???????? hCldWnd := hCurWindow;
???????? hTabWnd := 0;
???????? repeat?//循环查找所有选项卡
?????????? hTabWnd := FindWindowEx(hMainWnd,'Frame Tab',nil);
?????????? if hTabWnd <> 0 then hCldWnd := FindWindowEx(hTabWnd,'TabWindowClass',nil);
?????????? if hCldWnd <> 0 then hCldWnd := FindWindowEx(hCldWnd,'Shell DocObject View','Internet Explorer_Server',nil);
?????????? if hCldWnd <> 0 then if GetIEFromHWnd(hCldWnd,IE1) = S_OK then?//获取IWebBrowser2
?????????? begin
???????????? IE1.Quit;?//关闭IE,也可以执行其他操作,呵呵
?????????? end;
???????? until hTabWnd = 0;
?????? end;
?????? hCurWindow:=GetWindow(hCurWindow,GW_HWNDNEXT);?//获取下一个窗口的句柄
??? end;
end;?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读