分享

关于WebBrowser使用的问题,从流中载入出问题 = = 求教高人指点

 quasiceo 2014-07-14

关于WebBrowser使用的问题,从流中载入出问题 = = 求教高人指点 [问题点数:100分,结帖人Anikox]

Anikox
关注
Anikox
Anikox
等级:Blank
结帖率:83.33%
楼主 发表于: 2006-10-27 10:32:27
思路大概这个样子,把WebBrowser数据写到流里面,然后将流写入字符串,进行一部分替换以后,再将字符串写入流,再载入WebBrowser中。大概的功能就是,在某个网站提交表单中加入某个提交项,然后一起提交。

本人刚学Delphi没多久,下面的问题没办法解决了,眼泪阿 T_T

我贴出原码部分:

______________________________________________________________________

这段是从网上找来的LoadStream和 SaveDocumentSourceToStream

procedure  LoadStream(WebBrowser:  TWebBrowser;  Stream:  TStream);
var
PersistStreamInit:  IPersistStreamInit;
StreamAdapter:  IStream;
MemoryStream:  TMemoryStream;
begin
{Load  empty  HTML  document  into  Webbrowser  to  make  "Document"  a  valid  HTML  document}
WebBrowser.Navigate('about:blank');
{wait  until  finished  loading}
repeat
Application.ProcessMessages;
Sleep(0);
until
WebBrowser.ReadyState  =  READYSTATE_COMPLETE;
{Get  IPersistStreamInit  -  Interface}
if  WebBrowser.Document.QueryInterface(IPersistStreamInit,  PersistStreamInit)  =  S_OK  then
begin
{Clear  document}
if  PersistStreamInit.InitNew  =  S_OK  then
begin
{Make  local  copy  of  the  contents  of  Stream  if  you  want  to  use  Stream  directly,  you  have  to
consider,  that  StreamAdapter  will  destroy  it  automatically}
MemoryStream:=  TMemoryStream.Create;
try
MemoryStream.CopyFrom(Stream,  0);
MemoryStream.Position:=  0;
except
MemoryStream.Free;
raise;
end;
{Use  Stream-Adapter  to  get  IStream  Interface  to  our  stream}
StreamAdapter:=  TStreamAdapter.Create(MemoryStream,  soOwned);
{Load  data  from  Stream  into  WebBrowser}
PersistStreamInit.Load(StreamAdapter);
end;
end;
end;



procedure  SaveDocumentSourceToStream(Document:  IDispatch;  Stream:  TStream);
var
PersistStreamInit:  IPersistStreamInit;
StreamAdapter:  IStream;
begin
Stream.Size  :=  0;
Stream.Position  :=  0;
if  Document.QueryInterface(IPersistStreamInit,  PersistStreamInit)  =  S_OK  then
begin
StreamAdapter  :=  TStreamAdapter.Create(Stream,  soReference);
PersistStreamInit.Save(StreamAdapter,  False);
StreamAdapter  :=  nil;
end;
end;

_____________________________________________________________


下面是替换功能部分:


procedure TForm1.Button2Click(Sender: TObject);
var
  SS: TStringStream;
  SS3: TStringStream;
  Document: IDispatch;
  ss1 : String;
  ss2 : String;

begin
  SS := TStringStream.Create('');
  try
  SaveDocumentSourceToStream(WebBrowser1.Document, SS);
   ss1 := SS.DataString ;

    ss2 := AnsiReplaceText(ss1,'</form>','加入的代码</form>');
    SS.Free;
    SS3 := TStringStream.Create(ss2);
    LoadStream(WebBrowser1, SS3);
  finally
    SS3.Free;
  end;
end;


——————————————————————————————————————————


问题是~~替换是能替换~~~可是显示出来的可就不是那么回事了:

http://img226./img226/7395/1zs7.jpg

http://img208./img208/9917/2ba0.jpg

最后一个最不明白~~为啥路径都变成about.blank:了。。。。那个不是初始化的代码么 T-T

http://img142./img142/3694/3vf6.jpg

还是我的思路根本就错了?

望高人指点!
回复次数:4
zzq4823
关注
zzq4823
zzq4823
等级:Blank
#1 得分:0 回复于: 2006-10-27 11:16:45
你怎么确定     ~~替换是能替换~~~
我觉得就没有替换成功,你定义ss1:string,好象只能处理255各字符
jiangsheng
关注
jiangsheng 版主
蒋晟
等级:Blank
11
更多勋章
#2 得分:60 回复于: 2006-10-27 13:57:37
The easiest way to do this is to embed a <base> tag into the generated 
HTML. You don't have to save it to disk, or make visible to the user, 
just feed it in the stream with the rest of the content. 

Another way is to write a custom implementation of IMoniker interface. 
You only need a non-trivial implementation of two methods: BindToStorage 
should return the IStream with your HTML content, and GetDisplayName 
should return the base URL you want to use to resolve relative links. 
You then use IPersistMoniker to feed the content into MSHTML using this 
custom implementation, instead of IPersistStreamInit. Disclaimer: I have 
not done this myself, but I've seen people reporting successful use of 
this technique. 
-- 
With best wishes, 
    Igor Tandetnik 


happyggy
关注
happyggy
happyggy
等级:Blank
#3 得分:40 回复于: 2006-10-27 15:14:26
//get html 源码
function GetHTMLCode(WB: IWebbrowser2; ACode: TStrings): Boolean;
var
  ps: IPersistStreamInit;
  s: string;
  ss: TStringStream;
  sa: IStream;
begin
  ps := WB.document as IPersistStreamInit;
  s := '';
  ss := TStringStream.Create(s);
  try
    sa := TStreamAdapter.Create(ss, soReference) as IStream;
    Result := Succeeded(ps.Save(sa, Bool(True)));
    if Result then ACode.Add(ss.Datastring);
  finally
    ss.Free;
  end;
end;
//webbrowser 载入html源码
procedure WB_LoadHTML(WebBrowser: TWebBrowser; HTMLCode: string);
var
  sl: TStringList;
  ms: TMemoryStream;
begin
  if Assigned(WebBrowser.Document) then
  begin
    sl := TStringList.Create;
    try
      ms := TMemoryStream.Create;
      try
        sl.Text := HTMLCode;
        sl.SaveToStream(ms);
        ms.Seek(0, 0);
        (WebBrowser.Document as IPersistStreamInit).Load(TStreamAdapter.Create(ms));
      finally
        ms.Free;
      end;
    finally
      sl.Free;
    end;
  end;
end;
//---------------------
你先取源码,修改完源码后再载入
Anikox
关注
Anikox
Anikox
等级:Blank
#4 得分:0 回复于: 2006-10-31 15:06:20
终于弄好了~~开始我的方法也是对的~~只是一个相对路径的问题在捣乱啊~~

谢谢答复的各位了 ^^

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多