You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Delphi窗体水平多次停靠实现遇阻,寻求技术帮助

Solution for Multiple Horizontal Form Docking in Delphi

It looks like your code is missing key pieces to enable multiple forms to dock horizontally into Panel1. Let's break down the issues and fix this step by step:

Key Issues in Your Current Code

  • You're using a single tmpform variable, which can only track one docked form—this won't work for multiple instances.
  • You haven't configured the necessary docking properties for either the new forms or the target panel.
  • You're not programmatically docking the forms to the panel (relying on manual drag-and-drop instead of automatic docking on button click).

Complete Corrected Implementation

First, update your form class to track all docked forms and handle cleanup properly:

type
  TForm1 = class(TForm)
    Button1: TButton;
    Panel1: TPanel;
    procedure Button1Click(Sender: TObject);
  private
    FDockedForms: TList; // Tracks all created docked forms
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

constructor TForm1.Create(AOwner: TComponent);
begin
  inherited;
  FDockedForms := TList.Create;
  // Ensure the panel is set up as a dock site
  Panel1.DockSite := True;
end;

destructor TForm1.Destroy;
var
  I: Integer;
begin
  // Clean up all docked forms to avoid memory leaks
  for I := FDockedForms.Count - 1 downto 0 do
    TForm(FDockedForms[I]).Free;
  FDockedForms.Free;
  inherited;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  lForm: TColorForm;
  lLastDocked: TControl;
begin
  lForm := TColorForm.Create(Application);
  try
    // Set basic form properties
    lForm.Caption := 'Docked Form ' + IntToStr(FDockedForms.Count + 1);
    lForm.Width := 200; // Initial width for horizontal docking
    lForm.Height := Panel1.ClientHeight; // Match panel's height

    // Enable docking capabilities on the new form
    lForm.DragKind := dkDock;
    lForm.DragMode := dmAutomatic;
    lForm.BorderStyle := bsSizeable; // Allows resizing via splitters

    // Find the last docked form to position the new one next to
    lLastDocked := nil;
    if FDockedForms.Count > 0 then
      lLastDocked := TForm(FDockedForms[FDockedForms.Count - 1]);

    // Dock the new form horizontally to the right of the last one
    lForm.ManualDock(Panel1, lLastDocked, alRight);

    // Show the form (it will appear docked immediately)
    lForm.Show;

    // Add to our tracking list for cleanup
    FDockedForms.Add(lForm);
  except
    lForm.Free;
    raise;
  end;
end;

Additional Setup Notes

  • Panel Configuration: Ensure Panel1.DockSite is set to True (we do this in the constructor, but you can also set it in the Object Inspector at design time).
  • TColorForm: Your TColorForm doesn't need any special docking properties, but make sure it's a standard TForm descendant.
  • Horizontal Arrangement: Using alRight as the docking position ensures each new form is placed to the right of the previous one, creating a horizontal row. If you want to stack them left-to-right starting from the panel's left edge, you can omit the lLastDocked parameter and use alLeft instead.

How This Works

  • We use TList to track all docked forms so we can clean them up properly when Form1 is destroyed.
  • ManualDock is used to programmatically dock each new form to Panel1, specifying exactly where it should be placed relative to existing docked forms.
  • Setting DragKind and DragMode ensures the forms can be repositioned manually if needed (you can disable this by setting DragMode to dmManual if you don't want users to adjust docking positions).

内容的提问来源于stack exchange,提问作者Steve88

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:51:35