Mirror

Resize a TForm in steps (Views: 300)


Problem/Question/Abstract:

How can I resize a form in steps, not smooth, like the Object Inspector in Delphi?

Answer:

First, declare a message handler for the WM_SIZING message in your form:

TForm1 = class(TForm)
  { ... }
protected
  procedure WmSizing(var Msg: TMessage); message WM_SIZING;
  { ... }
end;

Then define the WmSizing message handler like so:

procedure TForm1.WmSizing(var Msg: TMessage);
const
  STEP = 20;
var
  Side: LongInt;
  Rect: PRect;
begin
  inherited;
  Side := Msg.WParam;
  Rect := PRect(Msg.LParam);
  if (WMSZ_BOTTOM and Side = WMSZ_BOTTOM) then
    Rect^.Bottom := Rect^.Bottom - (Rect^.Bottom mod STEP);
  if (WMSZ_BOTTOMLEFT and Side = WMSZ_BOTTOMLEFT) then
  begin
    Rect^.Bottom := Rect^.Bottom - (Rect^.Bottom mod STEP);
    Rect^.Left := Rect^.Left - (Rect^.Left mod STEP);
  end;
  if (WMSZ_BOTTOMRIGHT and Side = WMSZ_BOTTOMRIGHT) then
  begin
    Rect^.Bottom := Rect^.Bottom - (Rect^.Bottom mod STEP);
    Rect^.Right := Rect^.Right - (Rect^.Right mod STEP);
  end;
  if (WMSZ_LEFT and Side = WMSZ_LEFT) then
    Rect^.Left := Rect^.Left - (Rect^.Left mod STEP);
  if (WMSZ_RIGHT and Side = WMSZ_RIGHT) then
    Rect^.Right := Rect^.Right - (Rect^.Right mod STEP);
  if (WMSZ_TOP and Side = WMSZ_TOP) then
    Rect^.Top := Rect^.Top - (Rect^.Top mod STEP);
  if (WMSZ_TOPLEFT and Side = WMSZ_TOPLEFT) then
  begin
    Rect^.Top := Rect^.Top - (Rect^.Top mod STEP);
    Rect^.Left := Rect^.Left - (Rect^.Left mod STEP);
  end;
  if (WMSZ_TOPRIGHT and Side = WMSZ_TOPRIGHT) then
  begin
    Rect^.Top := Rect^.Top - (Rect^.Top mod STEP);
    Rect^.Right := Rect^.Right - (Rect^.Right mod STEP);
  end;
  Msg.Result := 1;
end;

You can adjust the STEP constant to meet your needs. Likewise, you can make any edge a constant size simply by assigning a valueto it.

<< Back to main page