如何为特定类型的所有实例实现自定义属性编辑器?

10

我已经按照一些创建自定义属性编辑器对话框的教程进行了尝试,但涉及到的内容太多了,我无法使其正常工作。我想要实现的是一个具有日期选择器(日历)、时间选择器、确定和取消按钮的自定义表单。表单本身不是问题,但我该如何实现这样一个功能,以便在任何特定类型的组件中发布一个属性,并附带一个启动属性编辑器的按钮?

我想完全覆盖TDateTime类型,并放置我的自定义编辑器,因此无论在哪里发布并在对象检查器中可见TDateTime,我都可以使用此编辑器在同一窗口中修改日期和时间。

问题在于创建自定义属性编辑器的文档很差,虽然有些资源非常详细,但它们过于关注能力,缺乏针对最常见场景的重点。

1个回答

15

我不想在这里提出问题并期望有人为我解答,所以我自己进行了研究以解决我的问题,并且我想分享这个小项目中涉及的独特经验,因为我确信其他人也对同样的事情感到沮丧。

使用自定义属性编辑器、对话框和组件编辑器有许多不同的可能性。这里特别需要一个TDateTimeProperty的派生类。这将允许您在对象查看器中直接以纯文本(字符串)形式编辑属性值,同时保持日期时间格式。

我假设您已经有了创建自定义组件和包的一般知识,可以从包中发布此属性编辑器,因为这是一堂课程,我不会涵盖它。这只需在Register过程中放置一行代码,但我们稍后再说。

首先,在您注册组件的Design-Time包中创建一个新表单。将单位命名为DateTimeProperty.pas,并将表单命名为DateTimeDialog(从而使表单的类为TDateTimeDialog)。放置您需要的任何控件,在这种情况下为TMonthCalendarTDateTimePicker(其Kind设置为dtkTime)和2个TBitBtn控件,其中一个标记为OK,其ModalResultmrOK,另一个标记为Cancel,其ModalResultmrCancel

您的单位应该看起来像这样:

unit DateTimeProperty;

interface

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

type
  TDateTimeDialog = class(TForm)
    dtDate: TMonthCalendar;
    dtTime: TDateTimePicker;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
  private

  public

  end;         

var
  DateTimeDialog: TDateTimeDialog;

implementation

{$R *.dfm}

end.

下面是此表单背后的DFM代码:

object DateTimeDialog: TDateTimeDialog
  Left = 591
  Top = 158
  BorderIcons = [biSystemMenu]
  BorderStyle = bsToolWindow
  Caption = 'Pick Date/Time'
  ClientHeight = 231
  ClientWidth = 241
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poScreenCenter
  DesignSize = (
    241
    231)
  PixelsPerInch = 96
  TextHeight = 13
  object dtDate: TMonthCalendar
    Left = 8
    Top = 31
    Width = 225
    Height = 166
    Anchors = [akLeft, akRight, akBottom]
    Date = 41261.901190613430000000
    TabOrder = 1
  end
  object dtTime: TDateTimePicker
    Left = 8
    Top = 8
    Width = 113
    Height = 21
    Date = 41261.000000000000000000
    Time = 41261.000000000000000000
    Kind = dtkTime
    TabOrder = 2
  end
  object BitBtn1: TBitBtn
    Left = 158
    Top = 200
    Width = 75
    Height = 25
    Caption = 'OK'
    Default = True
    ModalResult = 1
    TabOrder = 0
  end
  object BitBtn2: TBitBtn
    Left = 77
    Top = 200
    Width = 75
    Height = 25
    Caption = 'Cancel'
    ModalResult = 2
    TabOrder = 3
  end
end
现在,在您的uses从句中添加DesignEditorsDesignIntf。确保您在此设计时包的Requires中声明了DesignIDE。这是发布任何属性编辑器所必需的。
在表单中,创建一个名为DateTime的新公共属性,类型为TDateTime,具有属性获取器和设置器。此属性将允许您轻松读取/写入选择实际表示的完整TDateTime值。因此,您应该在表单中拥有以下内容:
private
  function GetDateTime: TDateTime;
  procedure SetDateTime(const Value: TDateTime);
public
  property DateTime: TDateTime read GetDateTime write SetDateTime;

....

function TDateTimeDialog.GetDateTime: TDateTime;
begin
  Result:= Int(dtDate.Date) + Frac(dtTime.Time);
end;

procedure TDateTimeDialog.SetDateTime(const Value: TDateTime);
begin
  dtDate.Date:= Value;
  dtTime.DateTime:= Value;
end;

接下来我们需要添加实际的属性编辑器类。在implementation下面的{$R *.dfm}之后创建此类:

type
  TDateTimeEditor = class(TDateTimeProperty)
  public
    procedure Edit; override;
    function GetAttributes: TPropertyAttributes; override;
    function GetValue: String; override;
    procedure SetValue(const Value: String); override;
  end;

procedure TDateTimeEditor.Edit;
var
  F: TDateTimeDialog;
begin
  //Initialize the property editor window
  F:= TDateTimeDialog.Create(Application);
  try
    F.DateTime:= GetFloatValue;
    if F.ShowModal = mrOK then begin
      SetFloatValue(F.DateTime);
    end;
  finally
    F.Free;
  end;
end;

function TDateTimeEditor.GetAttributes: TPropertyAttributes;
begin
  //Makes the small button show to the right of the property
  Result := inherited GetAttributes + [paDialog];
end;

function TDateTimeEditor.GetValue: String;
begin
  //Returns the string which should show in Object Inspector
  Result:= FormatDateTime('m/d/yy h:nn:ss ampm', GetFloatValue);
end;

procedure TDateTimeEditor.SetValue(const Value: String);
begin
  //Assigns the string typed in Object Inspector to the property
  inherited;
end;

最后,我们需要添加一个Register过程来执行这个新属性编辑器的实际注册:

procedure Register;
begin
  RegisterPropertyEditor(TypeInfo(TDateTime), nil, '', TDateTimeEditor);
end;

现在有一个重要的部分需要理解,在调用RegisterPropertyEditor时。由于第二个和第三个参数为nil和空字符串,这意味着编辑器将应用于所有TDateTime实例。请查看此过程以获取有关如何使其特定于某些组件和属性实例的更多信息。

安装后的最终结果如下所示...

最终属性编辑器的示例

以下是一些有用的自定义属性编辑器资源:

  1. 如何制作自定义组件属性?
  2. http://delphi.about.com/library/bluc/text/uc092501d.htm
  3. http://www.sandownet.com/propedit.html

更多好的资源可以是像CnWizards或JediVCL这样的FLOSS库,它们实现并注册了一些全局属性编辑器,只需阅读和学习即可。 - Arioch 'The
不要忘记 Ray Konopka 的 Developing Custom Delphi 3 Components,虽然已经停止印刷,但仍然适用。Ray 在他的网站上有一个 PDF 版本出售:http://www.raize.com/DevTools/Ordering/Pricing.asp - Marjan Venema
+1 做得不错。不过我会调整一下时间和日期控件的顺序... ;-) - NGLN
1
+1 我不明白为什么你要重写 GetValueSetValue。特别是 GetValue 强制使用你喜欢的日期格式,但 SetValue 却没有这样做。我建议直接删除这两个方法。 - David Heffernan
@DavidHeffernan 是的,我同意,我想指出它可以被覆盖进行验证等操作,尽管我没有解释那部分。 - Jerry Dodge
顺便提一下,仅供参考,理论上这应该适用于所有 Delphi 版本2+,对吗?还是3+?但无论如何,我记得听说 D1 与任何后来的版本都有完全不同的属性编辑器。 - Jerry Dodge

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接