Receive iwebbrowser2 automation events

Source: Internet
Author: User
Tags ole
OLE Automation controllers sometimes receive event notifications from the prosecution. For example, webbrowser's onnavagitecomplete and ondocumentcomplete events. The OLE object must implement the iconnectionpointcontainer interface. Iconnectionpointcontainer and several other interfaces related to automation events are defined as follows:

Ienumconnections = interface
['{B196B287-BAB4-101A-B69C-00AA00341D07}']
Function next (CELT: longint; out ELT;
Pceltfetched: plongint): hresult; stdcall;
Function SKIP (CELT: longint): hresult; stdcall;
Function Reset: hresult; stdcall;
Function clone (Out Enum: ienumconnections): hresult; stdcall;
End;

{Iconnectionpoint interface}

{$ externalsym iconnectionpoint}
iconnectionpoint = interface
['{B196B286-BAB4-101A-B69C-00AA00341D07}']
function getconnectioninterface (Out IID: tiid): hresult; stdcall;
function getconnectionpointcontainer (Out CPC: iconnectionpointcontainer): hresult;
stdcall;
function advise (const unksink: iunknown; out dwcookie: longint): hresult; stdcall;
function unadvise (dwcookie: longint): hresult; stdcall;
function enumconnections (Out Enum: ienumconnections): hresult; stdcall;
end;

{Iconnectionpointcontainer interface}
{$ Externalsym iconnectionpointcontainer}
Iconnectionpointcontainer = interface
['{B196B284-BAB4-101A-B69C-00AA00341D07}']
Function enumconnectionpoints (Out Enum: ienumconnectionpoints): hresult;
Stdcall;
Function findconnectionpoint (const IID: tiid;
Out CP: iconnectionpoint): hresult; stdcall;
End;

{Ienumconnectionpoints interface}

{$ Externalsym ienumconnectionpoints}
Ienumconnectionpoints = interface
['{B196B285-BAB4-101A-B69C-00AA00341D07}']
Function next (CELT: longint; out ELT;
Pceltfetched: plongint): hresult; stdcall;
Function SKIP (CELT: longint): hresult; stdcall;
Function Reset: hresult; stdcall;
Function clone (Out Enum: ienumconnectionpoints): hresult;
Stdcall;
End;

You can use interfaceconnect to register an automatic event notification:

Procedure interfaceconnect (const Source: iunknown; const IID: tguid;
Const sink: iunknown; var connection: longint );
VaR
CPC: iconnectionpointcontainer;
CP: iconnectionpoint;
Begin
Connection: = 0;
If source. QueryInterface (iconnectionpointcontainer, CPC) = s_ OK then
If CPC. findconnectionpoint (IID, CP) = s_ OK then
CP. Advise (sink, connection );
End;

Here, source is the interface of the OLE object under prosecution. IID is the guid of the event connection point. An OLE object may have multiple event connection points. For example, iwebbrowser2 has a time connection point:

Webbrowsereventid: tguid = '{34a715a0-6587-11d0-924a-0020afc7ac4d }';

Sink is the interface of the object that receives Event Notifications. This object must at least implement the iunknown and idispatch interfaces. When an event is triggered, the invoke method of the event receiver is called:

Function invoke (dispid: integer; const IID: tguid; localeid: integer; flags: word; var Params;
Varresult, cmdinfo, argerr: pointer): hresult; virtual; stdcall;

Dispid is the event ID. You can find this ID from the event interface declaration of the automation object. For example, iwebbrowser event Declaration

Dwebbrowserevents2 = dispinterface
['{34a715a0-6587-11d0-924a-0020afc7ac4d}']
Procedure statustextchange (const text: widestring); dispid 102;
Procedure progresschange (Progress: integer; progressmax: integer); dispid 108;
Procedure commandstatechange (command: integer; Enable: wordbool); dispid 105;
Procedure downloadbegin; dispid 106;
Procedure downloadcomplete; dispid 104;
Procedure titlechange (const text: widestring); dispid 113;
Procedure propertychange (const szproperty: widestring); dispid 112;
Procedure beforenavigate2 (const Pdisp: idispatch; var URL: olevariant; var flags: olevariant;
VaR targetframename: olevariant; var postdata: olevariant;
VaR headers: olevariant; var cancel: wordbool); dispid 250;
Procedure newwindow2 (VAR ppdisp: idispatch; var cancel: wordbool); dispid 251;
Procedure navigatecomplete2 (const Pdisp: idispatch; var URL: olevariant); dispid 252;
Procedure documentcomplete (const Pdisp: idispatch; var URL: olevariant); dispid 259;
Procedure onquit; dispid 253;
Procedure onvisible (visible: wordbool); dispid 254;
Procedure ontoolbar (toolbar: wordbool); dispid 255;
Procedure onmenubar (menubar: wordbool); dispid 256;
Procedure onstatusbar (statusbar: wordbool); dispid 257;
Procedure onfullscreen (fullscreen: wordbool); dispid 258;
Procedure ontheatermode (theatermode: wordbool); dispid 260;
End;

The dispid number after the method declaration is dispid. Therefore, if you receive a notification that the dispid of iwebbrowser2 is 259, it indicates that the documentcomplete event has occurred.

The Params Variable Parameter of invoke contains additional information about a specific event, or is called an event parameter. It is a tdispparams structure:

Tagdispparams = record
Rgvarg: pvariantarglist; // array of the variant type.
Rgdispidnamedargs: pdispidlist;
Cargs: longint; // number of parameters
Cnamedargs: longint;
End;

For the documentcomplelte event of iwebbrowser2, rgvarg [1] is the iwebbrowser2 interface that sends the event notification. Note that the documentcomplelte event is also triggered when an embedded framework is downloaded, to determine whether the entire web page has been downloaded, use the following method (teventdispatch is a Class I wrote to receive automatic Event Notifications ):

Function teventdispatch. Invoke (dispid: integer; const IID: tguid;
Localeid: integer; flags: word; var Params;
Varresult, raise info, argerr: pointer): hresult;
VaR
Vpdispparams: pdispparams;
IB: iwebbrowser2;
Begin
Vpdispparams: = pdispparams (@ Params );
If dispid = 259 then
Begin
If (idispatch (olevariant (vpdispparams. rgvarg [1]). QueryInterface (iid_iwebbrowser2, IB) = 0) and (IB. Container = nil) then
Begin
// The entire web page is downloaded
End
Else
Begin
// The download of an embedded framework webpage is completed, but the entire webpage is not completed
End;
End;
Result: = s_ OK;
End;

I will not repeat other iwebbrowser events. For details, refer to the twebbrowser'sCode.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.