【问题标题】:How to improve the use of Delphi Frames如何提高 Delphi Frames 的使用率
【发布时间】:2011-02-13 16:46:05
【问题描述】:

多年来我一直在 Delphi 中使用框架,它们是 VCL 最强大的功能之一,但标准使用它们似乎存在一些风险,例如:

  1. 很容易意外移动或编辑框架主窗体上的框架子组件,而不会意识到您正在对框架进行“调整” - 我知道这不会影响原始框架代码,但通常不会你想要什么。

  2. 在使用框架时,您仍会看到其子组件以进行可视化编辑,即使该框架已使用多年且不应触摸。

所以我开始思考......

  1. 有没有办法对组件进行“分组”以使它们的位置被“锁定”?这对于完成的表格和框架都很有用。通常,其他开发人员向我返回的代码只是表单边界发生了变化,甚至他们并不打算进行任何更改。

  2. 有没有办法将框架及其组件转换为单个 Delphi 组件?如果是这样,框架内部将完全隐藏,其可用性将进一步提高。

我对任何想法都感兴趣...

布赖恩。

【问题讨论】:

  • 2021 注意:框架不能很好地与 VCL 样式(也称为皮肤)搭配使用。在某些情况下皮肤没有正确应用,整个 GUI 看起来像是由脑损伤的人构建的。

标签: delphi components tframe


【解决方案1】:

将你的框架注册为一个组件解决了 1. 和 2.:

  1. 当您将框架控件放在窗体或其他框架上时,框架上的组件会被锁定
  2. 您将获得一个可以直观设计的组件(实际上是:控件)

但是:有几个问题(可以解决,见文章链接),其中最重要的是这个:

当您将组件放在框架上,然后将该框架作为组件放在 Delphi 表单或框架上时,这些组件在结构窗格中可见。

问题是因为它们在结构窗格中可见,您可以删除它们,导致访问冲突。

解决这个问题的诀窍是not forget the 'sprig'。
在DelphiLive 2009 期间,我从Ray Konopka 学到了宝贵的一课。

由于这节课很有价值,我在上面写了一个blog post,详细描述了它。

最重要的部分是这段代码(更多细节在博文中):

procedure RegisterFramesAsComponents(const Page: string; const FrameClasses: array of TFrameClass);
var
  FrameClass: TFrameClass;
begin
  for FrameClass in FrameClasses do
  begin
    RegisterComponents(Page, [FrameClass]);
    RegisterSprigType(FrameClass, TComponentSprig);
  end;
end;

希望这会有所帮助。

--杰罗恩

【讨论】:

  • @Jeroen:非常狡猾,干得好。去看看你的博文。布赖恩。
  • @Brian:如果它适合你,那么请重新考虑接受的答案。
  • 嗨。你能澄清一下“TComponentSprig”是谁吗?
  • @ExternalServerError 如果博客文章不够清楚,也许 Ray Konopka 的解释更清楚:codeverge.com/public.delphi.vcl.writing/…
  • @ExternalServerError,没错:这就是 Ray 解释 sprig,而不是 spring 的原因。 sprig 是组件和 IDE 设计者之间的一种分支管道。几乎没有关于它的任何文档,但是如果您查看 Delphi 7 VCL 层次结构海报,您可以找到一些使用的名称,并从那里谷歌了解它们的上下文。 scribd.com/document/17544777/Delphi-7-VCLHierarchyPoster
【解决方案2】:

是的,只需将它们注册为组件。 :-)

正常设计您的框架,然后注册它。还要确保不要对不同的单元有不必要的依赖,因为在使用“组件”时这些是链接的。您也可以添加published 属性,以便稍后在对象检查器中使用它们。例如,请参阅 IDE 生成的以下代码(另请参阅我的 cmets):

unit myUnit;

uses
 ...

type
  TmyComp = class(TFrame) //set your frame name to be the name your component 
    ToolBar1: TToolBar; //different components added in the form designer
    aliMain: TActionList;
    ...
  published //this section is added by hand
    property DataSource: TDataSource read FDataSource write SetDataSource; //some published properties added just for exemplification
    property DefFields: string read FDefFields write SetDefFields;
    ...
  end;


procedure Register; //added by hand

implementation

{$R *.DFM}

procedure Register;
begin
  RegisterComponents('MyFrames', [TmyComp]); //register the frame in the desired component category
end;

在您选择的包中编译上述内容,安装它并检查您的组件调色板。 :-)

HTH

【讨论】:

  • @Plainth:非常棒的指针,谢谢。我也注意到下面受访者对“小枝”的回答。布赖恩。
  • @john 我在 XE2 上完全做到了这一点,当放置在 TForm 上时,它可以从组件托盘中进行选择。但是我不能为 myFrame 创建一个独立的单元。我已经尝试通过创建一个 TFrame 然后将 TFrame 的引用更改为 TMyFrame 来做到这一点。它没有用,我新发布的属性没有出现。可能缺少什么?
【解决方案3】:

只是为了增加贡献,请注意,如果您转到 Structure 窗口并右键单击您选择的 TFrame 名称,然后单击 Add to Palete 菜单选项。 这将从您的框架中创建一个组件,您不需要创建任何Register 过程。 ;-)

【讨论】:

    【解决方案4】:

    我几乎总是在代码中创建框架实例。到目前为止,这对我来说很简单并且效果很好。

    【讨论】:

      【解决方案5】:

      我在尝试使用框架作为组件时也遇到了这个问题。解决显而易见的问题有多种可能性,但它们都破坏了信息隐藏的原则(所有框架的子组件都作为已发布的属性公开,这意味着每个人都可以访问它们)。

      我通过实现一个通用的“框架控制”组件解决了这个问题:

      unit RttiBrow.Cbde.FrameControl;
      
      interface
      
      uses
        Classes, Controls, Forms, Messages, ExtCtrls;
      
      type
        TFrameClass = class of TFrame;
      
        TComponentFrame = class (TFrame)
        private
          function GetClientHeight: Integer;
          function GetClientWidth: Integer;
          procedure SetClientHeight(const Value: Integer);
          procedure SetClientWidth(const Value: Integer);
          function GetOldCreateOrder: Boolean;
          procedure SetOldCreateOrder(const Value: Boolean);
          function GetPixelsPerInch: Integer;
          procedure SetPixelsPerInch(const Value: Integer);
          function GetTextHeight: Integer;
          procedure SetTextHeight(const Value: Integer);
        published
          { workarounds for IDE bug }
          property ClientWidth: Integer read GetClientWidth write SetClientWidth stored False;
          property ClientHeight: Integer read GetClientHeight write SetClientHeight stored False;
          property OldCreateOrder: Boolean read GetOldCreateOrder write SetOldCreateOrder stored False;
          property PixelsPerInch: Integer read GetPixelsPerInch write SetPixelsPerInch stored False;
          property TextHeight: Integer read GetTextHeight write SetTextHeight stored False;
        end;
      
        TComponentFrame<TFrameControl: class { TControl }> = class (TComponentFrame)
        private
          function GetController: TFrameControl; inline;
        protected
          property Controller: TFrameControl read GetController;
        public
          constructor Create (AOwner: TComponent); override;
        end;
      
        TFrameControl<T: TFrame> = class (TWinControl)
        private
          FFrame: T;
          function PlainFrame: TFrame;
        protected
          procedure CreateParams (var Params: TCreateParams); override;
          property Frame: T read FFrame;
        public
          constructor Create (AOwner: TComponent); override;
          property DockManager;
        published
          property Align;
          property Anchors;
          property BiDiMode;
          property Color;
          property Constraints;
          property Ctl3D;
          property UseDockManager default True;
          property DockSite;
          property DoubleBuffered;
          property DragCursor;
          property DragKind;
          property DragMode;
          property Enabled;
          property Font;
          property ParentBiDiMode;
          property ParentBackground;
          property ParentColor;
          property ParentCtl3D;
          property ParentDoubleBuffered;
          property ParentFont;
          property ParentShowHint;
          property ShowHint;
          property TabOrder;
          property TabStop;
          property Touch;
          property Visible;
          property OnAlignInsertBefore;
          property OnAlignPosition;
          property OnCanResize;
          property OnConstrainedResize;
          property OnDockDrop;
          property OnDockOver;
          property OnDragDrop;
          property OnDragOver;
          property OnEndDock;
          property OnEndDrag;
          property OnEnter;
          property OnExit;
          property OnGesture;
          property OnGetSiteInfo;
          property OnMouseActivate;
          property OnMouseDown;
          property OnMouseEnter;
          property OnMouseLeave;
          property OnMouseMove;
          property OnMouseUp;
          property OnResize;
          property OnStartDock;
          property OnStartDrag;
          property OnUnDock;
        end;
      
      
      implementation
      
      uses
        Windows;
      
      { TFrameControl<T> }
      
      constructor TFrameControl<T>.Create(AOwner: TComponent);
      begin
        inherited;
        FFrame := T (TFrameClass (T).Create (Self));
        PlainFrame.Parent := Self;
        PlainFrame.Align := alClient;
      end;
      
      procedure TFrameControl<T>.CreateParams(var Params: TCreateParams);
      begin
        inherited;
        Params.Style := Params.Style or WS_CLIPCHILDREN;
        Params.ExStyle := Params.ExStyle or WS_EX_CONTROLPARENT;
      end;
      
      function TFrameControl<T>.PlainFrame: TFrame;
      begin
        Result := FFrame; // buggy compiler workaround
      end;
      
      
      { TComponentFrame }
      
      function TComponentFrame.GetOldCreateOrder: Boolean;
      begin
        Result := False;
      end;
      
      function TComponentFrame.GetPixelsPerInch: Integer;
      begin
        Result := 0;
      end;
      
      function TComponentFrame.GetTextHeight: Integer;
      begin
        Result := 0;
      end;
      
      procedure TComponentFrame.SetClientHeight(const Value: Integer);
      begin
        Height := Value;
      end;
      
      procedure TComponentFrame.SetClientWidth(const Value: Integer);
      begin
        Width := Value;
      end;
      
      procedure TComponentFrame.SetOldCreateOrder(const Value: Boolean);
      begin
      end;
      
      procedure TComponentFrame.SetPixelsPerInch(const Value: Integer);
      begin
      end;
      
      procedure TComponentFrame.SetTextHeight(const Value: Integer);
      begin
      end;
      
      function TComponentFrame.GetClientHeight: Integer;
      begin
        Result := Height;
      end;
      
      function TComponentFrame.GetClientWidth: Integer;
      begin
        Result := Width;
      end;
      
      
      { TComponentFrame<TFrameControl> }
      
      constructor TComponentFrame<TFrameControl>.Create(AOwner: TComponent);
      begin
        inherited;
        Assert (AOwner <> nil);
        Assert (AOwner.InheritsFrom (TFrameControl));
      end;
      
      function TComponentFrame<TFrameControl>.GetController: TFrameControl;
      begin
        Result := TFrameControl (Owner);
      end;
      
      
      end.
      

      有了这个类,添加一个框架作为一个组件就变成了一个两阶段的过程:

        // frame unit
      type
        TFilteredList = class;
      
        TFrmFilteredList = class (TComponentFrame<TFilteredList>)
          // lots of published sub-components and event methods like this one:
          procedure BtnFooClick(Sender: TObject);
        end;
      
        TFilteredList = class (TFrameControl<TFrmFilteredList>)
        private
          procedure Foo;
        public
          // the component's public interface
        published
          // the component's published properties
        end;
      
      procedure Register;
      ...
      procedure Register;
      begin
        RegisterComponents ('CBDE Components', [TFilteredList]);
      end;
      
      procedure TFrmFilteredList.BtnFooClick(Sender: TObject);
      begin
        Controller.Foo;
      end;
      
      procedure TFilteredList.Foo;
      begin
      end;
      ...
      

      使用这种方法时,组件的用户不会看到您的子组件。

      【讨论】:

        猜你喜欢
        • 2012-04-29
        • 1970-01-01
        • 1970-01-01
        • 2019-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-19
        相关资源
        最近更新 更多