Recent

Author Topic: How to get mouse cursor position on the form?  (Read 1589 times)

JJJ

  • New Member
  • *
  • Posts: 20
How to get mouse cursor position on the form?
« on: April 23, 2022, 10:17:59 am »
I am using OnMouseDown- event to read cursor position on the form with "mouse.cursorpos" when button is clicked, but it doesn't return coordinates over the components. Do I have to make an individual routine for all components or is there an easier way to do this?

Tomi

  • Full Member
  • ***
  • Posts: 102
Re: How to get mouse cursor position on the form?
« Reply #1 on: April 23, 2022, 11:12:03 am »
Hello JJJ!
Try this:

Code: Pascal  [Select][+][-]
  1. if (mouse.cursorpos.X>=Form.left) and (mouse.cursorpos.X<=Form.left+Form.width) then xposonform:=mouse.cursorpos.X-Form.left;

Its logic is similar to Y coordinates:

Code: Pascal  [Select][+][-]
  1. if (mouse.cursorpos.Y>=Form.top) and (mouse.cursorpos.Y<=Form.top+Form.height) then yposonform:=mouse.cursorpos.Y-Form.top;

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to get mouse cursor position on the form?
« Reply #2 on: April 23, 2022, 11:18:43 am »
mouse.CursorPos gives the point in screen coordinates.
You can convert these to coordinates relative to the form/control using one of the Controls methods
ScreenToClient
or

ScreenToControl

JJJ

  • New Member
  • *
  • Posts: 20
Re: How to get mouse cursor position on the form?
« Reply #3 on: April 24, 2022, 09:53:21 am »
That is clear. What I meant is that if I have a form with 10 components do I have to make a routine for all components separately? I have for example form with a panel and shape on it and get mouse coordinates on the form:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormClick(Sender: TObject);
  2. var
  3. pt:tpoint;
  4. begin
  5.    pt:=mouse.CursorPos;
  6.    showmessage(inttostr(pt.x)+' : '+inttostr(pt.Y));
  7. end;
  8.  
  9. end.

If mouse cursor is on top of some component it doesn't return the coords but I have to add similar event for all components. So the question is whether it's possible to read the coordinates at once from the entire area of the form, regardless of whether the cursor is on the component or not

WackaMold

  • New Member
  • *
  • Posts: 11
Re: How to get mouse cursor position on the form?
« Reply #4 on: May 09, 2022, 08:01:48 pm »
Conceptually, wishing for something similar to the KeyPreview property, where the form sees keyboard events before the controls do - except it would be to 'preview' mouse events.

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: How to get mouse cursor position on the form?
« Reply #5 on: May 10, 2022, 12:35:25 am »
Hmm.

 include the LCLintf unit.

 and at some point for example, call the "SetCapture(TheFormTheControlsAreOn.Handle)"
 From this point on, capture will be sent to the form even if the mouse is over the controls and you can use the form's OnMouseMove etc to receive the input instead.

 Doing this of course you need to convert or find what child control is at that position.


 remember the call "ReleaseCapture" at some point.

The only true wisdom is knowing you know nothing

Manlio

  • Full Member
  • ***
  • Posts: 162
  • Pascal dev
Re: How to get mouse cursor position on the form?
« Reply #6 on: May 27, 2022, 11:06:30 am »
What I meant is that if I have a form with 10 components do I have to make a routine for all components separately?

No, you can have a single handler ("MyMouseMove" in the example below) and at runtime assign that handler to all the OnMouseMove properties of all the controls on the form (procedure HookMouseMove in the example below).

This approach allows you to add and remove controls on one or more forms, and not worry about remembering to assign OnMouseMove every time, because they will all be assigned at runtime automatically by a call to HookMouseMove.

Include type info in the "uses" section.


Code: [Select]

procedure TfmEE9.MyMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var P: TPoint;
begin
  if GetCursorPos(P) then begin
    P := ScreenToClient(P);
    // from here P is relative to form (self)
  end;
end;

procedure TfmEE9.HookMouseMove;
  procedure Hook(C: TControl);
  var i: integer;
  begin  // remember: uses typinfo
    if IsPublishedProp(c, 'OnMouseMove') then
      if GetMethodProp(C, 'OnMouseMove').Code = nil then // dont overwrite if already assigned
        SetMethodProp(C, 'OnMouseMove', TMethod(@MyMouseMove));
    if C is TWinControl then  // recursively
      for i := 0 to TWinControl(C).ControlCount-1 do
        Hook(TWinControl(C).Controls[i]);
  end;
begin
  Hook(Self);
end;



« Last Edit: May 28, 2022, 03:29:04 am by Manlio »
manlio mazzon gmail

 

TinyPortal © 2005-2018