unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ActiveX, OleCtrls, SHDocVw, MSHtml, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

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;
begin
  Wnd := 1442892;

  GetIEFromHWND(Wnd, IE); // Get Iwebbrowser2 from Handle
  Document := IE.Document as IHTMLDocument2;
  ShowMessage(Document.url);
  if (assigned(Document)) then
  begin
    All := Document.All;
    for I := 0 to All.Length - 1 do
    begin
      HtmlElement := All.item(I, 0) as IHtmlElement;
      if (assigned(Document)) then
        Memo1.Lines.Add(IntToStr(I) + '' + HtmlElement.InnerHTML);
    end;
  end;

end;

end.
posted on 2017-04-16 21:21  万一  阅读(475)  评论(0)    收藏  举报