OK, das wird wohl ein etwas länger Thread
Ich habe eine Klasse von TPanel abgeleitet und möchte dieser Klasse ein Image geben. das ist kein Problem, ich kann ja einfach eins erstellen. Mein Problem ist diesem Image dann ein ONCLICK-Event zur Laufzeit zuzuweisen.
Die Klasse sieht so aus (kompletter code)
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47:
| type TBubbleStone = class (TPanel) protected fForm, fColor: string; fImage: TImage; fIndex, fPoints: Integer; public constructor Create(AOwner: TComponent); override; destructor Destroy; override; procedure Initialize(BallID: integer); published property BallForm: string read fForm write fForm; property BallColor: string read fColor write fColor; property BallPoints: integer read fPoints write fPoints; property Index: integer read fIndex write fIndex; property Image: TImage read fImage write fImage; end;
implementation
constructor TBubbleStone.Create; begin inherited Create(AOwner);
Height := 20; Width := 20;
fImage := TImage.Create(AOwner); fImage.Parent := Self; fImage.Align := alClient; end;
destructor TBubbleStone.Destroy; begin inherited; end;
procedure TBubbleStone.Initialize(BallID: integer); const Colors: array[0..4] of string = ('Blue','Red','Green','White','Orange'); Points: array[0..4] of integer = ( 80, 200, 150, 300, 500);
begin fImage.Picture.LoadFromFile('BITMAPS\Ball'+IntToStr(BallID)+'.bmp'); fColor := Colors[BallID]; fForm := 'Circle'; fPoints := Points[BallID]; end; |
und so will ich das OnClickEvent zuweisen
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33:
| procedure TFormMain.BubbleStoneClick(Sender: TObject); begin if (Sender is TBubbleStone) then begin ShowMessage('You clicked on Stone '+IntToStr((Sender as TBubbleStone).Index)); end; end;
procedure TFormMain.CreateTable(Sender: TObject); var i,l,t: integer; begin Randomize; RandSeed := Random(100);
t := 0; l:= 0; for i:= 1 to BUBBLEMAX do begin Stone[i] := TBubbleStone.Create(FormMain); Stone[i].Parent := FormMain; Stone[i].Name := 'BubbleStone'+IntToStr(i); Stone[i].Top := 60 + t*20; Stone[i].Left := 220 + l*20; Stone[i].Initialize(Random(5)); Stone[i].Index := i; Stone[i].Image.OnClick := BubbleStoneClick; Stone[i].OnClick := BubbleStoneClick; inc(l); if i mod 10 = 0 then begin l := 0; inc(t); end; Application.ProcessMessages; end; end; |
Allerdings bekomme ich keine Reaktion, wenn ich das Programm laufen lasse.
Warum nicht?
