delphi 哪个是TFDQuery的TableentEditor?

e4yzc0pl  于 2023-10-18  发布在  其他
关注(0)|答案(1)|浏览(106)

我需要在自定义TFDQuery的上下文菜单上添加一个新选项,所以我遵循了这个指南,它工作得很好。How to add context-menu actions in Delphi Form Designer for a custom component?
问题是它替换了TFDQuery的当前TComponentEditor,因此我无法再访问其字段编辑器、查询编辑器...
我想我需要的是从FireDAC基TComponentEditor派生我的TComponentEditor,但我还没有能够识别它。你能告诉我为TFDQuery声明常规TComponentEditor的单元和类吗?.

**更新:**我已经将此代码添加到我的自定义查询中,以获取TFDQuery的当前默认编辑器,它在FireDAC. dcl. Reg单元中显示为TFDQueryEditor。但是我的系统没有任何FireDAC.Dcl.Reg.pas,也找不到任何对FireDAC源代码的TFDQueryEditor的引用。

constructor TKpQuery.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

  ShowMessage(  (GetComponentEditor(Self, NIL) as TObject).ClassName  );
  ShowMessage(  (GetComponentEditor(Self, NIL) as TObject).UnitName  );
end;

这是我的自定义ComponentEditor:

interface

uses System.Classes, DesignIntf, DesignEditors, FireDAC.Comp.Client;

type
{$REGION 'TKpQuery Editor Declaration'}
  TKpQueryEditor=class(TComponentEditor)
    function GetVerbCount: Integer; override;
    function GetVerb(Index: Integer): string; override;
    procedure ExecuteVerb(Index: Integer); override;
  end;
{$ENDREGION}

{$REGION 'TKpQuery Declaration'}
  ...
  ...
{$ENDREGION}

procedure Register;

implementation

uses
  Data.DB,
  System.Types,
  System.SysUtils,
  System.StrUtils,
  System.RegularExpressions;

procedure Register;
begin
  {$IFDEF WIN32}
  RegisterComponents('My Custom Data Access', [TKpQuery]);
  RegisterComponentEditor(TKpQuery, TKpQueryEditor);
  {$ENDIF}
end;

{$REGION 'TKpQuery Editor Implementation'}
procedure TKpQueryEditor.ExecuteVerb(Index: Integer);
begin
  inherited;
  case Index of
    0: (Component as TKpQuery).SetDisplayLabels;  
  end;
end;

function TKpQueryEditor.GetVerb(Index: Integer): string;
begin
  case Index of
    0: Result := 'Set DisplayLabels';  
  end;
end;

function TKpQueryEditor.GetVerbCount: Integer;
begin
  Result := 1;   
end;
{$ENDREGION}
hsvhsicv

hsvhsicv1#

由于Uwe Raabe已经确认派生FDQuery组件编辑器所需的单元不可用,因此我复制了Blurry Sterk建议的解决方案。
这似乎工作得很好:

unit MyCustom.Query.Editor;

interface

uses
  Windows, SysUtils, Classes, DesignIntf, DesignEditors;

  type
  TgkQueryEditor = class(TComponentEditor)
  private
    FOldEditor: IComponentEditor;
  protected
  public
    constructor Create(AComponent: TComponent; ADesigner: IDesigner); override;
    procedure ExecuteVerb(Index: Integer); override;
    function GetVerb(Index: Integer): string; override;
    function GetVerbCount: Integer; override;
    procedure Edit; override;
    procedure Copy; override;
  end;

procedure Register;

implementation

uses
  MyCustom.Query;

var
  PrevEditorClass: TComponentEditorClass = nil;

constructor TgkQueryEditor.Create(AComponent: TComponent; ADesigner: IDesigner);
begin
  inherited Create(AComponent, ADesigner);

  if Assigned(PrevEditorClass) then
    FOldEditor := TComponentEditor(PrevEditorClass.Create(AComponent, ADesigner));
end;

procedure TgkQueryEditor.ExecuteVerb(Index: Integer);
var
  i: Integer;
begin
  i := Index - FOldEditor.GetVerbCount;
  case i of
    0: (Component as TKpQuery).SetDisplayLabels;  // This is my new custom action
  else
    if Assigned(FOldEditor) then
      FOldEditor.ExecuteVerb(Index)
  end;
end;

function TgkQueryEditor.GetVerb(Index: Integer): string;
var
  i: Integer;
begin
  i := Index - FOldEditor.GetVerbCount;
  case i of
    0: Result := 'Set Display Label';
  else
    if Assigned(FOldEditor) then
      Result := FOldEditor.GetVerb(Index)
  end;
end;

function TgkQueryEditor.GetVerbCount: Integer;
begin
  Result := 1;
  if Assigned(FOldEditor) then
    Inc(Result, FOldEditor.GetVerbCount);
end;

procedure TgkQueryEditor.Edit;
begin
  if Assigned(FOldEditor) then
    FOldEditor.Edit;
end;

procedure TgkQueryEditor.Copy;
begin
  if Assigned(FOldEditor) then
    FOldEditor.Copy;
end;

procedure Register;
var
  Query: TKpQuery;
  Editor: IComponentEditor;
begin
  Query := TKpQuery.Create(nil);
  try
    Editor := GetComponentEditor(Query, nil);
    if Assigned(Editor) then
      PrevEditorClass := TComponentEditorClass((Editor as TObject).ClassType);
  finally
    Editor := nil;
    FreeAndNIL(Query);
  end;
  RegisterComponentEditor(TKpQuery, TgkQueryEditor);
end;

end.

相关问题