Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Trapping events from IHTMLElement in a Delphi program

363 views
Skip to first unread message

Wadood Chaudhary

unread,
Mar 4, 2002, 10:21:48 PM3/4/02
to

I'm having a hard time figuring out how to trap onClick events from IHTMLElements. A sample source will be much appreciated. I have tried TDHTML component and also EventSink from Bly's site. I had no success with TDHTMLComponent.
To me it should have been as simple as:
Document...Items(..) AS IHTMLElement.OnClick := MyOnClick.

Martin Kammann

unread,
Mar 4, 2002, 11:49:07 PM3/4/02
to

Not quite, but...

Do this:
Re-import the mshtml type library via Project/Import Type Library... (Make sure
to tick the "Generate Component Wrapper" checkbox at the bottom).

Then use this code (I called the imported mshtml Type Library mshtml60):

{$WARN SYMBOL_PLATFORM OFF}

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
StdCtrls, Dialogs, mshtml, OleCtrls, SHDocVw_TLB;

type
TForm1 = class(TForm)
WebBrowser1: TWebBrowser;
procedure FormCreate(Sender: TObject);
procedure WebBrowser1DownloadComplete(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
FGroupsAnchor: THTMLAnchorElement;
procedure GroupsAnchorClick(Sender: TObject);
public
end;

var
Form1: TForm1;

implementation

uses Mshtml60;

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
FGroupsAnchor := THTMLAnchorElement.Create(nil);
FGroupsAnchor.Ononclick := GroupsAnchorClick;
WebBrowser1.Navigate('http://www.google.com');
end;

procedure TForm1.WebBrowser1DownloadComplete(Sender: TObject);
var
Doc: IHTMLDocument2;
Disp: IDispatch;
Anchor: DispHTMLAnchorElement;
begin
if Supports(WebBrowser1.Document, IHTMLDocument2, Doc)
then begin
Disp := Doc.all.item('2a', 0); // 2a = id of the group link
if Supports(Disp, DispHTMLAnchorElement, Anchor)
then FGroupsAnchor.ConnectTo(Anchor);
end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
FGroupsAnchor.Free;
end;

procedure TForm1.GroupsAnchorClick(Sender: TObject);
begin
ShowMessage('Clicked on Groups!');
end;

end.


--
Martin

Nusrat Jahan

unread,
Mar 5, 2002, 1:44:47 AM3/5/02
to
Thanks Martin for your reply. I'm still without the faintest idea of how to
go about this messy business.
I've Delphi 4.0. When I try to generate Type library, I get no option for
Component wrapper as you had advised. The generated mshtml does not have a
component called THTMLAnchorElement - or any TObjects except TScriplet.
Using Binh Ly (http://www.techvanguards.com), I've generated two other files
frontPage_editorEvent and mshml_Events, which have lot of Componentss with
enticing names. TFrontPageEditorAnchorEvents, TFrontPageEditorImageEvents,
TFrontPageEditorElementEvents, and so on - but I can not get it to connect
to a source or assign it to my event handlers.
Here is the code of what I'm trying to do.

var
fpe: TFrontPageEditorHTMLElementEvents;


Elements := fp.ActivePageWindow.Document.all.tags('Select') AS
IHTMLElementCollection;
Fe:= TFrontPageEditorHTMLElementEvents.Create(Self);
fe.Connect(fp.Get_ActiveDocument.Get_activeElement);//gives an error that
unable to connect - OLE errror blah blah blah
//fe.onclick:=ButtonInsertClick (gives an error of type mismatch);

Wadood

"Martin Kammann" <mNOka...@intellienceSPAM.comPLEASE> wrote in message
news:3c844e39_2@dnews...

Martin Kammann

unread,
Mar 5, 2002, 3:16:17 AM3/5/02
to
> Thanks Martin for your reply. I'm still without the faintest idea of how to
> go about this messy business.
> I've Delphi 4.0. When I try to generate Type library, I get no option for
> Component wrapper as you had advised. The generated mshtml does not have a
> component called THTMLAnchorElement - or any TObjects except TScriplet.

I believe that feature is only available as with Delphi 5 and later.

> Using Binh Ly (http://www.techvanguards.com), I've generated two other files
> frontPage_editorEvent and mshml_Events, which have lot of Componentss with
> enticing names.

If you are talking about the EventSinkImp utility: that should do the job as
well. The steps to hook into the events are essentially the same as with the
D5&D6 version. It should do the job.

> TFrontPageEditorAnchorEvents, TFrontPageEditorImageEvents,
> TFrontPageEditorElementEvents, and so on - but I can not get it to connect
> to a source or assign it to my event handlers.

Sorry, I don't know about Frontpage automation. I was under the impression that
you were automating Internet Explorer or TWebBrowser.

--
Martin


Wadood Chaudhary

unread,
Mar 5, 2002, 11:27:59 PM3/5/02
to
Can you help me in figuring out what does HTMLElement.OnClick expects? How
does one assign one's own OnClick events to IHTMLElement.OnClick. If you
have used EventSinkImpl by Binn Ly then can you tell me what does connect in
THTMLElementEvents expects?

Thanks very much.

Wadood


"Martin Kammann" <mNOka...@intellienceSPAM.comPLEASE> wrote in message

news:3c847ec7_1@dnews...

Martin Kammann

unread,
Mar 5, 2002, 11:52:06 PM3/5/02
to
> Can you help me in figuring out what does HTMLElement.OnClick expects? How
> does one assign one's own OnClick events to IHTMLElement.OnClick. If you
> have used EventSinkImpl by Binn Ly then can you tell me what does connect in
> THTMLElementEvents expects?

declaration of the connect method:
procedure Connect (const ASource: IUnknown);

ASource will be the HTML Element for which you want to trap the event.
You will need to locate the relevant HTML Element within the Document as
described in my reply to your initial post.

--
Martin


Wadood Chaudhary

unread,
Mar 6, 2002, 1:16:19 AM3/6/02
to
This is what I'm trying to do. I creat a new IHTMLLelement of Table or
Labels etc., add them where cursor is in the HTML file and then I want to be
notified when they are clicked. In the following code, I create a new
element and inserts it iin teh ActivElement. That part works fine, then
using EventIMPLSink generated file I try to connect the two objects.

var

HTMLLabel: FPHTMLLabelElement; //from frontPage_TLB (almost identical to
mshtml_TLB except for FP Prefix)

LabelEvents: TFrontPageEditorHTMLLabelEvents;// from EventSinkIMpl

begin

HTMlLabel:= fp.ActiveDocument.createElement('Label') AS FPHtmllabelElement;
HtmlLabel.title:='This is frustrating';
HtmlLabel.innerText:='This is really frustrating';
fp.Get_ActiveDocument.Get_activeElement.insertAdjacentHTML('BeforeEnd',HtmlL
abel.InnerHTML);
LabelEvents:= TFrontPageEditorHTMLLabelEvents.Create(Self);
LabelEvents.Connect(HtmlLabel);//I get a runtime error.
LabelEvents.onClick:=ButtonInsertClick;// This is what I want to do but I
get a syntax error since OnClick is defined as: Onclick Event is defined as:
//
function (Sender: TObject): WordBool of object;

Wadood

"Martin Kammann" <mNOka...@intellienceSPAM.comPLEASE> wrote in message

news:3c85a06a_2@dnews...

0 new messages