unit KFormPrg;

interface
uses Windows,Forms,Classes,Controls;

procedure CenterForm(f:TForm;const name:string);

implementation

procedure CenterForm(f:TForm;const name:string);
var c:TComponent; pc,pt:TPoint; co : TControl; dy : integer;
begin
  GetCursorPos(pt);
  dy := f.Height - f.ClientHeight;
  c := f.FindComponent(name);
  if  not ( c is TControl ) then begin
    f.Left := pt.x - f.Width div 2; if f.Left < 0 then f.Left := 0;
    f.Top := pt.y - f.Height div 2 - dy; if f.top < 0 then f.Top := 0;
    Exit;
  end;
  co := c as TControl;
  pc.x := co.Width div 2; pc.y := co.Height div 2;
  pc := f.ScreenToClient(co.ClientToScreen(pc));
  f.Left := pt.x - pc.x; if f.Left < 0 then f.Left := 0;
  f.Top := pt.y - pc.y - dy; if f.Top < 0 then f.Top := 0;
end;


end.
 