Mega Code Archive

 
Categories / Delphi / Hardware
 

Center the mouse pointer on the focused control

Title: Center the mouse pointer on the focused control Question: How to move the mouse automatically to the center of the control that has the focus... Answer: We need a function that can be used for any visual control, e.g. inside the OnEnter event: procedure TForm1.Button1Enter(Sender: TObject); begin MoveMouseOverControl(Sender); end; Here is it: procedure MoveMouseOverControl(Sender: TObject); var x,y: integer; point: TPoint; begin with TControl(Sender) do begin x:= left + (width div 2); y:= top + (height div 2); point:= Parent.ClientToScreen(point); SetCursorPos(point.x, point.y); end; end;