// Wenn man die Nachricht WM_WINDOWPOSCHANGING abfängt, kann man
// das "Einrasten" eines Fensters programmieren.


// Getestet mit D4 unter XP

// Beispiel 1: Form1 rastet am Bildschirmrand ein 
 
type 
  TForm1 = class(TForm) 
  private 
    { Private-Deklarationen } 
  public 
    procedure POSCHG(var m: TWMWindowPosMsg); message WM_WINDOWPOSCHANGING; 
  end; 
 
var 
  Form1: TForm1; 
 
implementation 
 
{$R *.DFM} 
 
const 
  sdiff = 20; 
  wdiff = 25; 
 
procedure TForm1.POSCHG(var m: TWMWindowPosMsg); 
var rec: TRect; 
begin 
  DefaultHandler(m); 
  SystemParametersInfo(SPI_GETWORKAREA, 0, @rec, 0); 
  if (m.WindowPos.y < rec.top + sdiff) and (m.WindowPos.y > rec.top - sdiff) 
    then m.WindowPos.y := rec.top else 
    if (m.WindowPos.y + m.WindowPos.cy > (rec.bottom - sdiff)) and 
      (m.WindowPos.y + m.WindowPos.cy < (rec.bottom + sdiff)) then 
      m.WindowPos.y := rec.bottom - m.WindowPos.cy; 
  if (m.WindowPos.x < rec.left + wdiff) and (m.WindowPos.x > rec.left - wdiff) 
    then m.WindowPos.x := rec.left else 
    if (m.WindowPos.x + m.WindowPos.cx > (rec.right - wdiff)) and 
      (m.WindowPos.x + m.WindowPos.cx < (rec.right + wdiff)) then 
      m.WindowPos.x := rec.right - m.WindowPos.cx; 
end; 
 
//---------------------------------------------------------------------------- 
 
// Beispiel 2: Form2 haftet an allen Ecken und Kanten von Form1 
 
type 
  TForm2 = class(TForm) 
    procedure FormActivate(Sender: TObject); 
  private 
    { Private-Deklarationen } 
  public 
    procedure POSCHG(var m: TWMWindowPosMsg); message WM_WINDOWPOSCHANGING; 
  end; 
 
var 
  Form2: TForm2; 
 
implementation 
 
uses Unit1; 
 
{$R *.DFM} 
 
const 
  diff = 22; 
 
var 
  munten, punten, moben, poben, mlinks, plinks, mrechts, prechts: integer; 
  rec: TRect; 
 
procedure TForm2.FormActivate(Sender: TObject); 
begin 
  munten := Form1.boundsrect.bottom - diff; 
  punten := Form1.boundsrect.bottom + diff; 
  moben := Form1.boundsrect.top - diff; 
  poben := Form1.boundsrect.top + diff; 
  mlinks := Form1.boundsrect.left - diff; 
  plinks := Form1.boundsrect.left + diff; 
  mrechts := Form1.boundsrect.right - diff; 
  prechts := Form1.boundsrect.right + diff; 
  rec := Form1.boundsrect; 
  inflaterect(rec, -diff, -diff); 
end; 
 
procedure TForm2.POSCHG(var m: TWMWindowPosMsg); 
var r: TRect; 
begin 
  DefaultHandler(m); 
  if not IntersectRect(r, rec, self.boundsrect) then begin 
    if (left > mlinks - width) and (left < prechts) then begin 
      if (m.WindowPos.y < punten) and (m.WindowPos.y > munten) 
        then m.WindowPos.y := Form1.boundsrect.bottom else 
        if (m.WindowPos.y + height > moben) and 
          (m.WindowPos.y + height < poben) then 
          m.WindowPos.y := Form1.boundsrect.top - height else 
          if (m.WindowPos.y < poben) and (m.WindowPos.y > moben) then 
            m.WindowPos.y := Form1.boundsrect.top else 
            if (m.WindowPos.y + height > munten) and 
              (m.WindowPos.y + height < punten) then 
              m.WindowPos.y := Form1.boundsrect.bottom - height; 
    end; 
    if (top > moben - height) and (top < punten) then begin 
      if (m.WindowPos.x < prechts) and (m.WindowPos.x > mrechts) then 
        m.WindowPos.x := Form1.boundsrect.right else 
        if (m.WindowPos.x + width > mlinks) and 
          (m.WindowPos.x + width < plinks) then 
          m.WindowPos.x := Form1.boundsrect.left - width else 
          if (m.WindowPos.x < plinks) and (m.WindowPos.x > mlinks) then 
            m.WindowPos.x := Form1.boundsrect.left else 
            if (m.WindowPos.x + width > mrechts) and 
              (m.WindowPos.x + width < prechts) then 
              m.WindowPos.x := Form1.boundsrect.right - width; 
    end; 
  end; 
end; 


Zugriffe seit 6.9.2001 auf Delphi-Ecke