保证有20分:怎样让新增加的属性显示到左边的 Object Inspector里? (0分)

  • 主题发起人 主题发起人 ylung
  • 开始时间 开始时间
Y

ylung

Unregistered / Unconfirmed
GUEST, unregistred user!
自编的控件,从ComboBox继承,给它添加了一个属性。
在代码中能正常访问和使用,但是在设计时可以显式地访问修改吗?
也就是怎样让新增加的属性显示到左边的 Object Inspector里?

我在下面的贴中放多了100分,不好意思,请答对的富翁到这里领取:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2290587
 
★★★如何自定义属性编辑器

一、派生出一个属性编辑器对象:
Delphi的DsgnIntf.pas(D5)单元中声明了几个属性编辑器,它们都是从一个共同的基类
TPropertyEditor继承下来的。当创建一个属性编辑器时,属性编辑器必须继承于TPropertyEditor
或它的派生类。
至于究竟需要选择哪个编辑器,这取决于需要编辑哪些属性。如需要编辑一个整型的属性,
就可用TIntegerProperty作为属性编辑器。如要在编辑属性的同时加入其他功能,则需要以
TIntegerProperty为祖先类,派生出一个新的属性编辑器。

二、覆盖方法:
当派生一个属性编辑器对象时,必须覆盖GetValue()和SetValue()这两个方法。GetValue()是
获得属性值以供在Object Inspector中显示,这个值以字符串的形式返回; SetValue()的作用是
根据输入到Object Inspect上的字符串来设置属性的值;
如果需要获取属性的枚举值供编辑时选择,还需要覆盖GetValues()方法;
如果需要用对话框来编辑属性,需要覆盖GetAttributes()方法,以便让Object Inspector知道
这个属性可以用一个对话框来编辑。GetAttributes()方法的返值用于设定属性编辑器的特性。
paValueList 可以从下拉列表中选择属性值,适用于枚举型的属性诸如TForm.BorderStyle和
整型变量如TColor、TCharSet;
paSubProperties 包含子属性,子属性右缩进显示。适用于集合类型的属性,如TOpenDialog.Option和TForm.Font;
paDialog 在Object Inspector上将出现一个省略号按钮,单击这个按钮,将调用属性编辑器的
Edit()方法打开一个对话框用于TForm.Font这样的属性;
paMultiSelect 允许为多个组件选择属性值,有的属性没有这个功能,如Name属性;
paAutoUpdate 当属性的值有变化,就调用SetValue()把值写到.DFM文件中,适用用于TForm.Caption等属性;
paFullWidthName 告诉Object Inspector这值不需要交付,名称应以Object Inspector的全宽交付;
paSortList 按字母顺序排列属性值的列表;
paReadOnly 属性是只读的;
paRevertable 属性的值可以恢复为以前的值。TFont等嵌套的属性不应恢复;
如果GetAttributes()方法的返值包括paDialog,则还要覆盖Edit()方法以调对话框;

三、注册新的属性编辑器:
可以调用RegisterPropertyEditor()来注册一个属性编辑器,这个过程是这样声明的:
procedure RegisterPropertyEditor(PropertyType: PTypeInfo; ComponentClass: TClass;
const PropertyName: string; EditorClass: TPropertyEditorClass);
第一个参数叫PropertyType,它是一个指针,指向要编辑的属性的运行期类型信息。
ComponentClass参数用于指定这个属性编辑器所适用的组件类。
PropertyName参数用于指定属性的名称。
EditorClass参数用于指定属性编辑器的类型。

具体实例可以参考C:/Program Files/Borland/Delphi5/Source/Property Editors下的单元。
 
不会吧?这种问题也问?
在Published或PUBLIC里加属性就OK了?

published
property ImageIndex: TImageIndex index 0 read FImageIndex write SetImage;//这一行写完后按一下Shift+Ctrl+C就可以了

 
To wonken
不好意思,我是刚入门。其实我也是像你说的那样干的。但是新开一个程序,从面板上增加该控件到窗体上,左边的 Object Inspector 中没有我新增加的那个属性,代码中访问是正常的。

To andy263
呵呵,这么长啊,我正在看。
 
不可能。啊
type
{ TPnlItems }
TGMPnlItem = class(TSpeedButton)
private
FPaintNot: Boolean;
FHasSub: Boolean;
FImageIndex: TImageIndex;
FSubImages: TImageList;
FOnMouseEnter,FOnMouseLeave: TNotifyEvent;
FIsDefault: Boolean;
FSubMenu: TPopupMenu;
FCaptionAlign: TAlignment;
FBeginAGrp: Boolean;
MouseIn:Boolean ;
FSubPop: Boolean;
procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
procedure SetImage(const Index: Integer; const Value: TImageIndex);
procedure SetSubImages(const Value: TImageList);
procedure SetPaintNot(const Value: Boolean);
procedure SetSubMenu(const Value: TPopupMenu);
procedure SetBeginAGrp(const Value: Boolean);
procedure SetHasSub(const Value: Boolean);
procedure SetIsDefault(const Value: Boolean);
procedure SetCaptionAlign(const Value: TAlignment);
function PWindow(Cont:TControl):TForm ;
protected
procedure Paint; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Click; override;
published
property ImageIndex: TImageIndex index 0 read FImageIndex write SetImage;
property PaintNot: Boolean read FPaintNot write SetPaintNot default True;
property OnMouseEnter: TNotifyEvent read FOnMouseEnter write FOnMouseEnter;
property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;
property HasSub:Boolean read FHasSub write SetHasSub default False ;
property SubMenu:TPopupMenu read FSubMenu write SetSubMenu ;
property SubImages:TImageList read FSubImages write SetSubImages;
property IsDefault:Boolean read FIsDefault write SetIsDefault default False;
property CaptionAlign:TAlignment read FCaptionAlign write SetCaptionAlign default taLeftJustify ;
property BeginAGrp:Boolean read FBeginAGrp write SetBeginAGrp default False ;
property SubPop:Boolean read FSubPop write FSubPop default False ;
end;
这样的不会有问题的,问题是如果你是LIST的话就要用EDITOR来做,其它诸如TCOLOR、STRING/BOOLEAN之类的都会出现的。你说说你的属性是什么类吧
 
把属性声明在published段内
 
//俺的代码是这样的:请各位帮忙看看问题在哪里?
unit DataComboBox;
interface
uses
SysUtils, Classes, Controls, StdCtrls, Dialogs;
type
TDataComboBox = class(TCustomComboBox)
private
FDisplayCount: Integer;
procedure SetDisplayCount(const Value: Integer);
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
published
property DisplayCount:Integer read FDisplayCount write SetDisplayCount;
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TDataComboBox]);
end;
{ TDataComboBox }
procedure TDataComboBox.SetDisplayCount(const Value: Integer);
begin
if Value<0 then Showmessage('Test') else FDisplayCount:=Value;
end;
end.
 
你的代码没问题,我试了。可以的。可能是你的delphi有问题吧。
 
To wonken
请问,继承一个控件后,最基本的,只增加一个属性,而且想让它在设计时就能显示到Object Inspector,需要增加哪些东东?
To 桦树皮
是不是还要什么 Constructor、Destory等等?---我的是D7
 
谢谢各位富翁。
终于发现,我每增加一个新的属性,需要重新编译控件单元文件代码所在的包(不是编译这个单元文件),再看这个窗体就,新属性就出来了。
 
呵呵。那给分吧。:)))
 
请楼上四位富翁到
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2290587
这里领取区区20分,谢谢你们。结贴。
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部