How to move the mouse cursor to the focused control when the form is displayed

Submitted by:Tim Kelly

Date added:24 August, 2011

Category:Delphi

The ActiveControl property of a Delphi Form object specifies what control has the input focus. You can use this property to set what control is (initially) focused when the form is created and displayed to the user.

If you want to move the mouse cursor to the control with the focus, you can use the next code in the form's OnCreate event handler (the form is named "Form1") :

Tags: delphi form object

Code Snippet:

procedure TForm1.FormCreate(Sender: TObject) ;
var
mousePos : TPoint;
focusControl : TControl;
begin
focusControl := ActiveControl;
if focusControl = nil then
begin
focusControl := Controls[0];
end;

if focusControl <> nil then
begin
mousePos.X := focusControl.Left + focusControl.Width div 2;
mousePos.Y := focusControl.Top + focusControl.Height div 2;

Mouse.CursorPos := ClientToScreen(mousePos) ;
end;
end;
 
 

Comments