Delphi獲取所有開啟的IE視窗的網頁原始碼

一劍平江湖發表於2013-12-10
procedure TForm1.Button1Click(Sender: TObject);
var
  ShellWindow: IShellWindows;
  i: Integer;
  spDisp: IDispatch;
  IE1: IWebBrowser2;
begin
  ShellWindow := CoShellWindows.Create;
  for i:=0 to ShellWindow.Count - 1 do
  begin
    spDisp := ShellWindow.Item(i);
    if spDisp <> nil then
    begin
      spDisp.QueryInterface(iWebBrowser2, IE1);
      if IE1 <> nil then
      begin
        if Pos('http://', LowerCase(IE1.LocationURL)) = 1 then
        begin
          Memo1.Lines.Add(IE1.LocationURL); //獲取網址
          Memo1.Lines.Add((IE1.Document as IHtmlDocument2).body.outerHTML); //獲取原始碼
        end;
      end;
    end;
  end;
end;

相關文章