gpt4 book ai didi

delphi - OnDocumentComplete 不会在组件上触发

转载 作者:行者123 更新时间:2023-12-02 04:42:49 25 4
gpt4 key购买 nike

如果我在表单 WebBrowser1: TMyWebBrowser; 上使用以下组件,并且将 OnDocumentComplete = WebBrowser1DocumentComplete 放在表单上,​​则 TMyWebBrowser.DocumentComplete<即使 WebBrowser1DocumentComplete 过程为空, 也不会触发。

触发 TMyWebBrowser.DocumentComplete 的唯一方法是使用 OnDocumentComplete = nil

谁能解释一下为什么?谢谢山姆

type
TMyWebBrowser = class(TWebBrowser)
private
{ Private declarations }
protected
{ Protected declarations }
procedure DocumentComplete(ASender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
public
{ Public declarations }
Completed: Boolean;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure NavigateUntilCompleted(URL: String);
published
{ Published declarations }
End;

procedure Register;

implementation


procedure Register;
begin
RegisterComponents('Sam', [TMyWebBrowser]);
end;

constructor TMyWebBrowser.Create(AOwner: TComponent);
begin
inherited;
onDocumentComplete := DocumentComplete;
end;

procedure TMyWebBrowser.DocumentComplete(ASender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
begin
// check that the event is raised for the top-level browser (not frames or iframes)
if pDisp = TMyWebBrowser(ASender).ControlInterface then
begin
Completed := TRUE;
end;
inherited;
end;

最佳答案

您不能为一个事件分配多个方法,但您可以尝试分配 2 个方法并覆盖第一个方法。

类定义

constructor TMyWebBrowser.Create(AOwner: TComponent);
begin
inherited;
// set OnDocumentComplete event
onDocumentComplete := DocumentComplete;
end;

并在您的代码中

var
LMyWebBrowser : TMyWebBrowser;
begin
LMyWebBrowser := TMyWebBrowser.Create( nil );
// now overwriting the value set by constructor
LMyWebBrowser.onDocumentComplete := WebBrowser1Complete;
end;

您可以自行检查

var
LMyWebBrowser : TMyWebBrowser;
begin
LMyWebBrowser := TMyWebBrowser.Create( nil );
// check
if Assigned( LMyWebBrowser.OnDocumentComplete ) then
ShowMessage( 'OnDocumentComplete is already set!' );
end;

关于delphi - OnDocumentComplete 不会在组件上触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17444117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com