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.
不知问题出在哪里,还望各位高手指点。
谢谢!!!!!
源码如下:
--------------------
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.
不知问题出在哪里,还望各位高手指点。
谢谢!!!!!