Thursday, January 20, 2011

Auto center a form in Delphi

Just call this function to center the form on the screen (for example in the OnShow event).
procedure centerizeform(f:TForm);
begin
  f.Top:=(Screen.WorkAreaHeight-f.Height) div 2;
  if f.Top<0 then f.Top:=0;
  f.Left:=(Screen.WorkAreaWidth-f.Width) div 2;
  if f.Left<0 then f.Left:=0;
end;

_________________________________________________________________________

If the above code doesn't work for you, just  Try This Code Below :


procedure TForm1.FormCreate(Sender: TObject);
begin
 
Left:=(Screen.Width-Width)  div 2;
 
Top:=(Screen.Height-Height) div 2;
end;
 
_________________________________________________________________________
 

Center a form on screen at runtime
 
procedure CenterForm (AForm: TForm); 
var ALeft, ATop: integer; 
begin ALeft : = (Screen.Width - AForm.Width) div 2; 
ATop : = (Screen.Height - AForm.Height) div 2; 
 
{ prevents form being twice repainted! }
AForm.SetBounds (ALeft, ATop, AForm.Width, AForm.Height); end; 
 
_________________________________________________________________________ 
 

Center a form over active form at runtime
procedure CenterFormOverActive (AForm: TForm); 
var ALeft, ATop: integer; 
begin ALeft : = Screen.ActiveForm.Left + (Screen.ActiveForm.Width div 2) - (AForm.Width div 2); 
ATop : = Screen.ActiveForm.Top + (Screen.ActiveForm.Height div 2) - (AForm.Height div 2); 
 
{ prevent form from being outside screen }
if ALeft < 0 then ALeft := Screen.ActiveForm.Left;
if ATop < 0 then ATop := Screen.ActiveForm.Top;
if (ALeft + AForm.Width > Screen.Width) or (ATop + AForm.Height > Screen.Height) then
CenterForm (AForm) else 
{ prevents form being twice repainted! } 
AForm.SetBounds (ALeft, ATop, AForm.Width, AForm.Height); end;
 

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Kang Iwan K-sev | Thank's for your visit To My Site - Ridwan Mulyana | Cibeureum