ControlAtPos Behavior lazarus / 31-LCL ---------------------------------------------------------------------- simsee · 2023-06-04 09:48 In a form, I'm trying to detect the control under mouse cursor with the following code; Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);var  Control : TControl;begin  Control:=ControlAtPos(Point(X,Y),[capfRecursive, capfAllowWinControls]);  if Assigned(Control) then    Caption:=Control.Name  else    Caption:=X.ToString+':'+Y.ToString;end; Unfortunately it doesn't work. Do you have any suggestions? Thank you. ---------------------------------------------------------------------- Josh · 2023-06-04 10:12 formmousemove does not fire all the time, only when mouse is on the form canvas, ie it will not fire when over a tcontrol or twincontrol. your best bet is use a timer that does somethig like this Not tested so double check. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var cntrl:TControl;    CntrlPT:TPoint;begin  CntrlPT:=screentoclient(mouse.CursorPos); // this holds the co- ordinates of the cntrl, in relation to parent.  cntrl:= FindLCLControl(mouse.CursorPos);  if assigned(cntrl) then caption:=cntrl.name ---------------------------------------------------------------------- jamie · 2023-06-04 10:49 Drop a Tapplication on the form. Located on second tab. use the OnUserInput. mouse move code number is 512.. test it using this code Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.ApplicationProperties1UserInput(Sender: TObject; Msg: Cardinal  );begin  caption := Msg.ToString;end; ---------------------------------------------------------------------- simsee · 2023-06-04 10:57 Thanks for the great suggestions. Another question: When there are controls whose surface overlaps, how can I detect them all? ---------------------------------------------------------------------- wp · 2023-06-04 11:07 Quote from: simsee on June 04, 2023, 12:57:19 pmWhen there are controls whose surface overlaps, how can I detect them all? Iterate over all controls on the form and check the mouse position. Untested: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  LCLIntf; procedure TForm1.FindControlsUnderMouse(List: TFPList);var  i: Integer;  c: TControl;  P: TPoint;begin  for i := 0 to ComponentCount-1 do  begin    if Components[i] is TControl then    begin      c := TControl(Components[i]);      P := c.ScreenToClient(Mouse.CursorPos);      if PtInRect(Rect(0, 0, c.Width, c.Height), P) then        List.Add(c);    end;  end;end; ---------------------------------------------------------------------- Zoran · 2023-06-04 11:08 I think that the cleanest way is to add your UserInput event in Application object, when the forms gets activated. Try the attached application and see how it is done! ---------------------------------------------------------------------- Zoran · 2023-06-04 12:02 Quote from: wp on June 04, 2023, 01:07:36 pmQuote from: simsee on June 04, 2023, 12:57:19 pmWhen there are controls whose surface overlaps, how can I detect them all? Iterate over all controls on the form and check the mouse position. Untested: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  LCLIntf; procedure TForm1.FindControlsUnderMouse(List: TFPList);var  i: Integer;  c: TControl;  P: TPoint;begin  for i := 0 to ComponentCount-1 do  begin    if Components[i] is TControl then    begin      c := TControl(Components[i]);      P := c.ScreenToClient(Mouse.CursorPos);      if PtInRect(Rect(0, 0, c.Width, c.Height), P) then        List.Add(c);    end;  end;end;  WP, this checks only Form's direct children. This is your example, updated to check all (direct and indirect) children controls: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm2.ListAllControlsUnderMouse(List: TFPList);   procedure DoListAllControlsUnderMouse(WC: TWinControl);  var    I: Integer;    C: TControl;    P: TPoint;  begin    for I := 0 to WC.ControlCount - 1 do begin      if WC.Controls[I] is TControl then begin        C := TControl(WC.Controls[I]);        if Assigned(C.Parent) then          P := C.Parent.ScreenToClient(Mouse.CursorPos)        else          P := WC.ScreenToClient(Mouse.CursorPos);        if PtInRect(C.BoundsRect, P) then begin          List.Add(C);          if C is TWinControl then            DoListAllControlsUnderMouse(TWinControl(C));        end;    end;    end;  end; begin  DoListAllControlsUnderMouse(Self);end;  I am attaching the same application I previously uploaded, but now included listing of all (overlapped) controls on mouse position. Try! ---------------------------------------------------------------------- wp · 2023-06-04 13:23 Quote from: Zoran on June 04, 2023, 02:02:45 pmWP, this checks only Form's direct children. No, it checks all components owned by the form, and this usually includes all controls sitting directly or indirectly on the form (I do agree that there may be some rare crazy cases where somebody might make the form own a control parented by another form...) ---------------------------------------------------------------------- Zoran · 2023-06-04 14:24 Quote from: wp on June 04, 2023, 03:23:56 pmQuote from: Zoran on June 04, 2023, 02:02:45 pmWP, this checks only Form's direct children. No, it checks all components owned by the form, and this usually includes all controls sitting directly or indirectly on the form (I do agree that there may be some rare crazy cases where somebody might make the form own a control parented by another form...) I see, I though you used Components property (instead of Controls) by accident, sorry. You are right, although my version is still cleaner -- at least for these "rare crazy cases" -- actually, although I don't think I would ever have controls created by another form, it is not rare that I (when creating controls in code) create some controls with nil owner (and take care myself of destroying these correctly it in code), or put a panel or some other control as the owner. I also did one thing wrong -- when converting your procedure, I replaced Components with Controls, but kept the line Code: [Select]if WC.Controls[I] is TControl then, which is now completely unnecessary, although it makes no harm. I really don't like this -- looks like overdefensive programming style, which is just a bad style.