I'm following the How to call Delphi code from scripts running in a TWebBrowser DelphiDabbler tutorial (by Peter Johnson) to allow Delphi to listen to TWebBrowser
JavaScript events.
This works up to the point where I see my Delphi procedures getting called. However, from in there I need to update some form labels, and I see no way to access my form from those procedures.
The DelphiDabbler example code nicely circumvents 'direct form access' by creating THintAction.Create(nil);
which will do it's thing:
This let's us decouple our external object implementation quite nicely from the program's form
But I want to access my form! Data to be passed are integers and strings.
I could use PostMessage() and WM_COPYDATA messages, but these would still need the form handle. And isn't there a 'direct' route to the form?
Relevant code:
type
TWebBrowserExternal = class(TAutoIntfObject, IWebBrowserExternal, IDispatch)
protected
procedure SetVanLabel(const ACaption: WideString); safecall; // My 3 procedures that are called...
procedure SetNaarLabel(const AValue: WideString); safecall; // ... declared in the type library.
procedure SetDistanceLabel(AValue: Integer); safecall;
public
constructor Create;
destructor Destroy; override;
end;
type
TExternalContainer = class(TNulWBContainer, IDocHostUIHandler, IOleClientSite)
private
fExternalObj: IDispatch; // external object implementation
protected
{ Re-implemented IDocHostUIHandler method }
function GetExternal(out ppDispatch: IDispatch): HResult; stdcall;
public
constructor Create(const HostedBrowser: TWebBrowser);
end;
constructor TExternalContainer.Create(const HostedBrowser: TWebBrowser);
begin
inherited Create(HostedBrowser);
fExternalObj := TWebBrowserExternal.Create;
end;
The form has a property FContainer: TExternalContainer;
, in the FormCreate I do fContainer := TExternalContainer.Create(WebBrowser);
(parameter is the design time TWebBrowser
), so the
TExternalContainer.fExternalObj
is assigned to that.
Question:
procedure TWebBrowserExternal.SetDistanceLabel(AValue: Integer);
begin
// **From here, how do I send AValue to a label caption on my form?**
end;
I must confess that interfaces are not my forte ;-)
[Added:] Note: My forms are all created dynamically, there is no TForm instance in the current unit.