user1446583
user1446583

Reputation: 33

How to receive events from IE using Delphi?

I need to evaluate the document from each browser window and act accordingly. I'm using shellwindows to obtain the IwebBrowser2. Then I have access to all the document properties I need. ie...

ShellWindows := TshellWindows.Create(nil);

...

ShellWindowDisp := ShellWindows.Item(Count); //for loop

...

ShellWindowDisp.QueryInterface(iWebBrowser2, WebBrowser);

etc....

This method works fine as far as I can tell. However, if one of the documents change my code will never know it. So I need to monitor Explorer for events such as OnDocumentComplete. Dumping the code above into a timer and comparing properties obviously is not the way to go. I've found several components that capture events but would rather not rely on a third party component for this. Since my knowledge on this area is limited I need to understand what is going on. Any good articles out there that explain how to approach this, preferably with simple source code?

Upvotes: 3

Views: 1317

Answers (1)

RRUZ
RRUZ

Reputation: 136391

In order to access the events of the WebBrowser instances, you must get a pointer to the IConnectionPointContainer interface using the QueryInterface method of the IWebBrowser2 interface, then call the FindConnectionPoint method passing the GUID of the DWebBrowserEvents2 interface and finally call Advise method of the IConnectionPoint interface to start to receive the events.

Something like so

var
 LConnectionPointContainer : IConnectionPointContainer;
 LConnectionPoint : IConnectionPoint;
 dwCookie: Longint;
begin
  //LWebBrowser2 is a IWebBrowser2 object
  LWebBrowser2.QueryInterface(IConnectionPointContainer, LConnectionPointContainer);
  LConnectionPointContainer.FindConnectionPoint(DWebBrowserEvents2, LConnectionPoint);
  LConnectionPoint.Advise(Self, dwCookie);
end;

All this code must be implemented inside of a class which descends from IDispatch in order to receive the events in the Invoke method. As in your case you need intercept the DocumentComplete event you must check when the value of the DispID parameter is 259.

If you want a sample of this explanation, try checking this code intercept Internet Explorer messages.

Upvotes: 4

Related Questions