转:ExpressBars中的停靠控件使用

http://www.cnblogs.com/jxsoft/archive/2011/08/25/2152872.html

1          新手上路

1.1      控件简介

Dock panels

  Dock panels是建立停靠应用的最主要部分,是在应用内部的windows局部窗口,可以放置其他的VCL控件。当一个Dock panels放在Form上后,它会自动漂浮在Form之上。如果需要停靠到窗体中,需要使用TdxDockSite控件,并且可以设置主题风格。

  注意,当要对Dock panels控件进行删除、复制粘贴或者其他一些操作时,务必使用控件的上下文菜单来实现。

  Dock panels之间可以互相停靠,停靠的结果有2种类型-tab containers 和side containers,例如,可以将side containers放到一个tab containers中。

Dock Sites

  Dock Sites支持放置Dock panels,注意Dock Sites只能接受Dock panels控件;

  如果没有Dock Sites上没有控件时,会有一个空白的矩形区域占位,如果不想这样,可以采用以下技巧:

l       用一个Dock Sites填充整个Form,然后在上面放上一个Dock panels;

l       如果将DockSites的autoSize属性设为true,它的大小会自适用所接受的Dock Panel或者container;

Docking Manager

  Docking Manager控件可以集中控制所有或者部分的停靠控件。注意,它不是必须的,如果在Application的任何窗体上都没有放置Docking Manager,docking controller会接管停靠控件的管理。但是docking controller只提供默认设置,不允许改变停靠控件的设置。一般情况下都会使用Docking Manager。

1.2      如何定制Dock Panels外观

ExpressDocking提供4种预先设置好显示风格,标准(类似于Delphi)、Visual Studio .Net、office和Windows Xp风格。为了在设计时就能指定风格,需要使用Docking Manager的ViewStyle和LookAndFeel属性。注意LookAndFeel属性设置只能在ViewStyle属性设置为vsUseLookAndFeel时使用。

要指定一个dock panel的caption,使用Caption属性,设置图标使用ImageIndex属性,当然之前要给docking manager提供一个imageList。

1.3      停靠的基本操作

  如果要改变dock panel的位置,但是不想在经过dock site上方的时候自动嵌入,可以在拖动的时候按住CTRL。

  停靠控件的AllowDockClients属性为接受的控件停靠设定位置,AllowDock为停靠到其他控件设定位置。设置AllowFloating为False,可以禁止停靠控件漂浮,Dockable设置为False,则不允许拖动停靠。

1.4      创建一个Visual Studio .Net风格的界面

l       新建一个应用;

l       在窗体上放置一个TdxDockSite控件,并设置Align属性为alClient;

l       在TdxDockSite上放置一个TdxDockPanel控件,TdxDockPanel会自动充满整个TdxDockSite的区域。设置ShowCaption属性为False,隐藏面板标题栏;

l       在TdxDockPanel上放置一个memo控件,并设置Align为alClient;

l       设置dock panel的AllowDockClients属性,将所有的选项为False,阻止其他dock panel的停靠;

l       将dock site的AllowDockClients属性的dtTop选项设为False,将会阻止其他dock panel停靠到其上方;

l       至此,所有准备步骤已经就绪。现在可以在窗体上新建一些dock panel,这些dock panel只能被拖放到窗体的边缘,除了上方。当一个dock panel停靠到窗体的左边,设置AutoHide属性,将会自动加入一个side container控件;

l       剩下的事情就要切入主题了,放置一个docking manager控件,设置ViewStyle属性为vsNET,整个应用的界面就会变成VS .NET风格了。

 

2          运行时的停靠控件

2.1      停靠规则

  Dock panel 只能在dock site上该能改变大小。ExpressDocking提供2种停靠点类型:dock sites、layout sites和float sites。Dock sites 是放置在窗体上,用来接受dock panel的停靠;Layout sites是当有停靠操作时,自动创建在dock site上,以提供更灵活的布局定制;Float sites是漂浮dock panel的载体;

  当dock panel处于漂浮状态,会自动创建Float sites,一旦dock panel停靠,Float site就会被销毁;

  Layout sites 也是自动创建的,在dock panel停靠后发生。Layout site不会占据空间;

2.2      在运行时控制dock panels的停靠

  Dock panel 提供DockTo, MakeFloating和UnDock方法来执行停靠操作。

  MakeFloating是使dock panel离开Dock site而处于漂浮状态;

  UnDock类似于MakFloating,但是会隐藏Dock panel。这个方法很有用处,例如当一个dock panel停靠到另外一个dock panel形成side container,你需要知道目标dock panel是不是在一个tab container中,如果之前所有的dock panel都被UnDock了,那么你就不必担心担心了。

  例如将dock panel停靠到左边:

dxDockPanel1.DockTo(dxDockSite1, dtLeft, 0);

  例如将一个dock panel充满窗体的下方,另外一个dock panel放置未充满区域的右侧:

dxDockPanel2.DockTo(dxDockSite1, dtBottom, 0); //此步会自动创建一个LayoutDockSite来放置dxDockPanel2;

dxDockPanel3.DockTo(dxDockPanel2.LayoutDockSite, dtRight, 0); //在dxDockPanel2的LayoutDockSite上来布局新dock panel;

docking manager提供一些方法来存储和加载布局,这些方法是SaveLayoutToIntFile, SaveLayoutToRegistry, SaveLayoutToStream, LoadLayoutFromIntFile,LoadLayoutFromRegistry, LoadLayoutFromStream

2.3      创建和管理Container Control

l       调用停靠控件的DockTo方法就会自动一个创建容器;当UnDock发生后,会自动销毁容器空间;

l       调用停靠控件的MakeFloating方法会自动销毁容器控件;

l       调用容器的ActiveChildIndex来查找当前活动dock panel;

l       Dock Panel有个DockIndex属性,决定了其在容器中的索引;

l       容器有Children,ChildCount, ValidChildren和ValidChildCount属性,可以用来访问容器的成员dock panel;

l       Dock panel的ParentDockControl, TabContainer和SideContainer属性可以用来访问父停靠控件;

  例如下面的代码演示:

 
// Delphi

var

ATabContainer: TdxTabContainerDockSite;

ASideContainer: TdxSideContainerDockSite;

begin

// create a side container by docking the second panel

// to the center of the first one

dxDockPanel2.DockTo(dxDockPanel1, dtClient, 1);



// obtaining the tab container created

ATabContainer := dxDockPanel1.TabContainer;

if ATabContainer = nil then Exit;

// adding the third panel to the tab container

dxDockPanel3.DockTo(ATabContainer, dtClient, 2);

// switching to the first panel (tab)

ATabContainer.ActiveChildIndex := dxDockPanel1.DockIndex;

// create a vertical side container

dxDockPanel4.DockTo(dxDockPanel5, dtTop, 0);



// obtain the side container created

ASideContainer := dxDockPanel4.SideContainer;

if ASideContainer = nil then Exit;

// expand the top panel within the side container

ASideContainer.ActiveChildIndex := dxDockPanel4.DockIndex;

// dock the side container to a site's right edge

ASideContainer.DockTo(dxDockSite1, dtRight, 0);

// dock the tab container to a site's bottom edge

ATabContainer.DockTo(dxDockSite1, dtBottom, 0);



end;
 

2.4      在运行时动态创建停靠控件

  停靠控件依靠Visible属性来设置是否显示。当调用控件的Close方法也会隐藏控件,但要记住,需要将docking manager的doFreeOnClose选项禁用,否则,Close方法会销毁控件;另外一个隐藏控件的方法是使用UnDock。调用Free也是销毁控件。

l       不必手工创建容器控件,因为他们是伴随停靠操作自动创建和销毁的;

l       如果一个控件隐藏,容器还会为它保留位置,当再次显示时,还会出现在同样的位置;

l       如果开启自动隐藏特性,再设置Visible属性为False会无效;因此在隐藏这类控件前,最好先取消自动隐藏特性;

停靠控件提供一些事件来响应这些操作:VisibleChanged, VisibleChaning, OnClose, OnCloseQuery;

l       显示和隐藏停靠控件的示例

 
  procedure TForm1. Button1Click(Sender: TObject);

begin

if dxDockPanel1.AutoHide and not dxDockPanel1.Visible then

begin

dxDockingController.BeginUpdate;

try

// make the panel's client area visible on screen

dxDockPanel1.Visible := True;

// hide the panel

dxDockPanel1.Visible := False;

finally

dxDockingController.EndUpdate;

end;

end

else

dxDockPanel1.Visible := not dxDockPanel1.Visible;

end;
 

  

l       动态创建停靠控件

 
  procedure TForm1.FormCreate(Sender: TObject);

begin

// specify the open dialog抯 filter

OpenDialog1.Filter := '*.bmp|*.bmp';

with dxDockingManager1 do

begin

// force dock controls to be destroyed when closed

Options := dxDockingManager1.Options + [doFreeOnClose];

// specify the default tabs position and

// the dock controls paint style

DefaultTabContainerSiteProperties.TabsPosition := tctpTop;



ViewStyle := vsNET;

end;

end;



procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;

Shift: TShiftState);

var

APicture: TPicture;

APanel: TdxDockPanel;

AImage: TImage;

ALastIndex, I: Integer;

begin

if Key = VK_INSERT then

begin

// open the file and create the corresponding image object



if not OpenDialog1.Execute() then Exit;

APicture := TPicture.Create();

APicture.LoadFromFile(OpenDialog1.FileName);

// create a new panel that will hold the image

APanel := TdxDockPanel.Create(Self);

APanel.Caption := ExtractFileName(OpenDialog1.FileName);

// check whether the dock site already has docked controls

if dxDockSite1.ChildCount > 0 then

begin

// add the panel to the tab container



ALastIndex := dxDockSite1.Children[1].ChildCount;

APanel.DockTo(dxDockSite1.Children[1], dtClient, ALastIndex + 1);

end

else

// dock the panel to the dock site

APanel.DockTo(dxDockSite1, dtClient, 0);

// create an image control and place it on the panel

AImage := TImage.Create(Self);

AImage.Parent := APanel;

AImage.Align := alClient;

AImage.Center := True;

AImage.Picture := APicture;



end;

// delete all panels if the DELETE key has been pressed

if Key = VK_DELETE then

begin

for I := 0 to dxDockingController.DockControlCount - 1 do

begin

if dxDockingController.DockControls[I] is TdxDockPanel then

dxDockingController.DockControls[I].Close;

end;

end;

end;
 

  

原文地址:https://www.cnblogs.com/rogge7/p/4648841.html