2

如果我在表单上使用以下组件WebBrowser1: TMyWebBrowser;,并且如果我放在表单上OnDocumentComplete = WebBrowser1DocumentCompleteTMyWebBrowser.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;
4

1 回答 1

0

您不能为一个事件分配多个方法,但您尝试分配 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;
于 2013-07-03T11:21:06.990 回答