Implementation of docking technology in Delphi:
With the continuous advancement of software technology, the software interface is becoming more and more beautiful and the operation is becoming more and more convenient.
Looking at various professional software on the market, we will find that most of them provide the form docking function, especially tool software, which basically has more or less docking function.
Naturally, Delphi also supports docking, and it is closely integrated with VCL, which is a great boon for the majority of Delphi programmers. Let's save the boring coding time. Focus on the conception of the core program.
Let us first review the structure of VCL. There is a DockSite property (boolean) in the TWinControl class. Its function is to allow other controls to be docked on it. There is a DragKind property in the TControl class. If you want this control To be able to dock on other controls, set the DragKind property to dkDock. It's that simple, just set the properties, and a program that supports docking is completed.
Of course, the above are only the most basic steps. With the above two steps, we can continue to write code to implement more complex functions.
Generally, programs that support docking can be docked on the top, bottom, left, and right of the main window. That is to say, it is better to put controls that can be docked on the side of the main window (as long as they are inherited from TWinControl), generally we choose TPanel , in order to facilitate readers' understanding, we can assume that the left side of the main window can be docked, so put an Align attribute on the main window with a lLeft's Panel is named LeftDockPanel, the width is 0, and the DockSite attribute is True. Of course, our LeftDockPanel should be able to change the size, so put a TSplitter on the right side of it, named LeftSplitter, and the Align attribute is alLeft. Next is the docking control. Generally, program docking controls are forms, so we also build a form and name it DockableForm. The DragKind property is set to dkDock and the DragMode property is set to dmAutomatic (automatic docking).
Now we can run this program, what? Not effective? The docked form disappears after being docked!
Oh, I almost forgot, Delphi will generate some events when the docked form is docked, they are
1. OnDockOver(Sender: TObject; Source: TDragDockObject;
X, Y: Integer; State: TDragState; var Accept: Boolean);
2. OnDockDrop(Sender: TObject; Source: TDragDockObject;
X, Y: Integer);
3. OnGetSiteInfo(Sender: TObject; DockClient: TControl;
var InfluenceRect: TRect; MousePos: TPoint; var CanDock: Boolean);
4. OnStartDock(Sender: TObject;
var DragObject: TDragDockObject);
5. OnEndDock(Sender, Target: TObject; X, Y: Integer);
6. OnUnDock(Sender: TObject; Client: TControl;
NewTarget: TWinControl; var Allow: Boolean);
Wow, so many, don’t worry, let me explain in detail:
Let's take a look at the first event first
OnDockOver is triggered when the docked control (DockableForm) passes the docked control (LeftDockPanel). Source contains information about docking and dragging operations. One important attribute is Control, which is DockableForm. Another important attribute is DockRect, which is the docking position; X and Y are the positions of the mouse. The state of State is dsDragEnter, dsDragLeave, dsDragMove, respectively means dragging in, dragging away, and dragging to move; Accept means whether to agree to the docking. The main function of the OnDockOver event is to control the preview position of the docked form. Let's add the following code: