Forum > LCL
ControlAtPos Behavior
simsee:
In a form, I'm trying to detect the control under mouse cursor with the following code;
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.ApplicationProperties1UserInput(Sender: TObject; Msg: Cardinal );begin caption := Msg.ToString;end;
simsee:
Thanks for the great suggestions. Another question: When there are controls whose surface overlaps, how can I detect them all?
wp:
--- Quote from: simsee on June 04, 2023, 12:57:19 pm ---When there are controls whose surface overlaps, how can I detect them all?
--- End quote ---
Iterate over all controls on the form and check the mouse position. Untested:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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;
Navigation
[0] Message Index
[#] Next page