源地址:http://edn.embarcadero.com/article/33446
摘要: Instructions on how to use the TDockTabSet component to make advanced docking user interfaces.
Introduction
This article discusses the use of the TDockTabSet component that was originally introduced in Delphi 2005.
Creating the applications main form
Create a File | New | VCL Forms Application - Delphi for Win32.
Setting up the main form
Locate the TDockTabSet component on the additional page in the Tool Palette and drop it onto the main form.
- Set the following properties:
Align | alLeft |
DockSite | False |
ShrinkToFit | True |
Style | tsModernTabs |
TabPosition | tpLeft |
Width | 25 |
You may want to use the tsModernPopup tab style instead of the tsModernTabs. The download that accompanies this article does.
To keep the TDockTabSet component company on the form, drop the following components and modify the properties as indicated.
- TPanel
Align | alTop |
BevelKind | bkTile |
BevelOuter | bvNone |
Caption |
- TPanel
Align | alLeft |
BevelOuter | bvNone |
DockSite | True |
Name | pDockLeft |
Width | 0 |
- TSplitter
Align | alLeft |
- TMemo
Align | Client |
- Drop a TButton on the top aligned Panel and set its caption to Show Form
- Set the Forms caption to TDockTabSetDemo Application
- Name the form frmMain and save the unit as MainForm
If you name your components differently remember to reference the correct name when adding the source code.
The main form of your application should look something like the following.
The setting of the all important property
There is one more property that needs to be set on the TDockTabSet before we can continue. Set the DestinationDockSite to be the left aligned panel. Called pDockLeft in this article.
Now it's time to create another form for the application. There are now 3 ways this can be done in Delphi 2006 and I'll mention all of them:
- Select the File | New | Form for Win32 or
- Click on the Project Manager
- In the Tool Palette you will see the object repository items get added.
- Locate the Delphi Projects | Delphi Files category.
- Double click on the Form icon to create a New Form.
- Right click on the project manager and select the Add New menu item. From the sub menu select Form.
Name the new form frmDock and save the unit as DockForm.
In the code editor for this unit do the following.
- Delete the global frmDock: TfrmDock variable from the forms implementation section, it isn't required
- Make sure the form isn't in the list of forms that are created automatically (I always turn this option off by default). To do this select the Project | Options menu item and move frmDock to the Available Forms list for the Forms option page.
- Add an OnClose event that contains the following code
procedure TfrmDock.FormClose(Sender: TObject; var Action: TCloseAction);beginManualFloat(Rect(0, 0, 0, 0));Action := caFree;end;
4.Add an OnStartDock event that contains the following code
procedure TfrmDock.FormStartDock(Sender: TObject; var DragObject:
TDragDockObject);
begin
DragObject := TDragDockObjectEx.Create(Self);
DragObject.Brush.Color := clAqua; // this will display a red outline
end;
class function TfrmDock.CreateDockForm(const aColor: TColor): TCustomForm;
begin
result := TfrmDock.Create(Application);
result.Color := aColor;
result.Caption := ColorToString(aColor);
result.Show;
end;
6.Finally modify the following properties on the dock form
BorderStyle | bsSizeToolWin |
DragKind | dkDock |
DragMode | dmAutomatic |
You may also want to modify the size of the frmDock form to not be so wide.
That is the form that will be docked completed. Time to write some in the main unit!
Switch to the MainForm unit now and make the following changes.
1.Invoke the Use Unit dialog (Alt+F11 or File | Use Unit) and select the DockForm unit.
2.Create an OnClick event for the TButton with the following code
procedure TfrmMain.Button1Click(Sender: TObject);vari: Integer;begin// close all previously dockable forms before recreatingfor i := 0 to Screen.FormCount - 1 doif Screen.Forms[i] is TfrmDock thenScreen.Forms[i].Close;// dock to the component called pDockLeftTfrmDock.CreateDockForm(clBlue).ManualDock(pDockLeft);// dock to the top on the pDockLeft panelTfrmDock.CreateDockForm(clGreen).ManualDock(pDockLeft, nil, alTop);// dock to the right on the pDockLeft panelTfrmDock.CreateDockForm(clRed).ManualDock(pDockLeft, nil, alRight);// dock directly to the DockTabSetTfrmDock.CreateDockForm(clWhite).ManualDock(DockTabSet1);end;
The remaining code is required to get the docking behavior to play nice amongst each other.
1.Create an OnDockDrop event for the pDockLeft panel and add the following code
This OnDockDrop event makes sure that the width of the pDockLeft panel is sufficient for when the frmDock form is dropped on it. If the panel previously had a width of 0 (which means you can't see it) then set the width to 150. This value is hardcoded for the demo but I could have easily read the current width of the control being dropped and using that value. You can get the width of the control being dropped from the TDragDockObject passed in as the Source parameter. You would use the following to get the width of the control that is being dropped:
pDockLeft.Width := Source.Control.Width;
The OnDockDrop code also makes sure that the splitter is visible when a form is being docked and that it is in the correct position.
procedure TfrmMain.pDockLeftDockDrop(Sender: TObject; Source: TDragDockObject; X, Y: Integer);
beginif pDockLeft.Width = 0 thenpDockLeft.Width := 150;Splitter1.Visible := True;Splitter1.Left := pDockLeft.Width;
end;
2.Create an OnUndock event for the pDockLeft panel and add the following code
If the form being undocked is the last one on it then we need to set the Panels width back to 0 and hide the Splitter.
procedure TfrmMain.pDockLeftUnDock(Sender: TObject; Client: TControl; NewTarget: TWinControl; var Allow: Boolean);
beginif pDockLeft.DockClientCount = 1 thenbeginpDockLeft.Width := 0;Splitter1.Visible := False;end;
end;
The DockOver event is the event responsible for drawing the forms outline at the dock site.
procedure TfrmMain.pDockLeftDockOver(Sender: TObject; Source: TDragDockObject; X, Y: Integer; State: TDragState;var Accept: Boolean);
varlRect: TRect;
beginAccept := Source.Control is TfrmDock;if Accept thenbeginlRect.TopLeft := pDockLeft.ClientToScreen(Point(0, 0));lRect.BottomRight := pDockLeft.ClientToScreen(Point(150, pDockLeft.Height));Source.DockRect := lRect;end;
end;
If a control is being docked to the DockTabSet component we want to make sure it is visible.
procedure TfrmMain.DockTabSet1DockDrop(Sender: TObject; Source: TDragDockObject; X, Y: Integer);
begin
DockTabSet1.Visible := True;
end;
The DockTabSet1 component should only be visible if there is a control docked to it.
procedure TfrmMain.DockTabSet1TabRemoved(Sender: TObject);
beginDockTabSet1.Visible := DockTabSet1.Tabs.Count > 0;
end;
More action shots
Application appearance on startup
Tab selected. Note you can click on the pin button to pin the green form to the docksite or click on the cross to close the form. To not do anything, click in the memo to change the focus away from the green form to hide it.
Dragging an undocked form, note the red drag outline
Hide image
All windows unpinned
What about VCL.NET support?
Unfortunately there is a bug in the VCL.NET implementation of the TDockTabSet component. It is currently logged in QualityCentral and hopefully it will be addressed in the next major Delphi release.
Report No: 24640 ( RAID: 238759 ) Status: Open
TDockTabSet control doesn't work correctly in VCL.NET
http://qc.borland.com/wc/qcmain.aspx?d=24640
The bug cannot be fixed in an update as it requires an interface change to the TDockTabSet class. If you wish to fix the bug yourself you can use the following workaround:
Move the DoAddDockClient method from strict private to protected and override it.
previously...
TDockTabSet = class(TTabSet)should become...
strict private
procedure DoAddDockClient(Client: TControl; const ARect: TRect);
TDockTabSet = class(TTabSet)
protected
procedure DoAddDockClient(Client: TControl; const ARect: TRect); override;
You also have to take the necessary steps to ensure that the modified unit is compiled into your assembly as well.
An alternate approach might be:
- Create a new Delphi.NET package
- Save the package and name it FixedDockTabSetNET100
- Open the Borland.VCL.TDockTabSet unit from the sourceDotNetVCL folder
- Select File | Save As... and save a copy of the unit in the folder where you saved the package and rename the package to FixedDockTabSet
- In the FixedDockTabSet unit right click on the TDockTabSet class name and select Rename refactoring. Rename the class to TFixedDockTabSet
- Create a global function called Register and implement it as follows:
procedure Register;Build the new package Install the package using the Installed .NET Components menu item from the components menu, making sure to add it to the VCL.NET components tab. If all worked well when you create a new VCL.NET Application you will get a Fixed DockTabSet palette page that has the new TDockTabSet control on it.
begin
RegisterComponents('Fixed DockTabSet', [TFixedDockTabSet]);
end;
Closing comments
With the help of the TDockTabSet component you can now create more advanced user interfaces with dockable windows which is sure to annoy most users. With a lot more hair pulling and sleepless nights, you can use the code in this article as a basis to create your our docking framework. I've done this as part for my QualityCentral windows client I created.
I encourage you to download and try it out from my website www.jed-software.com.
Hide image
Hide image
While doing so, don't forget to add any issues or enhancements to QualityCentral.
Source Download
You can download the source to this article from Code Central:
Download Source Code