原文连接
http://www./keylife/iblog_show.asp?xid=36435
作者 : loadymf
在Twebbrowser中
WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
Sender为消息发送者,在这里为webbrowser1这个变量,
而pdisp则为消息发送的frame指针,
Twebbrowser中有:
ControlInterface: IWebBrowser2 read GetControlInterface;
IWebBrowser2 = interface(IWebBrowserApp)
也就是说sender为控件Twebbrowser的实例指针
而pdisp为IWebBrowser2实例指针。
//下面为一获取系统中IE实例的源码
var
ShellWindow: IShellWindows;
Web: IWebBrowser2;
Dispatch: IDispatch;
i,j:integer;
IEAddress:string;
HTMLDocument:IHTMLDocument2;
ElementCollection:IHTMLElementCollection;
FrameWindow:IHTMLWindow2;
Vi,Vj:OLEVariant;
HTMLFrameBase :IHTMLFrameBase ;
HTMLFrameElement:IHTMLFrameElement ;
HTMLIFrameElement:IHTMLIFrameElement;
begin
ShellWindow := CoShellWindows.Create;
/*/每个运行的浏览器(IE 和 资源浏览器)都会在 ShellWindows 中进行登记,因此我们要通过 IShellWindows 取得实例(示例程序中使用的就是这个方法)。*///
for i:=0 to ShellWindow.Count -1 do
begin
Vi:=i;
Dispatch:=ShellWindow.Item(Vi);
if Dispatch=nil then continue;
Dispatch.QueryInterface(IWebBrowser2,Web);//接口取出放于web
if Web<>nil then//是否存在
begin
IEAddress:=Web.LocationURL;
if Pos(aURL,IEAddress)>0 then
begin
Web.Document.QueryInterface(IHTMLDocument2,HTMLDocument);//取文档接口
if HTMLDocument<>nil then
begin
if HTMLDocument.frames.length =0 then//无框架
begin
ElementCollection:=HTMLDocument.Get_All;
DoWithHtmlElement(ElementCollection);
end
else//有框架
begin
for j:=0 to HTMLDocument.frames.length -1 do
begin
Vj:=j;
Dispatch:=HTMLDocument.frames.item(Vj);框架遍历
// if Succeeded(Dispatch.QueryInterface(IHTMLFrameBase,HTMLFrameBase)
if Succeeded(Dispatch.QueryInterface(IHTMLWindow2,FrameWindow)) then
begin
// DoWithHtmlElement(FrameWindow.document.all);
end;
End;
end;
end;
end;
End;
end;
end;
procedure TMainFrm.btnTestClick(Sender: TObject);
begin
FillIEForm(edUrl.Text);
end;
end.
本文来自Delphi之窗,原文地址:http://www.
总结:IE填表的流程:
IWebBrowser2窗口==>IHTMLDocument2文档==>ElementCollection对象
IWebBrowser2窗口==>IHTMLWindow2框架==>ElementCollection对象