请教WebBrowser1的一个问题 ( 积分: 200 )

  • 主题发起人 主题发起人 renyp
  • 开始时间 开始时间
R

renyp

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.Button1Click(Sender: TObject);
begin
form2:=tform2.Create(self);
form2.Webbrowser1.Navigate(GetCurrentDir+'/Templates.html');
form2.Show;
(form2.WebBrowser1.Document as IHTMLDocument2).body.insertAdjacentHTML('beforeEnd','<div class=&quot;STYLE2&quot;>'+'任我飞扬'+' '+'10:00:00'+'</div>');
(form2.WebBrowser1.Document as IHTMLDocument2).body.insertAdjacentHTML('beforeEnd','<div>&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'+'你好'+'</div>');
end;
执行到
(form2.WebBrowser1.Document as IHTMLDocument2).body.insertAdjacentHTML('beforeEnd','<div class=&quot;STYLE2&quot;>'+'任我飞扬'+' '+'10:00:00'+'</div>');
就会报错,若是这样
procedure TForm1.Button1Click(Sender: TObject);
begin
form2:=tform2.Create(self);
form2.Webbrowser1.Navigate(GetCurrentDir+'/Templates.html');
form2.Show;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
(form2.WebBrowser1.Document as IHTMLDocument2).body.insertAdjacentHTML('beforeEnd','<div class=&quot;STYLE2&quot;>'+'任我飞扬'+' '+'10:00:00'+'</div>');
(form2.WebBrowser1.Document as IHTMLDocument2).body.insertAdjacentHTML('beforeEnd','<div>&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'+'你好'+'</div>');
end;
分开则可以正确执行,请问为什么?
 
Navigate方法是异步加载HTML页面的
执行Navigate后要等待它加载完页面,才能对body对象操作
 
那请问怎么处理,我加了这句也不行
while webbrowser1.Busy do
application.ProcessMessages;
 
可以这样
在Form.OnCreate事件中
WebBrowser1.Navigate('about:blank');

procedure TForm1.Button1Click(Sender: TObject);
var
lst : TStrings;
begin
lst := TStringList.Create;
lst.LoadFromFile(GetCurrentDir+'/Templates.html');
(WebBrowser1.Document as IHTMLDocument2).body.innerHTML := lst.Text;
lst.Free;
(WebBrowser1.Document as IHTMLDocument2).body.insertAdjacentHTML......
end;
 
不行,我是做类似QQ的显示接收到的消息用的,一接收到消息,则
ReceivedMsgForm:=FindWindowByUserID(recv_msg.user_id);
if ReceivedMsgForm=Nil then
Begin
ReceivedMsgForm:=TsendM_frm.Create(Application);
ReceivedMsgForm.Webbrowser1.Navigate(GetCurrentDir+'/Templates.html');
(WebBrowser1.Document as IHTMLDocument2).body.insertAdjacentHTML('beforeEnd','<div class=&quot;STYLE2&quot;>'+Msg.nikename+' '+TimeTostr(TimeOf(Msg.zx_time))+'</div>');
(WebBrowser1.Document as IHTMLDocument2).body.insertAdjacentHTML('beforeEnd','<div>&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'+msg.info+'</div>');
ReceivedMsgForm.show;
end;
请问有什么好方法?
 
不是一样的吗?
先Navigate空白页,然后用TStringList加载html页面,再把html内容赋值给body.innerHTML,最后insertAdjacentHTML,就不是异步的了
 
高手在给我看看,我刚这样测了下
procedure TForm1.Button1Click(Sender: TObject);
var
lst : TStrings;
begin
form2:=tform2.Create(self);
form2.WebBrowser1.Navigate('about:blank');
lst := TStringList.Create;
lst.LoadFromFile(GetCurrentDir+'/Templates.html');
(form2.WebBrowser1.Document as IHTMLDocument2).body.innerHTML := lst.Text;
lst.Free;
form2.Show;
end;
执行到(form2.WebBrowser1.Document as IHTMLDocument2).body.innerHTML := lst.Text;就报错了
 
Navigate是异步的,不能Navigate后直接引用Document as IHTMLDocument2
所以要你在OnCreate里Navigate('about:blank'),在Click事件中处理Document as IHTMLDocument2

如果一定要在同一过程中处理,可以在WebBrowser.OnProgressChange事件中写
if Progress = ProgressMax then
WebBrowser1.Tag := 1;

然后
WebBrowser1.Tag := 0;
WebBrowser1.Navigate('about:blank');
while WebBrowser1.Tag <> 1 do
Application.ProcessMessages;
(WebBrowser1.Document as IHTMLDocument2).body......
 
我这样试了,
procedure TForm2.WebBrowser1ProgressChange(Sender: TObject; Progress,
ProgressMax: Integer);
begin
if Progress = ProgressMax then
WebBrowser1.Tag := 1;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
form2:=tform2.Create(self);
form2.WebBrowser1.Tag := 0;
form2.WebBrowser1.Navigate('about:blank');
while form2.WebBrowser1.Tag <> 1 do
Application.ProcessMessages;
(form2.WebBrowser1.Document as IHTMLDocument2).body.insertAdjacentHTML('beforeEnd','<div class=&quot;STYLE2&quot;>'+'任我飞扬'+' '+'10:00:00'+'</div>');
form2.Show;
end;

form2就调不出来了,看来的换思路了
 
procedure TForm1.Button1Click(Sender: TObject);
begin
form2:=tform2.Create(self);
form2.Webbrowser1.Navigate(GetCurrentDir+'/Templates.html');
form2.Show;
while WebBrowser1.ReadyState <> READYSTATE_COMPLETE do
Application.ProcessMessages;

(form2.WebBrowser1.Document as IHTMLDocument2).body.insertAdjacentHTML('beforeEnd','<div class=&quot;STYLE2&quot;>'+'任我飞扬'+' '+'10:00:00'+'</div>');
(form2.WebBrowser1.Document as IHTMLDocument2).body.insertAdjacentHTML('beforeEnd','<div> '+'你好'+'</div>');
end;
 
好了 解决的 谢谢
 
后退
顶部