2004. június 7., hétfő

How to resize open forms automatically when the Desktop size changes


Problem/Question/Abstract:

I have created a form which is snapped to the top of the screen. When I change the size of the Windows Desktop, the visible form isn't resized accordingly.

Answer:

I use the following code and it works quite good, even if the desktop size is changed:

type
  TForm1 = class(TForm)
  private
    procedure WMWindowPosChanging(var Message: TWMWindowPosMsg);
      message WM_WINDOWPOSCHANGING;

    { ... }

implementation

{ ... }

var
  WorkAreaRect: TRect;
  NewWorkAreaFlag: Boolean;

  { ... }

procedure TForm1.WMWindowPosChanging(var Message: TWMWindowPosMsg);
begin
  if (WindowState <> wsMaximized) then
  begin
    if (Message.WindowPos.Flags and SWP_NOMOVE) = 0 then
    begin {form is moved}
      if NewWorkAreaFlag then
      begin {workarea might have changed since last call}
        SystemParametersInfo(SPI_GETWORKAREA, 0, @WorkAreaRect, 0);
      end;
      {snap form to border here, something
                        like Message.WindowPos.X := WorkAreaRect.Left if near to left border}
      { ... }
    end;
    NewWorkAreaFlag := ((Message.WindowPos.Flags and (SWP_NOMOVE or SWP_NOSIZE)) <>
      0);
    {True if form was (probably) dropped => get workarea again}
  end;
end;

{ ... }

initialization
  NewWorkAreaFlag := True;

Nincsenek megjegyzések:

Megjegyzés küldése