您现在的位置:中国下载站学院中心网络编程Delphi教程Delphi实例教程 → 文章列表

将网页内容保存为字符串以及根据字符串显示成网页

作者:佚名  来源:不详  发布时间:2006-12-29 13:13:31   

减小字体 增大字体

 
 
 

经常看到有人在论坛里问,如何将网页内容保存为字符串以及根据字符串显示成网页。这里说说我的看法。

将网页内容保存为字符串很简单,只需(webbrowser1.Document as Ihtmldocument2).body.outerHtml一句就可得到网页的html源码,一个WideString型。如果考虑到要等ie下载完整个网页后再保存为string可以这样写(在窗体中放置了一个memo控件以查看该页面的html源码)。

procedure tatonreg.readdocument;
var
  IpStream: IPersistStreamInit;
  MemoryStream: TMemoryStream;
begin
  ie为twebbrowser控件或为一个twebbrowser实例
  with ie do begin
    while (ReadyState <> READYSTATE_COMPLETE) and (not forms.Application.Terminated) do
    Forms.Application.ProcessMessages;
    if Assigned(document) then begin
      MemoryStream := TMemoryStream.Create;
      try
      IpStream := document as IPersistStreamInit;
      if not Assigned(IpStream) then showmessage('错误') else
      if Succeeded(IpStream.save(TStreamadapter.Create(A), TRUE))
        then begin
          MemoryStream.Seek(0, 0);
          memo1.lines.LoadFromStream(A);
        end;
      except
      end;
      MemoryStream.Free;
    end;
  end;
end;

程序运行后memo1.text就是该网页的源码了。
twebbrowser控件不能直接根据字符串显示成网页,必须先把string转成stream。

procedure tatonreg.loaddocument;
var
m:tmemorystream;
begin
  M := TMemoryStream.Create;
  memo1.Lines.SaveToStream(M);
  M.seek(0, 0);
  if not Assigned(ie.document) then begin
    ie.navigate('about:blank');
    while ie.readystate <> READYSTATE_COMPLETE do Forms.Application.ProcessMessages;
  end;
  (ie.Document as IPersistStreamInit).Load(TStreamadapter.Create(M));
end;


我做了一个这类的软件,可到我的个人主页上下载http://aton.126.com
这段时间我对ie编程产生了浓厚的兴趣。大有不精通这类编程誓不罢休之势。欢迎感兴趣着与我共同探讨。


itbulo.com/post.php?action=newthread&fid=51&extra=page%3D1" title="如有错误,麻烦请及时告诉我们,谢谢。" target="_blank">我要纠错】【itbulo.com/" target="_blank">进入论坛交流】【关闭此页】【iTbulo.net/" class="lblue" target="_blank">进入博客】

在百度中搜索更多将网页内容保存为字符串以及根据字符串显示成网页相关网页 转贴于:中国下载站

  • 上一篇文章:Delphi程序使用资源的释放
  • 下一篇文章:Delphi制作带图标的弹出式选单
  • 阅读统计:[]
  • 中国下载站】【设为主页】【收藏本页】【打印本文】【回到顶部】【关闭此页

    相关文章
    文章评论(评论内容只代表网友观点,与本站立场无关!)

    用户名: 查看更多评论

    分 值:100分 85分 70分 55分 40分 25分 10分 0分

    内 容:

             (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码


    设为首页 - 关于我们 - 广告服务 - 网站地图 - 加入收藏 - 网站声明 - 网站帮助 - 友情链接

    • Copyright (C) 2006-2008 www.cndownz.com All Rights Reserved.
      中国下载站 版权所有. 粤ICP备05141802号. 对本站有任何建议、意见或投诉,请来信:cndownzcom@yahoo.com.cn.
      喜欢中国下载站(cndownz.com),请把中国下载站(cndownz.com)告诉你QQ上的5位好友,多谢支持!