Q:

Dynamically resize controls based on drag area

I have a form with a TTreeview on the left and a TGroupbox on the right. When the form is resized by dragging on the right edge, I want the Groupbox to grow or shrink accordingly. When the form is dragged on the left I want the Treeview to resize. I tried the code below and right resizing works fine. But left resizing causes the treeview to grow longer than it should. What would be the correct way to accomplish this?
procedure TForm1.FormResize(Sender: TObject);
var
  p: TPoint;
begin
  P:= ScreenToClient(Mouse.CursorPos);
  if(P.X<0) then begin
    Treeview1.Anchors:= [akLeft,akTop,akRight,akBottom];
    Groupbox1.Anchors:= [akTop,akBottom];
  end
  else begin
    Treeview1.Anchors:= [akLeft,akTop,akBottom];
    Groupbox1.Anchors:= [akLeft,akTop,akRight,akBottom];
  end;
end;

A:

Rather than handling the OnResize event, you can instead intercept the WM_SIZING message instead

Sent to a window that the user is resizing. By processing this message, an application can monitor the size and position of the drag rectangle and, if needed, change its size or position.

Its wParam value tells you exactly which edge(s) of the window are being resized.  You can use that to adjust the Anchors accordingly, eg:
type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    GroupBox1: TGroupBox;
  private
    { Private declarations }
    procedure WMSizing(var Message: TMessage); message WM_SIZING;
  public
    { Public declarations }
  end;

...

procedure TForm1.WMSizing(var Message: TMessage);
var
  TreeViewAnchors: TAnchors;
  GroupBoxAnchors: TAnchors;
begin
  inherited;

  TreeViewAnchors := [akTop, akBottom];
  GroupBoxAnchors := [akTop, akBottom];

  case Message.WParam of
    WMSZ_LEFT, WMSZ_BOTTOMLEFT, WMSZ_TOPLEFT: begin
      TreeViewAnchors := TreeViewAnchors + [akLeft, akRight];
      GroupBoxAnchors := GroupBoxAnchors + [akRight];
    end;
    WMSZ_RIGHT, WMSZ_BOTTOMRIGHT, WMSZ_TOPRIGHT: begin
      TreeViewAnchors := TreeViewAnchors + [akLeft];
      GroupBoxAnchors := GroupBoxAnchors + [akLeft, akRight];
    end;
  end;

  TreeView1.Anchors := TreeViewAnchors;
  GroupBox1.Anchors := GroupBoxAnchors;
end;

