Jer*_*dge 10 ide delphi propertyeditor custom-component delphi-xe2
我已经按照一些关于创建自定义属性编辑器对话框的教程,但涉及的内容很多,我无法正常工作.我想要完成的是一个自定义表单,带有日期选择器(日历),时间选择器,以及确定和取消按钮.表单完全没问题,但是我如何实现这一点,以便我可以使用按钮发布属性编辑器的某个类型的任何组件中的属性?
我想完全覆盖TDateTime类型并将我的自定义编辑器放在其位置,因此无论在何处TDateTime发布并在Object Inspector中可见,我都可以使用此编辑器在同一窗口中同时修改日期和时间.
问题是关于创建自定义属性编辑器的文档很差,尽管有些资源非常透彻,但它们会详细介绍这些功能,并且在最常见的场景中缺乏重点.
Jer*_*dge 14
我不想在这里提出这个问题并希望有人为我解答,所以我自己做了研究来解决我的问题,我想分享这个迷你项目所涉及的独特经验,因为我相信其他人都是沮丧的同样的事情.
自定义属性编辑器,对话框和组件编辑器有许多不同的可能性.这尤其需要一个TDateTimeProperty后代.这将允许您在保持DateTime格式的同时,能够以纯文本(String)的形式直接在Object Inspector中编辑属性的值.
我假设您已经具备了创建自定义组件的一般知识以及可以在其中发布此属性编辑器的包,因为这是我自己的课程,我将不会介绍.这需要将一行代码放在Register过程中,但我们稍后会讨论.
首先,您需要在Design-Time包中创建一个新表单,您的组件已在其中注册.命名单位DateTimeProperty.pas,并命名表格DateTimeDialog(从而制作表格的类TDateTimeDialog).无论控制需要,在这种情况下,地方TMonthCalendar,TDateTimePicker(带Kind集到dtkTime),和2个TBitBtn控件,一个标OK有ModalResult中mrOK和其他标记Cancel用ModalResult的mrCancel.
你的单位应该是这样的:
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.
Run Code Online (Sandbox Code Playgroud)
以下是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
Run Code Online (Sandbox Code Playgroud)
现在,添加DesignEditors和DesignIntf你的uses条款.确保您已DesignIDE在Requires此Design-Time包中声明.这是发布任何属性编辑器所必需的.
在表单中,使用属性getter和setter 创建一个名为DateTimetype 的新公共属性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;
Run Code Online (Sandbox Code Playgroud)
接下来我们需要添加实际的属性编辑器类.创建这个类正下方的{$R *.dfm}是刚下implementation:
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;
Run Code Online (Sandbox Code Playgroud)
最后,我们需要添加一个Register过程来执行这个新属性编辑器的实际注册:
procedure Register;
begin
RegisterPropertyEditor(TypeInfo(TDateTime), nil, '', TDateTimeEditor);
end;
Run Code Online (Sandbox Code Playgroud)
现在有一个重要的部分需要理解RegisterPropertyEditor.由于第2个和第3个参数是nil空字符串,这意味着编辑器将应用于所有实例TDateTime.查看此过程以获取有关使其特定于某些组件和属性实例的更多信息.
这是安装后的最终结果......

为自定义属性编辑器提供的一些好资源如下: