Author Topic: Custom class OnMouseDown  (Read 523 times)


  • Full Member
  • ***
  • Posts: 170
Custom class OnMouseDown
« on: October 27, 2022, 10:26:15 pm »
Let's say I have something like this:
Code: Pascal  [Select][+][-]
  1. type
  3.   { TMapObject }
  5.   TMapObject = class
  6.   private
  7.       FRect: TRect;
  8.       FDesc: String;
  9.     procedure SetRect(aRect: TRect);
  10.   public
  11.   property Desc: string read FDesc write FDesc;
  12.   property Rect: TRect read FRect write SetRect;
  13.   end;

Is it possible and if it is then how Can I add procedures OnMouseDown, OnMouseMove, OnMouseUp to this ? I need that it can detect mouse position (x,y) and mouse clicks.

I wanted to describe it in details but for now it's to hard for me to do so.

For now I'm using this... It's ok as it works but it checks array which in final product will be HUGE (over 1000 objects).
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  2.   Shift: TShiftState; X, Y: Integer);
  3. var
  4.   i: Integer;
  5. begin
  6.     for i:= 0 to length(list)-1 do
  7.   Begin
  8.     if list[i].rect.Contains(Point(x,y)) then Form1.Caption:=list[i].desc;
  9.   end;
  10. end;  

I need to create something that will act as an clickable area. It will have its position (TRect.TopLeft) and width/height, description (TString or TStrings I guess). I also need it to have an event OnMouseDown so it can be clicked. When it's clicked I need to execute procedure that will for example add a line to TMemo.Lines.add(ClickedMapObject.Desc + ' ' + ClickedMapObject.Rect.TopLeft); or any other property that it have.

It's doesn't need to be rendered in any form. I need it to be area that is virtual (?) and have OnMouseDown/OnMouseUp/OnMouseEnter etc.
Maybe there is something else that I could use. I don't know.

« Last Edit: October 27, 2022, 10:56:32 pm by TomTom »


  • Hero Member
  • *****
  • Posts: 1171
  • Former Turbo Pascal 3 user
Re: Custom class OnMouseDown
« Reply #1 on: October 27, 2022, 11:27:00 pm »
I don't really follow your requirements. Including a 'break' in your 'if then' would speed things up though.
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.4 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.4 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.4 (64 bit on VBox)


  • Full Member
  • ***
  • Posts: 170
Re: Custom class OnMouseDown
« Reply #2 on: October 27, 2022, 11:37:49 pm »
Yeah I know... I find it hard to describe it. I will try to describe it better tomorrow with some screen shots so I hope it will be easier to understand what do I need this for or what I want to achieve.


  • Hero Member
  • *****
  • Posts: 4878
    • Bart en Mariska's Webstek
Re: Custom class OnMouseDown
« Reply #3 on: October 28, 2022, 04:10:55 pm »
Your class is not a visual component, so how can it have MouseEvents?


  • Hero Member
  • *****
  • Posts: 5044
Re: Custom class OnMouseDown
« Reply #4 on: October 29, 2022, 02:21:21 am »
I think the poster is looking for a transparent type of overlay that can receive mouse messages, and optionally draw in it maybe,

you can create a transparent window where it actually only works well in Windows, or you can use a TGraphicsControl that you create whereby you set the parent so it can receive the mouse messages but don't draw on it so you can still see the background.

The only true wisdom is knowing you know nothing


  • Full Member
  • ***
  • Posts: 170
Re: Custom class OnMouseDown
« Reply #5 on: November 03, 2022, 07:46:50 pm »
Thanks everyone. I managed to solve this. Maybe it's not perfect solution but it works for me.
I made TMapArea class with TRect and some other properties that I need (name, description, image ...)
Im adding TMapArea objects to dynamic array. Then in OnMouseDown of my map canvas (TBGRAVirtualScreen) I'm checking in a loop if clicked point is in TRect of any of my objects).
This red square is just Shape so user can see what, and where they're placing. It's moveable and dissapears when adding places is finished.


TinyPortal © 2005-2018