Title: Loop through frames of a TWebBrowser
Question: I needed a function to loop through a frameset and get all the framedocs as an IHTMLDocument2 object. It took me 2 days
to figure out how to do it, searching through tons of information.
I finally figured it out and thought about giving it to the public, so
that it can be usefull to others facing the same problem.
Answer:
------------------------------------------------------------------
!ATTENTION!
Please take a look at the updated article which can be found here:
http://www.delphi3000.com/articles/article_5070.asp
------------------------------------------------------------------
Hi and thanx to all that helped me. I finally made a function that
loops through all frames and gets all the documents for all frames.
That cost me 2 days of my life ;-) For all of you that are curious
here is my function. Maybe it can be useful for others that have the
same problem.
!Note!: there is a demo-project available for download at
http://www.spiele-datenbank.de/loopframes.zip (4.712 bytes)
-----------------------------8{*******************************************************}
{ }
{ Demo Unit for Looping through documents }
{ of a frameset of TWebBrowser_V1 }
{ }
{ Created: 20.03.2001, Juergen Sommer }
{ Author : Juergen Sommer }
{ Email : nostromo1111@gmx.de }
{ }
{*******************************************************}
unit frmForm1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls,SHDocVw_TLB, mshtml_tlb, OleCtrls,
ComCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
Button1: TButton;
Button2: TButton;
Memo: TMemo;
Browser: TWebBrowser_V1;
edt_url: TEdit;
StatusBar1: TStatusBar;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure BrowserFrameNavigateComplete(Sender: TObject;
const URL: WideString);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
close;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if edt_url.text='' then exit;
Browser.navigate(edt_url.text);
end;
procedure TForm1.BrowserFrameNavigateComplete(Sender: TObject;
const URL: WideString);
var
Document : IHTMLDocument2;
FrameDoc : IHTMLDocument2;
Frames : IHTMLFramesCollection2;
All : IHtmlElementCollection;
HtmlElement : IHtmlElement;
idisp : IDispatch;
i,j : Integer;
v : OleVariant;
win2 : IHTMLWindow2;
str_ready :string;
begin
Memo.Lines.Clear;
Browser.Document.QueryInterface(iHTMLDocument2,Document);
Frames := Document.get_frames;
All := Document.All;
for i:=0 to All.length-1 do
begin
HTMLElement := All.Item(i,0) as IHTMLElement;
if (Assigned(HTMLElement)) then
begin
if (HTMLElement.TagName='FRAME') then
begin
v := HTMLElement.getAttribute('Name',0);
Memo.lines.add('============================ FRAME FOUND ==');
Memo.lines.add(' ==FRAME NAME: '+v);
idisp := Frames.item(v);
// now we get the window-object
idisp.QueryInterface(IHTMLWindow2,win2);
if (Assigned(win2)) then
begin
// here it comes, the IHTMLDocument2
FrameDoc := win2.document;
if (Assigned(FrameDoc)) then
begin
// we have to wait for the document until it is loaded ...
repeat
application.processmessages;
str_ready:=FrameDoc.readyState;
StatusBar1.Panels[0].Text:=str_ready;
until (str_ready = 'complete') or (str_ready ='interactive');
// ...now it is safe to go on with work...
for j:=0 to FrameDoc.all.length-1 do
begin
// showing the doc.elements for demo purposes
HTMLElement := FrameDoc.All.Item(j,0) as IHTMLElement;
Memo.lines.add('Element : ['+HTMLElement.TagName+']');
end;
end;
end;
end;
end;
end;
end;
end.
-----------------------------8