关于TComponentEditor和TStringProperty的问题(200分)

  • 主题发起人 主题发起人 LiChaoHui
  • 开始时间 开始时间
L

LiChaoHui

Unregistered / Unconfirmed
GUEST, unregistred user!
我设计了一个组件,
希望能在用户双击组件时,打开一个字段(属性)的编辑器
就像TMainMenu的Items属性一样

我知道需要有一个组件编辑器,由它来调用字段(属性)编辑器
但是怎么打开属性编辑器呢?

希望熟悉组件设计器编程的高手们给予帮助

凡发言皆有分
 
随便定义了一个新控件TMyLabel(从TLabel继承).并定义了一个控件编辑器,双击控件可以编辑Caption属性
//D6下通过

//MyLabel
unit MyLabel;

interface

uses
Windows, Messages, SysUtils, Classes,
Controls, StdCtrls, Forms;

type
TMyLabel = class(TLabel);

procedure Register;

implementation

uses
DesignEditors, DesignIntf,

MyLabelForm;

type
TMyLabelEditor = class(TComponentEditor)
public
procedure Edit; override;
end;

{ TMyLabelEditor }

procedure TMyLabelEditor.Edit;
var
Lbl: TMyLabel;
begin
with TfrmMyLabel.Create(Application) do
try
Lbl := GetComponent as TMyLabel;
memCaption.Lines.Text := Lbl.Caption;
if ShowModal = mrOk then
Lbl.Caption := memCaption.Lines.Text;
finally
Free;
end;
end;

procedure Register;
begin
RegisterComponents('Samples', [TMyLabel]);
RegisterComponentEditor(TMyLabel, TMyLabelEditor);
end;

end.

//MyLabelForm.dfm
object frmMyLabel: TfrmMyLabel
Left = 226
Top = 77
BorderStyle = bsDialog
Caption = 'My Label Editor'
ClientHeight = 249
ClientWidth = 479
Color = clBtnFace
Font.Charset = GB2312_CHARSET
Font.Color = clWindowText
Font.Height = -12
Font.Name = '宋体'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 12
object memCaption: TMemo
Left = 0
Top = 0
Width = 479
Height = 201
Align = alTop
ScrollBars = ssBoth
TabOrder = 0
end
object btnOk: TButton
Left = 304
Top = 216
Width = 75
Height = 25
Caption = '确定(&O)'
ModalResult = 1
TabOrder = 1
end
object btnClose: TButton
Left = 392
Top = 216
Width = 75
Height = 25
Caption = '关闭(&C)'
ModalResult = 2
TabOrder = 2
end
end

//MyLabelForm
unit MyLabelForm;

interface

uses
Windows, Messages, SysUtils, Variants,
Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TfrmMyLabel = class(TForm)
memCaption: TMemo;
btnOk: TButton;
btnClose: TButton;
private
{ Private declarations }
public
{ Public declarations }
end;

implementation

{$R *.dfm}

end.
 
这种方法我是知道的,不过还是很感谢你
我的那个字段已经有了一个属性编辑器,
字段为TCollection,类型,有默认的编辑器,
我是想在双击组件时能够显示此编辑器
 
楼主:JohnsonGuo说的没错呀

双击MyLabel控件弹出Caption字段的编辑器
 
To LiChaoHui:
你只要用RegisterComponentEditor把自己的控件编辑器注册,
那么双击的时候就显示自己的编辑器啦.
 
我想各位还是没有明白我的意思,
你们所说的我都已经实现了,
我只是想让一个字段属性编辑器来充当组件编辑器

就像TMainMenu的Items属性那样
双击组件和修改Items属性,都出现同样的设计器
TCollection 有默认的属性编辑器,
但是我怎么调用它呢

多谢各位关注,凡关注都有分
 
type
TPersistentCracker = class(TPersistent);

procedure TMyEditor.Edit;
var
Obj: TPersistent;
begin
Obj := Component;
while (Obj <> nil) and not (Obj is TComponent) do
Obj := TPersistentCracker(Obj).GetOwner;
ShowCollectionEditorClass(Designer, TCollectionEditor,
TComponent(Obj), TCollection((Obj as TMyComponent).MyProperty),
'MyProperty', [coAdd, coDelete, coMove]);
end;

 
ok,多谢,我试一试
 
已经测试通过,多谢 JohnsonGuo 兄,
如果有用的着的地方,尽管开口
 
后退
顶部