[ Pobierz całość w formacie PDF ]
.Copyright ©2001 SYBEX, Inc., Alameda, CA www.sybex.com2874c08.qxd 7/2/01 4:27 PM Page 306306 Chapter8 " Building the User InterfaceAnother feature of the program is that it handles the OnDockOver and OnDockDrop events ofa dock host panel to display messages to the user, such as the number of controls currentlydocked:procedure TForm1.Panel1DockDrop(Sender: TObject; Source: TDragDockObject;X, Y: Integer);beginCaption := Docked: + IntToStr (Panel1.DockClientCount);end;In the same way, the program also handles the main form s docking events.Another con-trol, a list box, has a shortcut menu you can invoke to perform docking and undocking opera-tions in code, without the usual mouse dragging:procedure TForm1.DocktoPanel1Click(Sender: TObject);begin// dock to the panelListBox1.ManualDock (Panel1, Panel1, alBottom);end;procedure TForm1.DocktoForm1Click(Sender: TObject);begin// dock to the current formListBox1.Dock (Self, Rect (200, 100, 100, 100));end;procedure TForm1.Floating1Click(Sender: TObject);begin// toggle the floating statusif ListBox1.Floating thenListBox1.ManualDock (Panel1, Panel1, alBottom)elseListBox1.ManualFloat (Rect (100, 100, 200, 300));Floating1.Checked := ListBox1.Floating;end;The final feature of the example is probably the most interesting one: Every time the pro-gram closes, it saves the current docking status of the panel, using the dock manager support.When the program is reopened, it reapplies the docking information, restoring the previousconfiguration of the windows.The program does this only with the panel, so the other float-ing windows will be displayed in their original positions.Here is the code for saving andloading:procedure TForm1.FormDestroy(Sender: TObject);varFileStr: TFileStream;beginCopyright ©2001 SYBEX, Inc., Alameda, CA www.sybex.com2874c08.qxd 7/2/01 4:27 PM Page 307Delphi s Docking Support 307if Panel1.DockClientCount > 0 thenbeginFileStr := TFileStream.Create (DockFileName, fmCreate or fmOpenWrite);tryPanel1.DockManager.SaveToStream (FileStr);finallyFileStr.Free;end;endelse// remove the fileDeleteFile (DockFileName);end;procedure TForm1.FormCreate(Sender: TObject);varFileStr: TFileStream;begin// reload the settingsDockFileName := ExtractFilePath (Application.Exename) + dock.dck ;if FileExists (DockFileName) thenbeginFileStr := TFileStream.Create (DockFileName, fmOpenRead);tryPanel1.DockManager.LoadFromStream (FileStr);finallyFileStr.Free;end;end;Panel1.DockManager.ResetBounds (True);end;There are more features one might theoretically add to a docking program, but to addthose you should remove other features, as some of them might conflict.For example, auto-matic alignments don t work terribly well with the docking manager s code for restoring.Isuggest you take this program and explore its behavior, extending it to support the type ofuser interface you prefer.NOTERemember that although docking panels make an application look nice, some users get con-fused by the fact that their toolbars might disappear or be in a different position than they areused to.Don t overuse the docking features, or some of your inexperienced users may get lost.Copyright ©2001 SYBEX, Inc., Alameda, CA www.sybex.com2874c08.qxd 7/2/01 4:27 PM Page 308308 Chapter8 " Building the User InterfaceDocking to a PageControlAnother interesting feature of page controls is the specific support for docking.As you docka new control over a PageControl, a new page is automatically added to host it, as you caneasily see in the Delphi environment.To accomplish this, you simply set the PageControl asa dock host and activate docking for the client controls.This works best when you have sec-ondary forms you want to host.Moreover, if you want to be able to move the entire Page-Control into a floating window and then dock it back, you ll need a docking panel in themain form
[ Pobierz całość w formacie PDF ]