Implementing the docking function in Delphi7 When we use Delphi7 to develop application systems, we often need to use the function of docking sub-windows on the main window. If you are not familiar with this part, you usually go to CSDN and other websites to find various related controls, or Referring to the Docking routine that comes with Delphi, I will introduce to you a simple method that can be used. 1. Add four Panels and four Splitter to the main window, and set the alignment to the top, bottom, left, and right sides.
2. Set the DockSite attribute of the four Panels to True.
3. Add OnDockDrop, OnDockOver, and OnUnDock events to the left and right Panels as follows:
PRocedure TfrmMain.pnlLeftUnDock(Sender: TObject; Client: TControl;
NewTarget: TWinControl; var Allow: Boolean);
begin
if (Sender as TPanel).VisibleDockClientCount = 1 then
begin
(Sender as TPanel).Width := 1;
end ;
end ; procedure TfrmMain.pnlLeftDockDrop(Sender: TObject;
Source: TDragDockObject; X, Y: Integer);
begin
(Sender as TPanel).Width := max(source.Control.UndockWidth,(Sender as TPanel).Width);
end ; procedure TfrmMain.pnlLeftDockOver(Sender: TObject;
Source: TDragDockObject; X, Y: Integer; State: TDragState;
varAccept: Boolean);
begin
if State = dsDragEnter then
begin
(Sender as TPanel).Width := max(Source.Control.UndockWidth, (Sender as TPanel).Width);
end
else
begin
if State = dsDragLeave then
begin
(Sender as TPanel).Width := 1;
end;
end;
end;
The left and right Panels respond to event codes with the same code. 4. Add OnDockDrop, OnDockOver, and OnUnDock events to the upper and lower panels as follows:
procedure TfrmMain.pnlBottomUnDock(Sender: TObject; Client: TControl;
NewTarget: TWinControl; var Allow: Boolean);
begin
if (Sender as TPanel).DockClientCount = 1 then
begin
(Sender as TPanel).Height := 1;
end;
end; procedure TfrmMain.pnlBottomDockDrop(Sender: TObject;
Source: TDragDockObject; X, Y: Integer);
begin
(Sender as TPanel).Height := max(source.Control.UndockHeight,(Sender as TPanel).Height);
end ; procedure TfrmMain.pnlBottomDockOver(Sender: TObject;
Source: TDragDockObject; X, Y: Integer; State: TDragState;
varAccept: Boolean);
begin
if State = dsDragEnter then
begin
(Sender as TPanel).Height := max(Source.Control.UndockHeight, (Sender as TPanel).Height);
end
else
begin
if State = dsDragLeave then
begin
(Sender as TPanel).Height := 1;
end;
end;
end;
The upper and lower Panels respond to event codes with the same code. 5. Create a new form for docking to the main form.
6. Set the new form DragKind to dkDock and DragMode to dmAutomatic.
7. Add the following code to the OnClose event of the new form:
if self.HostDockSite <> nil then
begin
self.ManualDock(nil);
end ;
Action := caHide;
8. Set new forms not to be automatically created.
9. Create a new form in the main creative body and display it. Remember: use Show, not ShowModal. This method is not very standardized. If you need more detailed control, it is best to refer to Delphi's Docking example. If you change a Panel in this example to TabControl or PageConrol, you can get better results, but the code needs to be slightly changed. Interested friends can try it.