如何重载一个控件的标准属性?(200分)

  • 主题发起人 主题发起人 whzww
  • 开始时间 开始时间
W

whzww

Unregistered / Unconfirmed
GUEST, unregistred user!
我现在想设计一个扩展的 edit 控件,设置一个控制输入类型的属性InputType(Datetime,float,integer,string),当决定此editEx的允许输入类型以后,改动edit的color属性,可是我现在 在boject inspector内 可以通过改变InputType的设置改动Color属性,但是editEx的实际颜色 却没有变化,color属性的改变对它不起作用了,这是什么问题呢,那位高人帮忙看看

----------------测试源码-----------------------
unit IntEdit;

interface

uses
SysUtils, Classes, Controls, StdCtrls, Graphics, Messages;
type
TInType = (IsDatetime, IsFloat, IsInteger, IsString);
type
TStyleOption = (Flat, UseLabel);
type
TStyleOptions = set of TStyleOption;
type
TIntEdit = class(TEdit)
private
{ Private declarations }
FInputType: TInType;
FStyleOptions: TStyleOptions;
FAColor: string;
FColor: TColor;
FBrush:TBrush;
procedure SetAcolor(Value: string);
function GetAColor: string;
procedure setcolor(Value: TColor);
function GetColor: TColor;
procedure CMColorChanged(var Message: TMessage); message CM_ColorChanged;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property InputType: TInType read FInputType write FInputType;
property Options: TStyleOptions read FStyleOptions write FStyleOptions;
property InColor: string read GetAColor write FAColor;
property Color: TColor read GetColor write SetColor;
end;

procedure Register;

implementation

constructor TIntEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Color := FColor;
end;

destructor TIntEdit.Destroy;
begin
inherited Destroy;
end;

procedure TintEdit.CMColorChanged(var Message: TMessage);
begin
inherited;
FBrush.Color := FColor;
NotifyControls(CM_PARENTCOLORCHANGED);
end;

procedure TIntEdit.SetAcolor(Value: string);
begin
FAcolor := Value;
end;

function TIntEdit.GetAColor: string;
begin
if FInputType = isDatetime then
begin
FColor := ClWhite;
Result := 'Clwhite';
end;
if FInputType = isFloat then
begin
FColor := clred;
Result := 'ClRed';
end;
if FInputType = isInteger then
begin
FColor := Clblue;
Result := 'ClBlue';
end;
if FInputType = isString then
begin
Fcolor := ClGreen;
Result := 'ClGreen';
end;
end;

procedure TintEdit.setcolor(Value: TColor);
begin
if FColor <> Value then
begin
FColor := Value;
Perform(CM_COLORCHANGED, 0, 0);
end;
end;

function TIntEdit.GetColor: TColor;
begin
if FInputType = isDatetime then
Result := Clwhite;
if FInputType = isFloat then
Result := ClRed;
if FInputType = isInteger then
Result := ClBlue;
if FInputType = isString then
Result := ClGreen;
end;

procedure Register;
begin
RegisterComponents('Standard', [TIntEdit]);
end;

end.

 
按照你的意思,你改变颜色的Code应该放在SetInputType方法里啊?
建议,
Set InputType属性用一个SetInputType方法,
 
你发布的color属性重新写了set和get的方法,覆盖了父类的方法,所以显示不出来了。
 
帮你修改了一下代码:
unit IntEdit;

interface

uses
SysUtils, Classes, Controls, StdCtrls, Graphics, Messages;
type
TInType = (IsDatetime, IsFloat, IsInteger, IsString);
type
TStyleOption = (Flat, UseLabel);
type
TStyleOptions = set of TStyleOption;
type
TIntEdit = class(TEdit)
private
{ Private declarations }
FInputType: TInType;
FStyleOptions: TStyleOptions;
FAColor: string;
// FColor: TColor; // 这里就不用再申明了嘛,直接用父类的color属性操作就可以了。
FBrush:TBrush;
procedure SetAcolor(Value: string);
function GetAColor: string;
// procedure setcolor(Value: TColor); 去掉
// function GetColor: TColor; //去掉。
procedure CMColorChanged(var Message: TMessage); message CM_ColorChanged;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property InputType: TInType read FInputType write FInputType;
property Options: TStyleOptions read FStyleOptions write FStyleOptions;
property InColor: string read GetAColor write FAColor;
// property Color: TColor read GetColor write SetColor; //父类已经给你发布了color属性。
end;

procedure Register;

implementation

constructor TIntEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
//Color := FColor;
end;

destructor TIntEdit.Destroy;
begin
inherited Destroy;
end;

procedure TintEdit.CMColorChanged(var Message: TMessage);
begin
inherited;
// FBrush.Color := FColor;
NotifyControls(CM_PARENTCOLORCHANGED);
end;

procedure TIntEdit.SetAcolor(Value: string);
begin
FAcolor := Value;
end;

function TIntEdit.GetAColor: string;
begin
if FInputType = isDatetime then
begin
Color := ClWhite;
Result := 'Clwhite';
end;
if FInputType = isFloat then
begin
Color := clred;
Result := 'ClRed';
end;
if FInputType = isInteger then
begin
Color := Clblue;
Result := 'ClBlue';
end;
if FInputType = isString then
begin
color := ClGreen;
Result := 'ClGreen';
end;
end;
{
procedure TintEdit.setcolor(Value: TColor);
begin
if FColor <> Value then
begin
FColor := Value;
Perform(CM_COLORCHANGED, 0, 0);
end;
end;

function TIntEdit.GetColor: TColor;
begin
if FInputType = isDatetime then
Result := Clwhite;
if FInputType = isFloat then
Result := ClRed;
if FInputType = isInteger then
Result := ClBlue;
if FInputType = isString then
Result := ClGreen;
end;
}
procedure Register;
begin
RegisterComponents('Standard', [TIntEdit]);
end;

end.
 
后退
顶部