Keep a form centered in your screen  

Send By: Q3 Team
Web : http://www.q3.nu
Email: dlib@q3.nu
Date: 21/12/99

Tip accessed 614 times

 


With this function:



 	   function Alinea(S: String; N: Integer): String;
 	   begin
 	     While Length(S) < N do
 	       Insert(' ',S,Length(S)+1);
 	     Result:=S
 	   end;




A call example:


 	Label1.Caption:=Alinea('hola',10);



a la derecha.
The function return a string of 'N' characters with the input string 'S' right justified.



Update at 03/09/99


Align to left




 function AlineaIzda(S: String; N: Integer): String;
 begin
   Result:=StringOfChar(' ',N-Length(S))+S;
 end;




Align to right




 function AlineaIzda(S: String; N: Integer): String;
 begin
   Result:=S+StringOfChar(' ',N-Length(S));
 end;






apturando el mensaje WM_WINDOWPOSCHANGING:
And we would take ourselves the first disillusion: a horrible effect flicker takes place...
So, investigating a little bit, we will make it capturing the message WM_WINDOWPOSCHANGING:

  • Put this line into private part of your form's declaration:


     	type
     	  TForm1 = class(TForm)
     	    Button1: TButton;
     	    Label1: TLabel;
     	    procedure Button1Click(Sender: TObject);
     	  private
     	    { Private declarations }
     	    {Esta linea/This line}
     	     procedure Centrala(var m: TWMWINDOWPOSCHANGED); message  		
     			WM_WINDOWPOSCHANGING ;
    
    



  • And later, in the implementation part, put this procedure:


     	 procedure TForm1.Centrala(var m : TWMWINDOWPOSCHANGED);
     	 begin
     	       m.windowpos.x := (Screen.Width - Width) div 2;   {Left/Posicion X}
     	       m.windowpos.y := (Screen.Height - Height) div 2; {Left/Posicion X}
     	 end;
    



    And you will have your form centered in the screen permanently, without flicker effect.