在我的程序中使用了webbrowser控件,
1、我想动态生成网页内容然后在webbrowser中载入,应该怎么办,我指不生成临时文件的方法。
2、网页中有很多单选框和文本框等表单元素,当使用者作出选择或输入数据在文本框中后,我在程序中如何获取网页中使用者所选择的或所输入的数据(每个元素可以有不同的ID).
3、有没有办法在用户点击webbrowser网页中表单元素能触发自定义的事件?
4、希望能给出webbowser 较为详尽的使用资料,这方面介绍的比较少,好多是仅某个方面的介绍。以上最好能给出示例代码,则不胜感谢。
1、我想动态生成网页内容然后在webbrowser中载入,应该怎么办,我指不生成临时文件的方法。
2、网页中有很多单选框和文本框等表单元素,当使用者作出选择或输入数据在文本框中后,我在程序中如何获取网页中使用者所选择的或所输入的数据(每个元素可以有不同的ID).
3、有没有办法在用户点击webbrowser网页中表单元素能触发自定义的事件?
4、希望能给出webbowser 较为详尽的使用资料,这方面介绍的比较少,好多是仅某个方面的介绍。以上最好能给出示例代码,则不胜感谢。
2 遍历DHTML DOM找到输入框,取得IHTMLInputElement的value属性就可以了。
3 很多元素的事件可以设置的,但是body.onload不行,因为只有当文档载入完成之后才可以访问DHTML DOM。
4 参见
http://msdn.microsoft.com/workshop/browser/hosting/hosting.asp
http://community.csdn.net/Expert/topic/3398/3398782.xml?temp=.463711
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;{ TForm1 }procedure TForm1.Button1Click(Sender: TObject);
var
S: TStringStream;
begin
S:= TStringStream.Create('<html><h1>Stream Test</h1><p>:This HTML content ' +
'is being loaded from a stream.</html>');
try
LoadStream(WebBrowser1, S);
finally
S.Free;
end;
<input type=radio id=R2B name=R2 value=B>
<input type=radio id=R2C name=R2 value=C>
<input type=radio id=R2D name=R2 value=D>
获取四个单选框到底选择的是哪一个,比如选B就得到“B”,下面的代码不行,应该如何写呢?
with WebBrowser1 do begin
D := Document as IHTMLDocument2;
Form := D.Forms.item('myform',0) as IHTMLFormElement;
s:=(form.item('r2',0) as IHTMLElement).getAttribute('value',0);
caption:=S;
showmessage(s);
end;
end;
var
Docs, ovElements: OleVariant;
i: integer;
begin
Docs := Webbrowser1.document as IHTMLDocument2;
ovElements := Docs.all.tags('INPUT');
for i := 0 to (ovElements.Length - 1) do
if (UpperCase(ovElements.item(i).type) = 'RADIO') and (UpperCase(ovElements.item(i).name) = 'R2') and (ovElements.item(i).CHECKED) then
begin
showMessage(ovElements.item(i).Value);
end;
end;
o:OleVariant;
sPath:pchar;
begin
o := WebBrowser1.OleObject.document.all.item('user_id',0); //找到登录用户名的输入框
o.value := 'TEST';
o := WebBrowser1.oleobject.document.all.item('password',0); //找到登录密码的输入框 } {
o.value := 'TEST';
o :=WebBrowser1.oleobject.document.all.item('submit',0); //或者用指定表单名称提交o.Click; //点击操作,对其它对象也可同样操作