如何使组件编辑器在双击时执行原始处理程序?

时间:2015-02-15 03:34:40

标签: delphi custom-controls delphi-xe7

我为自定义控件构建了自定义组件编辑器(TComponentEditor)。我正在实现右键单击上下文菜单,这意味着默认情况下,在设计时双击控件会执行第一个上下文菜单项。但是,双击后,我不希望发生这种情况。我想创建一个OnClick事件处理程序的原始行为。

当覆盖此组件编辑器的Edit过程时,我已尝试使用和不使用inherited。如果我包含它,双击将执行上下文菜单中的第一项。如果我不包含inherited,则双击控件时根本不会发生任何事情。在添加此组件编辑器之前,双击将自动生成OnClick事件处理程序,而不执行任何我自己的事件。

{ TFontButtonEditor }

type
  TFontButtonEditor = class(TComponentEditor)
  private
    FForm: TfrmFontButtonEditor;
    FBtn: TFontButton;
    procedure ExecEditor;
  protected
    procedure ExecuteVerb(Index: Integer); override;
    function GetVerb(Index: Integer): String; override;
    function GetVerbCount: Integer; override;
  public
    constructor Create(AComponent: TComponent; ADesigner: IDesigner); override;
    destructor Destroy; override;
    procedure Edit; override;
  end;

constructor TFontButtonEditor.Create(AComponent: TComponent;
  ADesigner: IDesigner);
begin
  inherited;
  FBtn:= TFontButton(AComponent); //Reference instance of control
  FForm:= TfrmFontButtonEditor.Create(nil); //Create editor form instance
end;

destructor TFontButtonEditor.Destroy;
begin
  FForm.Free;
  inherited;
end;

procedure TFontButtonEditor.Edit;
begin
  //Executed on double-click, but I don't want to...
  //inherited;
end;

procedure TFontButtonEditor.ExecEditor;
begin
  FForm.FFont.Assign(FBtn.Image.Font);
  FForm.ImageChar:= FBtn.Image.Text;
  //More assignments
  case FForm.ShowModal of
    mrOK: begin
      FBtn.Image.Font.Assign(FForm.FFont);
      FBtn.Image.Text:= FForm.ImageChar;
      //More assignments
    end;
    else begin
      //Cancelled
    end;
  end;
end;

procedure TFontButtonEditor.ExecuteVerb(Index: Integer);
begin
  case Index of
    0: begin
      ExecEditor;
      //Also executed on double-click, don't want it to...
    end;
    1: begin
      MessageDlg('Font Button Control - XXXXX', mtInformation, [mbOK], 0);
    end;
  end;
end;

function TFontButtonEditor.GetVerb(Index: Integer): String;
begin
  case Index of
    0: Result:= '&Edit Font Button';
    1: Result:= '&About Font Button';
  end;
end;

function TFontButtonEditor.GetVerbCount: Integer;
begin
  Result:= 2;
end;

如何使用组件编辑器双击此自定义控件来执行旧式OnClick事件处理程序而不是尝试执行组件编辑器?

1 个答案:

答案 0 :(得分:4)

TDefaultEditor而不是TComponentEditor下降。

(TDefaultEditor通过调用EditEditOnCreate属性的OnChange方法或第一个方法,将OnClick方法替换为您的意愿它找到的事件属性。该属性的Edit方法在TMethodProperty.SetValue中创建带有Designer.CreateMethod的事件处理程序以及有关继承表单,现有名称等的几个检查...)