写了一个简单的组件,不能使用,请高手指点(100分)

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

wind_2005

Unregistered / Unconfirmed
GUEST, unregistred user!
我写了一个简单的组件,该组件用一画一个矩形或椭圆,
源码如下:
--------------------
unit MyShape;

interface

uses
Windows, Messages, SysUtils, Classes, Controls, Graphics;

type
TMyShapeType = (mstRectangle, mstEllipse);

TMyShape = class(TGraphicControl)
private
FShapeType: TMyShapeType;
FBrush: TBrush;
FPen: TPen;
procedure SetShapeType(const Value: TMyShapeType);
procedure SetBrush(const Value: TBrush);
procedure SetPen(const Value: TPen);
procedure ChangeShape(Sender: TObject);
{ Private declarations }
protected
procedure Paint;override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;

{ Public declarations }
published
property Shape: TMyShapeType read FShapeType write SetShapeType default mstRectangle;
property Width default 50;
property Height default 50;
property Pen: TPen read FPen write SetPen;
property Brush: TBrush read FBrush write SetBrush;
{ Published declarations }
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TMyShape]);
end;

{ TMyShape }

procedure TMyShape.ChangeShape(Sender: TObject);
begin
Invalidate;
end;

constructor TMyShape.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FShapeType := mstRectangle;
Width := 50;
Height := 50;
FPen.Create;
FBrush.Create;
FPen.OnChange := ChangeShape;
FBrush.OnChange := ChangeShape;

end;

destructor TMyShape.Destroy;
begin
FPen.Free;
FBrush.Free;

inherited;
end;

procedure TMyShape.Paint;
begin
with Canvas do
begin
Pen := FPen;
Brush := FBrush;

case FShapeType of
mstRectangle:
begin
Rectangle(0, 0, Width, Height);
end;
mstEllipse:
begin
Ellipse(0, 0, Width, Height);
end;
end;
end;

end;

procedure TMyShape.SetBrush(const Value: TBrush);
begin
FBrush.Assign(Value);
Invalidate;
end;

procedure TMyShape.SetPen(const Value: TPen);
begin
FPen.Assign(Value);
Invalidate;
end;

procedure TMyShape.SetShapeType(const Value: TMyShapeType);
begin
if FShapeType <> Value then
begin
FShapeType := Value;
Invalidate;
end;

end;

end.
-----------------
该组件编译并安装成功,
但是我想把该组件放至一个窗体时,提示:Access violation at address 400B5FD9 in module 'vcl60.bpl'. Write of address 00000010.

不知问题出在哪里,还望各位高手指点。
谢谢!!!!!
 
先看看对象是怎样创建的吧,入门后再考虑写组件

constructor TMyShape.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FShapeType := mstRectangle;
Width := 50;
Height := 50;
FPen := TPen.Create;// FPen.Create;
FBrush := TBrush.Create;//FBrush.Create;
FPen.OnChange := ChangeShape;
FBrush.OnChange := ChangeShape;

end;
 
呵呵,谢谢你了,老是犯这样的错误,惭愧惭愧!
 
后退
顶部