创建现有ComponentEditor(TFDQuery)

时间:2015-11-05 15:01:35

标签: delphi components firedac

我尝试在Delphi V10中编写一个新的ComponentEditor,当你在formdesigner上右键单击组件时,应该为组件TFDQuery添加一个新的menuitem。

Shows you the original ComponentEditor of TFDQuery

我的代码存在严重问题。有时会出现正确的菜单。我的新menuitem工作正常,但旧组件编辑器的后代四个菜单项有时不会触发异常。重新启动IDE后,我的组件不再注册。

使用谷歌我没有找到我的问题的指定信息

愿有人能给我一个提示,我做错了什么。

最诚挚的问候 格里

按照我的代码

UNIT GK_Query_Reg;

INTERFACE

USES
  Windows, Messages, SysUtils, Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, dbReg, DsDesign,
  ToolsAPI, DesignIntf, DesignEditors, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Param, FireDAC.Stan.Error,
  FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf, FireDAC.Stan.Async, FireDAC.DApt, FireDAC.Comp.DataSet, FireDAC.Comp.Client;

TYPE
  TgkQueryEditor = CLASS(TComponentEditor)
  PRIVATE
    FOldEditor: IComponentEditor;
  PROTECTED
  PUBLIC
    CONSTRUCTOR Create(AComponent: TComponent; ADesigner: IDesigner); OVERRIDE;
    DESTRUCTOR Destroy; OVERRIDE;
    PROCEDURE ExecuteVerb(Index: Integer); OVERRIDE;
    FUNCTION GetVerb(Index: Integer): STRING; OVERRIDE;
    FUNCTION GetVerbCount: Integer; OVERRIDE;
    PROCEDURE Edit; OVERRIDE;
    PROCEDURE Copy; OVERRIDE;
    PROCEDURE ShowEditor;
END;

PROCEDURE Register;

IMPLEMENTATION

VAR
  ComponentEditor: IComponentEditor;

CONSTRUCTOR TgkQueryEditor.Create(AComponent: TComponent; ADesigner: IDesigner);
BEGIN
  INHERITED Create(AComponent, ADesigner);
  FOldEditor := ComponentEditor;
  OutputDebugString(Pchar(Format('Create Component %d',[FOldEditor.GetVerbCount])));
END;

DESTRUCTOR TgkQueryEditor.Destroy;
BEGIN
  INHERITED;
  OutputDebugString(Pchar('Destroy Component'));
END;

PROCEDURE TgkQueryEditor.ExecuteVerb(Index: Integer);
VAR
  I: Integer;
BEGIN
  I := INDEX - FOldEditor.GetVerbCount;
  OutputDebugString(Pchar(Format('ExecuteVerb: %d, %d' ,[i,index])));
  CASE I OF
    0:
      ShowEditor;
    ELSE
      FOldEditor.ExecuteVerb(INDEX)
  END;
END;

FUNCTION TgkQueryEditor.GetVerb(Index: Integer): STRING;
VAR
  I: Integer;
BEGIN
  I := INDEX - FOldEditor.GetVerbCount;
  OutputDebugString(Pchar(Format('GetVerb: %d, %d' ,[i,index])));
  CASE I OF
    0:
      Result := '&Show Form...';
    ELSE
      Result := FOldEditor.GetVerb(INDEX)
  END;
END;

FUNCTION TgkQueryEditor.GetVerbCount: Integer;
BEGIN
  OutputDebugString(Pchar(Format('GetVerbCount old: %d' ,[FOldEditor.GetVerbCount])));
  Result := FOldEditor.GetVerbCount + 1;
  OutputDebugString(Pchar(Format('GetVerbCount: %d' ,[result])));
END;

PROCEDURE TgkQueryEditor.Edit;
BEGIN
  OutputDebugString(Pchar('Edit Component'));
  FOldEditor.Edit;
END;

PROCEDURE TgkQueryEditor.Copy;
BEGIN
  OutputDebugString(PChar('Copy Component'));
  FOldEditor.Copy;
END;

PROCEDURE TgkQueryEditor.ShowEditor;
VAR
  AQuery: TFDQuery;
BEGIN
  AQuery := TFDQuery(Component);
  IF (AQuery.Connection = NIL) THEN BEGIN
    ShowMessage('Assign Database first.');
    exit;
  END;
  IF (AQuery <> NIL) THEN BEGIN
    ShowMessage('OK for Open Query-Builder!');
  END;
END;

PROCEDURE Register;
VAR
  FDQ: TFDQuery;
BEGIN
  FDQ := TFDQuery.Create(NIL);
  TRY
    ComponentEditor := GetComponentEditor(FDQ, NIL);
    OutputDebugString(pChar(Format('Register Component %s, %d',[ComponentEditor.GetComponent.ClassName,ComponentEditor.GetVerbCount])));
  FINALLY
    FreeAndNIL(FDQ);
  END;
  RegisterComponentEditor(TFDQuery, TgkQueryEditor);
END;

END.

1 个答案:

答案 0 :(得分:1)

这里的解决方案

UNIT GK_Query_Reg;

INTERFACE

  USES
    Windows, Messages, SysUtils, Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms,
    Vcl.Dialogs, Vcl.Menus, dbReg, DsDesign,
    ToolsAPI, DesignIntf, DesignEditors, FireDAC.Stan.Intf, FireDAC.Stan.Option,
    FireDAC.Stan.Param, FireDAC.Stan.Error,
    FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf, FireDAC.Stan.Async,
    FireDAC.DApt, FireDAC.Comp.DataSet, FireDAC.Comp.Client;

  TYPE
    TgkQueryEditor = CLASS(TComponentEditor)
    PRIVATE
      FOldEditor: TComponentEditor;
    PROTECTED
    PUBLIC
      CONSTRUCTOR Create(AComponent: TComponent; ADesigner: IDesigner); OVERRIDE;
      DESTRUCTOR Destroy; OVERRIDE;
      PROCEDURE ExecuteVerb(Index: Integer); OVERRIDE;
      FUNCTION GetVerb(Index: Integer): STRING; OVERRIDE;
      FUNCTION GetVerbCount: Integer; OVERRIDE;
      PROCEDURE Edit; OVERRIDE;
      PROCEDURE Copy; OVERRIDE;
      PROCEDURE ShowEditor;
    END;

  PROCEDURE Register;

IMPLEMENTATION

  VAR
    PrevEditorClass: TComponentEditorClass = NIL;

  CONSTRUCTOR TgkQueryEditor.Create(AComponent: TComponent; ADesigner: IDesigner);
  BEGIN
    INHERITED Create(AComponent, ADesigner);
    IF Assigned(PrevEditorClass) THEN BEGIN
      FOldEditor := TComponentEditor(PrevEditorClass.Create(AComponent, ADesigner));
    END;
  END;

  DESTRUCTOR TgkQueryEditor.Destroy;
  BEGIN
    INHERITED;
  END;

  PROCEDURE TgkQueryEditor.ExecuteVerb(Index: Integer);
  BEGIN
    IF (INDEX = 0) THEN BEGIN
      ShowEditor
    END ELSE BEGIN
      IF Assigned(FOldEditor) THEN BEGIN
        FOldEditor.ExecuteVerb(INDEX - 1);
      END;
    END;
  END;

  FUNCTION TgkQueryEditor.GetVerb(Index: Integer): STRING;
  BEGIN
    IF (INDEX = 0) THEN BEGIN
      Result := '&Show Form...'
    END ELSE BEGIN
      IF Assigned(FOldEditor) THEN BEGIN
        Result := FOldEditor.GetVerb(INDEX - 1);
      END;
    END;
  END;

  FUNCTION TgkQueryEditor.GetVerbCount: Integer;
  BEGIN
    Result := 1;
    IF Assigned(FOldEditor) THEN BEGIN
      Inc(Result, FOldEditor.GetVerbCount);
    END;
  END;

  PROCEDURE TgkQueryEditor.Edit;
  BEGIN
//    IF Assigned(FOldEditor) THEN BEGIN
//      FOldEditor.Edit;
//    END;
    ShowEditor;
  END;

  PROCEDURE TgkQueryEditor.Copy;
  BEGIN
    IF Assigned(FOldEditor) THEN BEGIN
      FOldEditor.Copy;
    END;
  END;

  PROCEDURE TgkQueryEditor.ShowEditor;
  VAR
    AQuery: TFDQuery;
  BEGIN
    AQuery := Component AS TFDQuery;
    IF Assigned(AQuery) THEN BEGIN
      IF NOT Assigned(AQuery.Connection) THEN BEGIN
        ShowMessage('Assign Database first.');
      END ELSE BEGIN
        ShowMessage('OK for open a form to change SQL-Code!');
        Designer.Modified;
      END;
    END ELSE BEGIN
      ShowMessage('Component not assigned!')
    END;
  END;



  PROCEDURE Register;
  VAR
    FDQ: TFDQuery;
    Editor: IComponentEditor;
  BEGIN
    FDQ := TFDQuery.Create(NIL);
    TRY
      Editor := GetComponentEditor(FDQ, NIL);
      IF Assigned(Editor) THEN BEGIN
        PrevEditorClass := TComponentEditorClass((Editor AS TObject).ClassType);
      END;
    FINALLY
      Editor := NIL;
      FDQ.Free;
    END;
    RegisterComponentEditor(TFDQuery, TgkQueryEditor);
  END;

END.