2009. november 22., vasárnap

Snap a form to another one and move both around


Problem/Question/Abstract:

How do I get forms been redrawn while moving them? I need a form that snaps magnetically to another while moved, but there is no event! I've tried the message WM_WINDOWPOSCHANGING, but it's not possible to show when it fires, because the form is not redrawn, when moved.

Answer:

Note that MOPSChildForm is the "master" and SearchForm follows it around.

procedure TMOPSChildForm.WMWindowPosChanging(var Message: TWMWindowPosChanging);
var
  Moving: Boolean;
begin
  if SearchForm <> nil then
  begin
    with Message.WindowPos^ do
      Moving := (ComponentState * [csReading, csDestroying] = []) and (flags and
        SWP_NOSIZE = 0)
        and ((x <> Left) or (y <> Top));
    inherited;
    if Moving then
      SearchForm.MoveWithForm(HostDockSite <> nil)
  end;
end;

procedure TMOPSSearch.MoveWithForm(Docked: Boolean);
const
  DeltaX = 20; {Offset of this form from MOPSChildForm's TopLeft}
  DeltaY = 40;
begin
  if Docked then
    with TForm(TForm(TForm(Owner).HostDockSite).Owner) do
    begin
      Self.Left := Left + DeltaX;
      Self.Top := Top + DeltaY;
    end
  else
    with TForm(Owner) do
    begin
      Self.Left := Left + DeltaX;
      Self.Top := Top + DeltaY;
    end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése