思路大概这个样子,把WebBrowser数据写到流里面,然后将流写入字符串,进行一部分替换以后,再将字符串写入流,再载入WebBrowser中。大概的功能就是,在某个网站提交表单中加入某个提交项,然后一起提交。本人刚学Delphi没多久,下面的问题没办法解决了,眼泪阿 T_T我贴出原码部分:______________________________________________________________________这段是从网上找来的LoadStream和 SaveDocumentSourceToStreamprocedure 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.imageshack.us/img226/7395/1zs7.jpghttp://img208.imageshack.us/img208/9917/2ba0.jpg最后一个最不明白~~为啥路径都变成about.blank:了。那个不是初始化的代码么 T-Thttp://img142.imageshack.us/img142/3694/3vf6.jpg还是我的思路根本就错了?望高人指点!
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.imageshack.us/img226/7395/1zs7.jpghttp://img208.imageshack.us/img208/9917/2ba0.jpg最后一个最不明白~~为啥路径都变成about.blank:了。那个不是初始化的代码么 T-Thttp://img142.imageshack.us/img142/3694/3vf6.jpg还是我的思路根本就错了?望高人指点!
我觉得就没有替换成功,你定义ss1:string,好象只能处理255各字符
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
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;
//---------------------
你先取源码,修改完源码后再载入