Recent

Author Topic: Mouse position in TShape  (Read 1927 times)

francon11

  • Newbie
  • Posts: 4
Mouse position in TShape
« on: November 28, 2018, 02:24:41 am »
So I have to do a project with Lazarus for a school assignment and I decided to make a Mine Sweeper.

I made the whole program based around TShapes, which is probably not a great idea, but I don't have much time.
here is how i generated the grid:
Code: Pascal  [Select][+][-]
  1.  for x:= 1 to 55 do
  2.      begin
  3.          xx:=xx+15;
  4.          yy:=15;
  5.          for y:= 1 to 45 do
  6.           begin
  7.               grid[x,y]:= TShape.Create(Self);
  8.               grid[x,y].Parent := Self;
  9.               grid[x,y].Width := 15;
  10.               grid[x,y].Height := 15;
  11.               grid[x,y].Top := yy;
  12.               grid[x,y].Left := xx;
  13.               grid[x,y].Brush.Color:= clGray;
  14.               grid[x,y].Visible:=false;
  15.               yy:=yy+15;
  16.  
  17.           end;
but the problem I have is that I don't know how to make the program recognize which TShaped i clicked on, i was going to use the mouse position to do so, but whenever the mouse enters a shape the mouse position stop changing. What should I do? Is there any other way I could do that? Or should I start a new project aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: Mouse position in TShape
« Reply #1 on: November 28, 2018, 02:38:43 am »
When creating the shape add a tag to identify it and onclick event.

furious programming

  • Hero Member
  • *****
  • Posts: 858
Re: Mouse position in TShape
« Reply #2 on: November 28, 2018, 02:26:13 pm »
[…] i was going to use the mouse position to do so […]

If you can use a cursor position (e.g. GetCursorPos or position from parameters), you can also use FindLCLControl to grab the reference of the control that is under the cursor.

But creating Mine Sweeper with graphic controls like TShape is a bad idea. IMO the best way to do it is to write a custom control with (virtual) buttons grid and handle the basic messages like LM_MOUSEMOVE, LM_LBUTTONDOWN and so on. Best way, not the fastest or easiest way.
« Last Edit: November 28, 2018, 02:29:55 pm by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

 

TinyPortal © 2005-2018