Recent

Author Topic: TShape created at runtime OnClick event  (Read 558 times)

TomTom

  • Full Member
  • ***
  • Posts: 170
TShape created at runtime OnClick event
« on: October 20, 2022, 07:48:21 pm »
I created small program, where user can add TShape by clicking on a form.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  2.   Shift: TShiftState; X, Y: Integer);
  3. var
  4.   a: TShape;
  5. begin
  6.   label1.caption:='';
  7.   a:=TShape.create(self);
  8.   a.Shape:=stRectangle;
  9.   a.Brush.Color:=clRed;
  10.   a.Width:=random(20)+5;
  11.   a.Height:=20;
  12.   a.Left:=x;
  13.   a.top:=y;
  14.   a.Parent:=self;
  15.   a.Caption:=(random(500)+12).ToString;
  16. end;

What I need is a a.OnClick() or a.OnMouseUp() events so whenever user clicks on a particular shape TLabel on a form displays caption of that shape (for now its just random number but in future I will need to pass a lot of custom parameters).

How to write this onClick() procedure? I don't know how to access each instance.
« Last Edit: October 20, 2022, 07:57:18 pm by TomTom »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TShape created at runtime OnClick event
« Reply #1 on: October 20, 2022, 08:52:25 pm »
The attached project shows one way to achieve this.

TomTom

  • Full Member
  • ***
  • Posts: 170
Re: TShape created at runtime OnClick event
« Reply #2 on: October 20, 2022, 10:08:18 pm »
Oh, right. I can store shapes (finally it will be something else) in an array. Thank You :), but could You kindly explain this to me ?:
Code: Pascal  [Select][+][-]
  1. var
  2.   shape: TShape absolute Sender;  

that does that mean?

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: TShape created at runtime OnClick event
« Reply #3 on: October 20, 2022, 10:24:48 pm »
The Shape variable occupies the same memory as the Sender parameter.
You could use the same with code like this:
Code: Pascal  [Select][+][-]
  1.    ...
  2.   Shape := TShape(Sender)
or
Code: Pascal  [Select][+][-]
  1.   ...
  2.   with (Sender as TShape) do ...

In the end, Sender is treated as if it were a TShape (something you should only do if you are sure Sender actually is a TShape).

See this wiki page for more explanation.

Bart

TomTom

  • Full Member
  • ***
  • Posts: 170
Re: TShape created at runtime OnClick event
« Reply #4 on: October 20, 2022, 10:59:35 pm »
Oh. Now I think I understand, but this version
Code: Pascal  [Select][+][-]
  1. ...
  2. with (Sender as TShape) do ...
is somehow more understandable for me in this case.
I need that because I'm creating a crude version of interactive map. In editor part/mode in my program user will add Towns on map. These objects will hold few properties such as location on world map (BGRABitmap  X,Y coords), Town Map (Image), Description and maybe some more. All these towns created/placed on World Map will be saved in a XML WorldMapFile and then user will be able to load WorldMapFile.xml and all Town objects will be placed on World Map object if You know what I mean. Also there will be other types of objects that can be placed on WorldMap (Dungeons, Villages, Monuments, Special Places etc), but they will be propably the almost the same as Towns (image, location, description). User will be able to click on each object which will show a window or something with images, descriptions etc.
I hope to create a client for solo playing Mythic RPG if You've heard about it :).   


 

TinyPortal © 2005-2018