对象检查器不应用更改的属性

时间:2010-01-02 07:33:26

标签: delphi

//我不知道如何更好地提出问题... //

我注册了自己的新课程(TDisplay)。这个类的内部是一个对象(TRectangle - 也是我自己的),它是在TCustomDisplay的构造函数中创建的 - TDisplay的前身。

当我在表单上放置TDisplay时,我可以像往常一样编辑我的两个对象(TDisplay,TRectangle)的所有已发布属性,但只根据对象检查器设置了TDisplay。 TRectangle保持默认值(左0,前0,右30,下20),尽管如此,这些属性设置为左0,前0右100,下100。

我应该检查什么才能使其正确运行?

感谢名单

P.S 当对象检查器中定义的属性实际加载到对象中时?

以下是示例代码(“short one”)。它不是“干净”所以请宽容......

TRectangle = class(TComponent)
  private
    FOnAdjustContent: TNotifyEvent;
    FLeft, FRight, FBottom, FTop:Integer;
    FoutLeft, FoutRight, FoutBottom, FoutTop:integer;
    FinLeft, FinRight, FinBottom, FinTop:integer;
    FAligning:TAligning;
    FAdjustContent:TAdjustContent;
    FAnchors:TAnchors;
    FList:Tlist;
    FRectangleContainer:TRectangleContainer;
    FParentRectangle:TRectangle;
    FdLeft, FdTop, FdBottom, FdRight: integer;
 //   function FindParent(AName:string):TRectangle;


  public    
    constructor Create(AOwner:TComponent);override;
    destructor Destroy; override;
    procedure Assign(Source : TPersistent); override;
  published
    property AdjustContent:TAdjustContent read FAdjustContent write SetAdjustContent;
    property Aligning:TAligning read FAligning write SetAlign;
    property Anchors:TAnchors read FAnchors write SetAnchors;
    .
    .
    .   
  end; 



TTextObject = class (TRectangle)
  public
      procedure   Assign(Source : TPersistent); override;
  published
      property AlignmentInCell:integer read FAlignmentInCell write FAlignmentInCell;
      property Brush:TBrush read FBrush write FBrush;
      property CanMove:Boolean read FCanMove write FCanmove;
      .
      .
      .
  end;





TCustomDisplay = class (TCustomControl)
  private
     FRootTextObject:TTextObject;
     procedure SetRootTextObject(Value:TTextObject);
  protected
  public
     constructor Create(AOwner:TComponent); override;
     destructor  Destroy; override;
     procedure Assign(Source:TPersistent); override;
  published
     property RootTextObject:TTextObject read FRootTextObject write SetRootTextObject;
  end;



  TDesignDisplay = class (TCustomDisplay)
    protected
     procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
     procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
     procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
    public
     procedure Paint; override; 
    end;

  TDisplay = class (TCustomDisplay)
     private

        function  GetFirstMovable: TTextObject;
     protected
        procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
        procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
        procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
     public
        procedure Paint; override;
     end;


procedure Register;
begin
  RegisterComponents('My Palette', [TDisplay, TDesignDisplay]);
end;



constructor TCustomDisplay.Create(AOwner:TComponent);
  var i:integer;
  begin
  inherited;

  FRootTextObject:=TTextObject.Create(Self);
//  ShowMessage(FRootTextObject.Name);
//  FRootTextObject.Name:='Root';
  FRootTextObject.OnAdjustContent:=DoAdjustContent;

//  FRootTextObject.Right:=Width;
//  FRootTextObject.Bottom:=Height;
  FRootTextObject.CanMove:=False;
  TabStop:=True;
  end;



procedure TCustomDisplay.SetRootTextObject(Value:TTextObject);
begin
  FRootTextObject.Assign(Value);
end;




procedure TRectangle.Assign(Source: TPersistent);
var ARectangle:TRectangle;
begin
  if Source is TRectangle then
    begin
   ARectangle:=Source as TRectangle;

 //  Name:=ARectangle.Name;
  // FRect:=Rectangle.Rect;
   FLeft:=ARectangle.Left;
   FTop:=ARectangle.Top;
   FRight:=ARectangle.Right;
   FBottom:=ARectangle.Bottom;
   FoutLeft:=ARectangle.outLeft;
   FoutTop:=ARectangle.outTop;
   FoutRight:=ARectangle.outRight;
   FoutBottom:=ARectangle.outBottom;
   FinLeft:=ARectangle.inLeft;
   FinTop:=ARectangle.inTop;
   FinRight:=ARectangle.inRight;
   FinBottom:=ARectangle.inBottom;
   end
   else
    inherited;
end;



procedure TTextObject.Assign(Source : TPersistent);
var ATextObject:TTextObject;
begin
      inherited;
      if Source is TTextObject then
        begin
        ATextObject:=Source as TTextObject;

        AlignmentInCell:=ATextObject.AlignmentInCell;
        AttachedWP:=ATextObject.AttachedWP;
        Brush.Assign(ATextObject.Brush);
        Caption:=ATextObject.Caption;
        Font.Assign(ATextObject.Font);
        Painting.Assign(ATextObject.Painting);
        Pen.Assign(ATextObject.Pen);
        RichString:=ATextObject.RichString;
        StateDrawing:=ATextObject.StateDrawing;
        TabState:=ATextObject.TabState;
        TextFormat:=ATextObject.TextFormat;
        Thickness:=ATextObject.Thickness;
        Transparent:=ATextObject.Transparent;
        Value:=ATextObject.Value;
        WPname:=ATextObject.WPname;
        end;         
end;

1 个答案:

答案 0 :(得分:7)

确保TRectangle派生自TPersistent类或其后代类之一。还要确保为TRectangle实现了Assign方法,并且在Assign方法中分配了TRectangle的所有已发布属性。

注意:如果您的类派生自TComponent,并且在另一个组件中用作子组件,则应在创建对象时调用SetSubComponent(True)。

代码看起来像这样:

接口部分:

  type    
    TRectangle = class(TComponent)
      private
        FTop,
        FBottom,
        FLeft,
        FRight   : Integer;
      public
        procedure Assign(Source: TPersistent); override;
      published
        property Bottom: Integer read FBottom write FBottom;
        property Left: Integer read FLeft write FLeft;
        property Right: Integer read FRight write FRight;
        property Top: Integer read FTop write FTop;
      end;

      TDisplay = class(TComponent)
      private
        FRectangle : TRectangle;
        procedure SetRectangle(Value: TRectangle);
      public
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
      published
        property Rectangle: TRectangle read FRectangle write SetRectangle;
      end;

实施部分:

{ TRectangle }

procedure TRectangle.Assign(Source: TPersistent);
begin
  if Source is TRectangle then
  begin
    FBottom := TRectangle(Source).Bottom;
    FLeft := TRectangle(Source).Left;
    FRight := TRectangle(Source).Right;
    FTop := TRectangle(Source).Top;
  end
  else
    inherited;
end;

{ TDisplay }

constructor TDisplay.Create(AOwner: TComponent);
begin
  inherited;
  FRectangle := TRectangle.Create(Self);
  FRectangle.Parent := Self;
  FRectangle.SetSubComponent(True);
end;

destructor TDisplay.Destroy;
begin
  inherited;
end;

procedure TDisplay.SetRectangle(Value: TRectangle);
begin
  FRectangle.Assign(Value);
end;