怎样访问Internet Explorer中的WebBrowser

[Delphi]怎样访问Internet Explorer中的WebBrowser [转贴 2009-12-30 19:10:38]   

我顶 字号:大  小

下面就是一个例子展示如何从打开的Internet Explorer中取得其网页元素的HTML源代码:



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('IEFrame', nil);
  if Wnd = 0 then
  begin
    MessageDlg('No Running instance of Internet Explorer!', mtError, [mbOK], 0);
  end;
  // walk Shell DocObject View->Internet Explorer_Server
  WndChild := FindWindowEX(Wnd, 0, 'Shell DocObject View', nil);
  if WndChild <> 0 then
  begin
    WndChild := FindWindowEX(WndChild, 0, 'Internet Explorer_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;


uses MSHTML,ActiveX 


TIID可能是TGUID  


WndChild := FindWindowEX(Wnd, 0, 'Shell DocObject View', nil); 
此句在IE7下获取不了句柄呀? 
有没有人在IE7下获取'Shell DocObject View'的句柄的例子?  


以下是TestComplete的脚本,可见IE 7下多了一个TabWindowClass,你应该知道该怎么做了吧! 

p = Sys.WaitProcess("iexplore"); 
w = p.WaitWindow("IEFrame", "*", -1); 
if (! w.Exists) 
p = Sys.WaitProcess("explorer"); 
// IE should be running now... 
if (p.Exists) { 
w = p.Window("IEFrame", "*",1); 
// Give IE 3 seconds to start, check for the IE7 TabWindowClass first... 
if (w.WaitWindow("TabWindowClass", "*", -1, 3000).Exists) 
ie = w.Window("TabWindowClass", "*").Window("Shell DocObject View").Window("Internet Explorer_Server", "", 1); 
else 
ie = w.Window("Shell DocObject View").Window("Internet Explorer_Server", "", 1); 
page = ie.Page("*"); 


非常感谢您 Icebird,我在 
WndChild := FindWindowEX(Wnd, 0, 'Shell DocObject View', nil); 
前面增加了一句代码, 
WndTab := FindWindowEX(Wnd, 0, 'TabWindowClass', nil); 
就可以在IE7下获取句柄了。 


转载于:https://my.oschina.net/u/1777508/blog/361147

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值