Mirror

How to limit MDI child form movement to the client area of the MDI parent form (Views: 100)


Problem/Question/Abstract:

Is it possible to limit the MDI client form movement, so that the form cannot be moved outside the client area of the MDI form?

Answer:

Yes, you can handle the WM_WINDOWPOSCHANGING message in the child forms and modify the message parameters if needs be to keep the child fully visible. Of course this is a breach of the standard Windows behaviour.

private {in form declaration}

procedure WMWINDOWPOSCHANGING(var msg: TWMWINDOWPOSCHANGING);
  message WM_WINDOWPOSCHANGING;

procedure TForm1.WMWINDOWPOSCHANGING(var msg: TWMWINDOWPOSCHANGING);
var
  r: TRect;
begin
  with msg.Windowpos^ do
  begin
    if (flags and SWP_NOMOVE) = 0 then
    begin
      r := GetClientrect(Application.Mainform.handle, r);
      if x < 0 then
        x := 0;
      if y < 0 then
        y := 0;
      if (x + cx) > r.right then
        x := r.right - cx;
      if (y + cy) > r.bottom then
        y := r.bottom - cy;
    end;
    inherited;
  end;
end;

<< Back to main page