Hacer que el formulario se pegue a los bordes

2344 vistas

Si queremos que nuestro formulario se pegue a los bordes del escritorio de Windows cuando éste se acerque a ellos, lo único que tendremos que hacer es capturar el mensaje de Windows WM_WINDOWPOSCHANGING



delphi
  1. ...
  2.   private
  3.     procedure PegaAlBorde(var m: TWMWINDOWPOSCHANGED); message WM_WINDOWPOSCHANGING ;
  4. ...
  5.  
  6. implementation
  7.  
  8. procedure TForm1.PegaAlBorde(var m : TWMWINDOWPOSCHANGED);
  9. const
  10.   Sensibilidad: integer = 4;
  11. var
  12.   Dato: TRect;
  13. begin
  14.   try
  15.     { SystemParametersInfo sólo funciona en Win9x }
  16.     SystemParametersInfo(SPI_GETWORKAREA,0,@Dato,0);
  17.     if m.windowpos.x <= (Dato.Left + Sensibilidad) then m.windowpos.x := Dato.Left;
  18.     if m.windowpos.y <= (Dato.Top + Sensibilidad) then m.windowpos.y := Dato.Top;
  19.  
  20.     if (m.windowpos.x + Width) >= (Dato.Right - Sensibilidad) then
  21.       m.windowpos.x := Dato.Right - Width;
  22.     if (m.windowpos.y + Height) >= (Dato.Bottom - Sensibilidad) then
  23.       m.windowpos.y := Dato.Bottom-Height;
  24.   except
  25.     { si estamos en un SO diferente a Win9x... }
  26.     if m.windowpos.x <= Sensibilidad then m.windowpos.x := 0;
  27.     if m.windowpos.y <= Sensibilidad then m.windowpos.y := 0;
  28.     if (m.windowpos.x + Width) >= (Screen.Width - Sensibilidad) then
  29.       m.windowpos.x := Screen.Width - Width;
  30.     if (m.windowpos.y + Height) >= (Screen.Height - Sensibilidad) then
  31.       m.windowpos.y := Screen.Height - Height;
  32.   end;
  33. end;