Author: Jonas Bilinkevicius
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:
1 TForm1 = class(TForm)
2 { ... }3 protected4 procedure WmSizing(var Msg: TMessage); message WM_SIZING;
5 { ... }6 end;
7 8 //Then define the WmSizing message handler like so:9 10 procedure TForm1.WmSizing(var Msg: TMessage);
11 const12 STEP = 20;
13 var14 Side: LongInt;
15 Rect: PRect;
16 begin17 inherited;
18 Side := Msg.WParam;
19 Rect := PRect(Msg.LParam);
20 if (WMSZ_BOTTOM and Side = WMSZ_BOTTOM) then21 Rect^.Bottom := Rect^.Bottom - (Rect^.Bottom mod STEP);
22 if (WMSZ_BOTTOMLEFT and Side = WMSZ_BOTTOMLEFT) then23 begin24 Rect^.Bottom := Rect^.Bottom - (Rect^.Bottom mod STEP);
25 Rect^.Left := Rect^.Left - (Rect^.Left mod STEP);
26 end;
27 if (WMSZ_BOTTOMRIGHT and Side = WMSZ_BOTTOMRIGHT) then28 begin29 Rect^.Bottom := Rect^.Bottom - (Rect^.Bottom mod STEP);
30 Rect^.Right := Rect^.Right - (Rect^.Right mod STEP);
31 end;
32 if (WMSZ_LEFT and Side = WMSZ_LEFT) then33 Rect^.Left := Rect^.Left - (Rect^.Left mod STEP);
34 if (WMSZ_RIGHT and Side = WMSZ_RIGHT) then35 Rect^.Right := Rect^.Right - (Rect^.Right mod STEP);
36 if (WMSZ_TOP and Side = WMSZ_TOP) then37 Rect^.Top := Rect^.Top - (Rect^.Top mod STEP);
38 if (WMSZ_TOPLEFT and Side = WMSZ_TOPLEFT) then39 begin40 Rect^.Top := Rect^.Top - (Rect^.Top mod STEP);
41 Rect^.Left := Rect^.Left - (Rect^.Left mod STEP);
42 end;
43 if (WMSZ_TOPRIGHT and Side = WMSZ_TOPRIGHT) then44 begin45 Rect^.Top := Rect^.Top - (Rect^.Top mod STEP);
46 Rect^.Right := Rect^.Right - (Rect^.Right mod STEP);
47 end;
48 Msg.Result := 1;
49 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.