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
tmpformvariable, 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.DockSiteis set toTrue(we do this in the constructor, but you can also set it in the Object Inspector at design time). - TColorForm: Your
TColorFormdoesn't need any special docking properties, but make sure it's a standard TForm descendant. - Horizontal Arrangement: Using
alRightas 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 thelLastDockedparameter and usealLeftinstead.
How This Works
- We use
TListto track all docked forms so we can clean them up properly whenForm1is destroyed. ManualDockis used to programmatically dock each new form toPanel1, specifying exactly where it should be placed relative to existing docked forms.- Setting
DragKindandDragModeensures the forms can be repositioned manually if needed (you can disable this by settingDragModetodmManualif you don't want users to adjust docking positions).
内容的提问来源于stack exchange,提问作者Steve88
相关产品推荐
相关产品推荐

