简单的webbrowser问题,在线等,解决后立即给分(50分)

  • 主题发起人 主题发起人 chchao1980
  • 开始时间 开始时间
C

chchao1980

Unregistered / Unconfirmed
GUEST, unregistred user!
我想获得webbrowser所显示的网页的html代码,并用memo显示到窗体中,请问如何实现
 

function Form1.ShowHtmls(mWebBrowser: TWebBrowser; mStrings: TStrings): Boolean;
var
vMemoryStream: TMemoryStream;
begin
Result := False;
if not (Assigned(mStrings) and Assigned(mWebBrowser)) then Exit;
mWebBrowser.Navigate('about:blank');
if not Assigned(mWebBrowser.Document) then Exit;
vMemoryStream := TMemoryStream.Create;
try
mStrings.SaveToStream(vMemoryStream);
try
vMemoryStream.Position := 0;
Application.ProcessMessages;
(mWebBrowser.Document as IPersistStreamInit).Load(TStreamAdapter.Create(vMemoryStream));
except
Exit;
end;
finally
vMemoryStream.Free;
end;
Result := True;
end;
 
var
IpStream: IPersistStreamInit;
AStream: TMemoryStream;
begin
AStream := TMemoryStream.Create;
IpStream := WebBrowser.Document as IPersistStreamInit;
if Assigned(IpStream) and Succeeded(IpStream.save(TStreamadapter.Create(AStream), TRUE)) then
begin
AStream.Seek(0, 0);
Memo.Lines.LoadFromStream(AStream);
end;
AStream.Free;
end;
 
var
doc2:IHTMLDocument2;
begin
WebBrowser1.Navigate('about:blank');
WebBrowser1.Navigate('*******');//浏览的网页地址
doc2:=WebBrowser1.Document as IHTMLDocument2;
memo1.lines.add(doc2.body.outhtml);//得到源代码
memo1.lines.add(doc2.body.outtext);//得到文本
end;
 
刚才的代码没有调试过有点问题,下面的是在delphi7下调试通过的。
在use里面加上MSHTML,
procedure TForm1.Button1Click(Sender: TObject);
var
doc2:IHTMLDocument2;
begin
WebBrowser1.Navigate('about:blank');
WebBrowser1.Navigate('http://www.163.com');
end;
procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
VAR
doc2:IHTMLDocument2;
begin
doc2:=WebBrowser1.Document as IHTMLDocument2;
memo1.lines.add(doc2.body.innerhtml);
end;
 
接受答案了.
 

Similar threads

后退
顶部