我在编写自定义控件时需要建立一个“对象属性”并且要同时指定该对象属性的部分属性的默认值。代码如下:
unit ImageBox;interfaceuses
  SysUtils, Classes, Controls,Forms,ExtCtrls,Graphics,StdCtrls,Dialogs;
type TBorderPen = class(TPersistent)
  private
    FColor: TColor;
    FWidth: Integer;
    procedure SetColor(Value: TColor);
    procedure SetWidth(Value: Integer);
  public  published
    property Color:TColor read FColor write SetColor default clBlue;
    property Width:Integer read FWidth write SetWidth default 3;
end;type
  TImageBox = class(TScrollBox)
  private
    { Private declarations }
     FBorder:TShape;
     FBorderPen: TBorderPen;
     Fa1:boolean;
  protected
    { Protected declarations }
     procedure SetSelectedStyle(Value:TBorderPen);
  public
    { Public declarations }
    constructor Create(AOwner: TComponent);override;
    destructor Destroy(); override;  published
    { Published declarations }
    property SelectedStyle: TBorderPen read FBorderPen write SetSelectedStyle;
    property abc: boolean read fa1 write fa1 default true;
end;procedure Register;implementationprocedure Register;
begin
  RegisterComponents('我的组件', [TImageBox]);
end;procedure TBorderPen.SetColor(Value:TColor);
begin
  if (Color <> Value) then
  begin
    FColor := Value;
  end;
end;procedure TBorderPen.SetWidth(Value:Integer);
begin
  if (Width <> Value) then
  begin
    FWidth := Value;
  end;
end;
constructor TImageBox.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FBorderPen := TBorderPen.Create();
  FBorder := TShape.Create(self);
  try
    //FBorder.Parent := self;
    //FBorder.Show();
  except
  end;
end;destructor TImageBox.Destroy;
begin
  FBorder.Destroy();
  FBorderPen.Destroy();
  inherited Destroy;
end;procedure TImageBox.SetSelectedStyle(Value: TBorderPen);
begin
  if (Assigned(Value)) then
  begin
    FBorderPen := Value;
    FBorder.Pen.Color := Value.Color;
    FBorder.Pen.Width := Value.Width;
  end;
end;end.
可是,无法修改属性值,并且默认属性值也无法设置。
我的环境是Delphi 7.0

解决方案 »

  1.   

    property Color:TColor read FColor write SetColor default clBlue;
    //这里的“default”是留给属性编辑器(Object Inspector)的~~
    //代码中并不会执行~~constructor TImageBox.Create(AOwner: TComponent);
    ....
      FColor := clBlue;
    ....
    //所有还需要在构造方法中初始化一偏~~
      

  2.   

    property abc: boolean read fa1 write fa1 default true;此处的DEFAULT不能当做初始化, 必须在CREATE里面如ZSWANG所说才是初始化,记得是在哪本里见就是这末说的
      

  3.   

    就是标价138 1000多页的那本里面关于VCL章节
      

  4.   

    上面的几位已经说了很多了,我归纳以下:
    1。default 标识不会影响到程序的任何行为,
          他只是一个存储标志(storage specifiers)。
    2。在dfm文件中保存控件的各个属性参数时会比较值是否与default指定的值相等,如果相等则不保存到文件中,如果相等则不保存,这个自己可以做一个试验就知道了,主要是为了减少文件大小。
    3。初始化property时必须在create中初始化。
    4。没有标明default的property 默认为 nodefault.
      

  5.   

    那么,为什么在属性修改奇中修改SelectedStyle的属性值无效。
      

  6.   

    TBorderPen = class(TPersistent)
      private
        FColor: TColor;
        FWidth: Integer;
        procedure SetColor(Value: TColor);
        procedure SetWidth(Value: Integer);
      public
        constructor Create;
      published
        property Color:TColor read FColor write SetColor default clBlue;
        //上面这个default是没有用的,下面同
        property Width:Integer read FWidth write SetWidth default 3;
    end;
    { TBorderPen }constructor TBorderPen.Create;
    begin
      inherited Create;
      //初始值
      FColor := clBlue;
      FWidth := 3;
    end;procedure TBorderPen.SetColor(Value: TColor);
    begin
      FColor := value;
    end;procedure TBorderPen.SetWidth(Value: Integer);
    begin
      FWidth := Value;
    end;
      

  7.   

    修改你的代码
    procedure TImageBox.SetSelectedStyle(Value: TBorderPen);
    begin
      if (Assigned(Value)) then
      begin
        FBorderPen := Value;
        FBorder.Pen.Color := Value.Color;
        FBorder.Pen.Width := Value.Width;
      end;
    end;
    如下:
    procedure TImageBox.SetSelectedStyle(Value: TBorderPen);
    begin
      if (Assigned(Value)) then
      begin
        FBorderPen.Assign(Value);
      end;
    end;
      

  8.   

    如果按hawksoft(明月清风) 兄所说。需要重写Assing方法。如下:
    procedure TBorderPen.Assign(Source: TPersistent);
    begin
        if (Source is TBorderPen) then
        begin
          FColor := TBorderPen(Source).Color;
          FWidth := TBorderPen(Source).Width;
          inherited Assign(Source);
        end;
    end;这样,不但没起到应有的作用,反而在运行时会提示:TBorderPen不能转换到TBorderPen。
    难道真的自己设计属性编辑器吗。delphi7属性编辑器设计同delphi5获6的方法一样嘛。
      

  9.   

    property Color:TColor read FColor write SetColor default clBlue;
      

  10.   

    不能改变属性应该不是程序的事,程序运行应该很正常。procedure TImageBox.SetSelectedStyle(Value: TBorderPen);
    begin
      if (Assigned(Value)) then
      begin
        fborderpen.free;       //原来的对象要释放。
        FBorderPen := Value;   //value在函数外创建后不能销毁。 
        FBorder.Pen.Color := Value.Color;
        FBorder.Pen.Width := Value.Width;
      end;
    end;
      

  11.   

    Delphi的Object Inspector中显示出来的属性值都是字符串。当你为指定的属性赋值时,OI要有办法将你输入的字符串转换成期望的类型,这个过程一般是通过属性编辑器的SetValue方法完成的。对于简单数据类型,如Integer类型的属性编辑器TIntegerProperty,其SetValue方法是这样完成的:
    TIntegerProperty.SetValue(Value: string);
    begin
      SetIntValue(Value); //将string转换成Integer并赋给该属性
    end;
    这个属性编辑器在DesignEditors.pas中已经有了。对于你的TBorderPen类型的属性,Delphi完全不知道该如何转换(没有该类型的属性编辑器),所以你得自己写一个
    PS:你在运行时为该属性指定一个值,应该是可以成功的。