德尔福通用框架

标签 delphi frames

我仍然带着一个关于 Delphi 框架的问题来到这里。 我想创建一个应用程序,它使用各种类型的框架来管理不同的数据库表,因此为了了解如何完成此类任务,我创建了一个简单的 Delphi 表单:

unit main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.StdCtrls, Vcl.ExtCtrls, FramesManagement;

type
  TfrmMain = class(TForm)
    pnlCommands: TPanel;
    pnlFrames: TPanel;
    btnFrame1: TButton;
    btnFrame2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure btnFrame1Click(Sender: TObject);
    procedure btnFrame2Click(Sender: TObject);
  private
    FFrame: IFrameManagement;
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}

uses Frame1, Frame2;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  FFrame := TFramemanagement.Create;
end;

procedure TfrmMain.btnFrame1Click(Sender: TObject);
begin
  FFrame.CreateGenericFrame(pnlFrames, TFrame(Frame1.TFra1));
end;

procedure TfrmMain.btnFrame2Click(Sender: TObject);
begin
  FFrame.CreateGenericFrame(pnlFrames, TFrame(Frame2.TFra2));
end;

end.

此表单使用如下声明的接口(interface):

unit FramesManagement;

interface

uses
  Vcl.Forms, Vcl.StdCtrls, Vcl.ExtCtrls, Frame1, Frame2;

type

  IFrameManagement = interface
  ['{A00E0D1B-3438-4DC4-9794-702E8302B567}']
    procedure CreateGenericFrame(ParentPanel: TPanel; FrameName: TFrame);
  end;

  TFrameManagement = class(TInterfacedObject, IFrameManagement)
  private
    genericFrame: TFrame;
    procedure CreateGenericFrame(ParentPanel: TPanel; FrameName: TFrame);
  end;

implementation

procedure TFrameManagement.CreateGenericFrame(ParentPanel: TPanel;
  FrameName: TFrame);
begin
  genericFrame := FrameName.Create(ParentPanel);
  genericFrame.Parent := ParentPanel;
end;

这里有两个框架。

第 1 帧:

unit Frame1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.StdCtrls;

type
  TFra1 = class(TFrame)
    txtFrame1: TStaticText;
    txtFrameType: TStaticText;
    lblFrameType: TLabel;
  private

  public

  end;

implementation

{$R *.dfm}

end.

和第 2 帧:

unit Frame2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.StdCtrls;

type
  TFra2 = class(TFrame)
    txtFrame2: TStaticText;
    txtFrameType: TStaticText;
    lblFrameType: TLabel;
  private

  public

  end;

implementation

{$R *.dfm}

end.

这就是所有代码,但是当我运行应用程序并尝试创建第一帧或第二帧时,我收到如下错误:

enter image description here

我认为解决方案可能是使用泛型,但我不知道如何使用它们。我的想法是对的还是有另一种方法可以达到这个目标? 谁能帮帮我?

最佳答案

procedure TFrameManagement.CreateGenericFrame(ParentPanel: TPanel; FrameName: TFrame);
begin
  genericFrame := FrameName.Create(ParentPanel);
  genericFrame.Parent := ParentPanel;
end;

这里 FrameName 是一个实例,您正在调用该实例的构造函数。您没有按照您的意愿创建新实例。

你需要使用元类。

type
  TFrameClass = class of TFrame;

procedure TFrameManagement.CreateGenericFrame(ParentPanel: TPanel; FrameClass: TFrameClass);
begin
  genericFrame := FrameClass.Create(ParentPanel);
  genericFrame.Parent := ParentPanel;
end;

你可以这样调用它:

FFrame.CreateGenericFrame(pnlFrames, Frame2.TFra2);

关于德尔福通用框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30893517/

相关文章:

html - 如何跨帧显示叠加层?

html - 来自 FRAMESET 中链接的 HTTP_REFERER 在不同的计算机上发送不同的值?

java - java swing中如何获取内框?

delphi - 使桌面窗口在窗口周围绘制框架无效

delphi - 规范枚举器MoveNext函数中是否需要if语句?

python - 从屏幕流式传输帧,生成视频

python - 使用opencv和python从视频中提取帧

windows - 在运行时确定弹出提示消息 (THintInfo::HintStr) 的大小

delphi - 从命令行构建时如何修改/替换选项集文件?

Delphi:如何动态分配事件处理程序而不覆盖现有的事件处理程序?