unit MainFrm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, SHDocVw, ComCtrls, StdCtrls, ExtCtrls, MSHTML;
type
TDemoForm =
class(TForm)
Panel: TPanel;
UrlEdit: TEdit;
LoadButton: TButton;
StatusBar: TStatusBar;
WebBrowser: TWebBrowser;
procedure LoadButtonClick(Sender: TObject);
procedure WebBrowserDocumentComplete(Sender: TObject;
const pDisp: IDispatch;
var URL: OleVariant);
end;
var
DemoForm: TDemoForm;
implementation
{$R *.dfm}
function GetFrameDoc(doc: IHTMLDocument2; v: OleVariant): IHTMLDocument2;
var
win: IHTMLWindow2;
u: IUnknown;
begin
u := doc.frames.item(v);
if Assigned(u)
and Succeeded(u.QueryInterface(IHTMLWindow2, win))
then Result := win.Document
else Result :=
nil;
end;
// preconditions:
// - wb.Document fully loaded
// returns first vElement in first vForm in any frame
function GetInputElement(wb: IWebBrowser2; vForm, vElement: Variant): IHTMLInputElement;
var
iFrame: Integer;
doc, frameDoc: IHTMLDocument2;
fe: IHTMLFormElement;
begin
doc := wb.Document
as IHTMLDocument2;
if doc.frames.length = 0
then
begin
fe := doc.forms.item(vForm, 0)
as IHTMLFormElement;
if Assigned(fe)
then Result := fe.item(vElement, 0)
as IHTMLInputElement
else Result :=
nil;
end else
for iFrame := 0
to Pred(doc.frames.length)
do
begin
frameDoc := GetFrameDoc(doc, iFrame);
fe := doc.forms.item(vForm, 0)
as IHTMLFormElement;
if Assigned(fe)
then Result := fe.item(vElement, 0)
as IHTMLInputElement
else Result :=
nil;
if Assigned(Result)
then Exit;
end;
end;
procedure TDemoForm.LoadButtonClick(Sender: TObject);
begin
with WebBrowser
do
Navigate(UrlEdit.Text);
// [url]http://www.google.de[/url]
end;
procedure TDemoForm.WebBrowserDocumentComplete(Sender: TObject;
const pDisp: IDispatch;
var URL: OleVariant);
var
ie: IHTMLInputElement;
begin
with Sender
as TWebBrowser
do
if pDisp = DefaultInterface
then
begin
ie := GetInputElement(DefaultInterface, '
f', '
q');
if Assigned(ie)
then ie.value := '
hasta la vista'
else ShowMessage('
bad luck');
end;
end;
end.