用DELPHI怎样得到当前打开IE窗口中的HTML源码?调通后马上发分。。。(300分)

  • 主题发起人 主题发起人 fpsky
  • 开始时间 开始时间
var
SWs:TShellWindows;//windows的IE窗口
IE:TInternetExplorer;//当前的IE窗口
begin
SWs:=TShellWindows.Create(nil);
IE:=TInternetExplorer.Create(nil);
.....
end;
 
3ku only you

可以具体点吗?
 
我已经调试通过,但是在选择浏览器窗口上有问题
下面是全部源代码

unit Unit1;

interface

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

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

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
IE := TInternetExplorer.Create(Self);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
vDoc: OleVariant;
IpStream: IPersistStreamInit;
Stm: TMemoryStream;
st: TStrings;
begin
IE.ConnectKind := ckRunningInstance;
IE.Connect;
vDoc := IE.Document;
if Assigned(IDispatch(vDoc)) then
begin
Stm := TMemoryStream.Create;
st := TStringList.Create;
IpStream := IDispatch(vDoc) as IPersistStreamInit;
IpStream.save(TStreamAdapter.Create(Stm), TRUE);
Stm.Position := 0;
st.LoadFromStream(Stm);
Stm.Free;
Memo1.Lines := st;
st.Free;
end;
end;

end.
 
可以创建TShellWindows的实例 来枚举所有的浏览器窗口
然后用IE.ConnectTo(svrIntf: IWebBrowser2) 来选择所需窗口
 
最后修正的程序,注意这一句
IE.ConnectTo(Ws.Item(0) as IWebBrowser2);
改变Ws.Item() 的参数可以遍历IE实例,
Ws.Count为所有实例的个数

unit Unit1;

interface

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

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

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
IE := TInternetExplorer.Create(Self);
Ws := TShellWindows.Create(Self);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
vDoc: OleVariant;
IpStream: IPersistStreamInit;
Stm: TMemoryStream;
st: TStrings;
begin
Ws.Connect;
Caption := 'IE Count: ' + IntToStr(Ws.Count);
IE.ConnectKind := ckRunningInstance;
//改变参数连接到任何一个IE实例
IE.ConnectTo(Ws.Item(0) as IWebBrowser2);
vDoc := IE.Document;
if Assigned(IDispatch(vDoc)) then
begin
Stm := TMemoryStream.Create;
st := TStringList.Create;
IpStream := IDispatch(vDoc) as IPersistStreamInit;
IpStream.save(TStreamAdapter.Create(Stm), TRUE);
Stm.Position := 0;
st.LoadFromStream(Stm);
Stm.Free;
Memo1.Lines := st;
st.Free;
end;
end;

end.
 
可以这样判断窗口是否为浏览器窗口
if Assigned(IDispatch(vDoc)) and Supports(vDoc, IHtmlDocument2) then
 
好,正在试
 
//------------------------------------------------------------------------------
procedure TfrmMain.SaveHTMLSourceToFile(const FileName: string;WB: TWebBrowser);
var
PersistStream: IPersistStreamInit;
FileStream: TFileStream;
Stream: IStream;
SaveResult: HRESULT;
begin
PersistStream := WB.Document as IPersistStreamInit;
FileStream := TFileStream.Create(FileName, fmCreate);
try
Stream := TStreamAdapter.Create(FileStream, soReference) as IStream;
SaveResult := PersistStream.Save(Stream, True);
if FAILED(SaveResult) then
MessageBox(Handle, 'Fail to save HTML source', 'Error', mb_iconerror);
finally
FileStream.Free;
end;
end;
 
我都是调通后才贴出来的,
你现在还没有调通吗?
 
我已经找到更简单的办法,谢谢各位了
var
Form1: TForm1;
NMHTTP1:TNMHTTP;

implementation

{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
NMHTTP1.Get('www.hrbco.com');
Memo1.Text := NMHTTP1.Body;
end;
 
你他妈的,问的问题有毛病!
你这种方法谁都会!
 
后退
顶部