Jak poprawnie napisać komponent który podobnie jak w np. TImage w Object Inspectorze podaje się jakąś bitmape, bo po umieszczeniu w projekcie napisanego przeze mnie komponentu zaraz po wybraniu bitmapy pokazuje się błąd Access Violation .... [???]
Kod komponentu:
unit AnimeButton1;
interface
uses
SysUtils, Classes, Messages, Controls, Graphics, ExtCtrls;
type
TAnimeButton1 = class(TImage)
private
Timer: TTimer;
FInterval: integer; //szybkość animacji
FEnable: Boolean; //komponent aktywny
{Obrazki do animacji}
FImage0: TBitmap;
FImage1: TBitmap;
FImage2: TBitmap;
FImage3: TBitmap;
FImage4: TBitmap;
FImage5: TBitmap;
FImage6: TBitmap;
Click: Boolean; //kierunek animacji
Postep: Byte; //numer wyswietlanego obrazka
FOnEnter, FOnLeave : TNotifyEvent;
procedure SetInterval(AInterval: integer);
procedure SetEnable(AEnable: Boolean);
procedure SetImage0(AImage0: TBitmap);
protected
procedure OnTimer(Sender : TObject); virtual;
{zdarzenie obsługujące Timer}
procedure CMMouseEnter(var Msg: TMessage); message CM_MOUSEENTER;
{zdarzenie wywoływane po najechaniu kursorem na objekt}
procedure CMMouseLeave(var Msg: TMessage); message CM_MOUSELEAVE;
{zdarzenie wywoływane po zjechaniu kursora z objektu}
public
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
procedure RefreshPaint;
published
property Interval : Integer
read FInterval write SetInterval default 50;
property Enable : Boolean
read FEnable write SetEnable default True;
{Obrazki do animacji}
property Image0 : TBitmap
read FImage0 write SetImage0;
property Image1 : TBitmap
read FImage1 write FImage1;
property Image2 : TBitmap
read FImage2 write FImage2;
property Image3 : TBitmap
read FImage3 write FImage3;
property Image4 : TBitmap
read FImage4 write FImage4;
property Image5 : TBitmap
read FImage5 write FImage5;
property Image6 : TBitmap
read FImage6 write FImage6;
property OnMouseEnter : TNotifyEvent
read FOnEnter write FOnEnter;
property OnMouseLeave : TNotifyEvent
read FOnLeave write FOnLeave;
property Width default 70;
property Height default 35;
end;
procedure Register;
implementation
constructor TAnimeButton1.Create(AOwner : TComponent);
begin
inherited;
Image0 := TBitmap.Create;
Image1 := TBitmap.Create;
Image2 := TBitmap.Create;
Image3 := TBitmap.Create;
Image4 := TBitmap.Create;
Image5 := TBitmap.Create;
Image6 := TBitmap.Create;
FInterval := 50;
Timer := TTimer.Create(Self);
Timer.Interval := FInterval;
Timer.OnTimer := OnTimer;
Timer.Enabled := False;
Height := 35;
Width := 70;
FImage0 := TBitmap.Create;
FImage1 := TBitmap.Create;
FImage2 := TBitmap.Create;
FImage3 := TBitmap.Create;
FImage4 := TBitmap.Create;
FImage5 := TBitmap.Create;
FImage6 := TBitmap.Create;
if FImage0 <> nil then Picture.Bitmap := FImage0;
Click := false;
Postep := 0;
FEnable := true;
end;
destructor TAnimeButton1.Destroy;
begin
inherited;
FImage0.Free;
FImage1.Free;
FImage2.Free;
FImage3.Free;
FImage4.Free;
FImage5.Free;
FImage6.Free;
end;
procedure TAnimeButton1.RefreshPaint;
begin
case Postep of
0: begin
Picture.Bitmap:=FImage0;
Timer.Enabled:=false;
Click:=False;
exit;
end;
1: Picture.Bitmap:=FImage1;
2: Picture.Bitmap:=FImage2;
3: Picture.Bitmap:=FImage3;
4: Picture.Bitmap:=FImage4;
5: Picture.Bitmap:=FImage5;
6: begin
Picture.Bitmap:=FImage6;
Timer.Enabled:=false;
Click:=True;
exit;
end;
end;
end;
procedure TAnimeButton1.OnTimer(Sender: TObject);
begin
if Click then inc(postep) else dec(postep);
case Postep of
0: begin
Picture.Bitmap:=FImage0;
Timer.Enabled:=false;
Click:=False;
exit;
end;
1: Picture.Bitmap:=FImage1;
2: Picture.Bitmap:=FImage2;
3: Picture.Bitmap:=FImage3;
4: Picture.Bitmap:=FImage4;
5: Picture.Bitmap:=FImage5;
6: begin
Picture.Bitmap:=FImage6;
Timer.Enabled:=false;
Click:=True;
exit;
end;
end;
end;
procedure TAnimeButton1.CMMouseEnter(var Msg: TMessage);
begin
if csDesigning in ComponentState then
begin
Exit;
end;
if FEnable then
begin
Click := True;
Timer.Enabled := True;
end;
end;
procedure TAnimeButton1.CMMouseLeave(var Msg: TMessage);
begin
if csDesigning in ComponentState then
begin
Exit;
end;
if FEnable then
begin
Click := False;
Timer.Enabled := True;
end;
end;
procedure TAnimeButton1.SetImage0(AImage0: TBitmap);
begin
FImage0:=AImage0;
Height:=AImage0.Height;
Width:=AImage0.Width;
Picture.Bitmap:=FImage0;
end;
procedure TAnimeButton1.SetEnable(AEnable: Boolean);
begin
Timer.Enabled:=AEnable;
FEnable:=AEnable;
if not AEnable then
Picture.Bitmap:=FImage0;
end;
procedure TAnimeButton1.SetInterval(AInterval: integer);
begin
FInterval:=AInterval;
Timer.Interval:=AInterval;
end;
procedure Register;
begin
RegisterComponents('FFFelek', [TAnimeButton1]);
end;
end.
Tylko nie komentować tego bo dopiero ucze się pisania komponentów.
Nikt <ort>niewie </ort>o co chodzi czy nie chce się nikomu czytać tego kodu? :[