在Delphi7中实现停靠功能
我们在使用Delphi7开发应用系统过程中经常需要使用子窗口在主窗口上停靠的功能,如果对这一部分不熟练时,通常会到CSDN等网站寻找各种相关的控件,或者参考Delphi自带的例程Docking,下面我给大家介绍一种能够凑乎用的简便方法。
1、在主窗口中添加四个Panel和四个Splitter,设置对齐上下左右四边。
2、设定四个Panel的属性DockSite属性为True。
3、左右Panel添加 OnDockDrop、OnDockOver、OnUnDock事件如下:
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;
2、设定四个Panel的属性DockSite属性为True。
3、左右Panel添加 OnDockDrop、OnDockOver、OnUnDock事件如下:
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;
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;
var Accept: 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;
左右Panel响应事件代码相同。
Source: TDragDockObject; X, Y: Integer; State: TDragState;
var Accept: 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;
左右Panel响应事件代码相同。
4、上下Panel添加 OnDockDrop、OnDockOver、OnUnDock事件如下:
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.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;
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;
var Accept: 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;
上下两个Panel响应事件代码相同。
Source: TDragDockObject; X, Y: Integer; State: TDragState;
var Accept: 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;
上下两个Panel响应事件代码相同。
5、创建新的窗体用于停靠到主窗体。
6、设置新窗体DragKind为dkDock,DragMode为dmAutomatic。
7、在新窗体OnClose事件中添加如下代码:
if self.HostDockSite <> nil then
begin
self.ManualDock(nil);
end;
Action := caHide;
8、设置新窗体不自动创建。
9、在主创体中创建新窗体并显示。记住:用Show,不要用ShowModal。
6、设置新窗体DragKind为dkDock,DragMode为dmAutomatic。
7、在新窗体OnClose事件中添加如下代码:
if self.HostDockSite <> nil then
begin
self.ManualDock(nil);
end;
Action := caHide;
8、设置新窗体不自动创建。
9、在主创体中创建新窗体并显示。记住:用Show,不要用ShowModal。
这种方法是一个不很规范的方法,如果需要更细致的控制,最好参考Delphi的Docking示例。如果将此例中的某个Panel更改为TabControl或者PageConrol,你可以得到更好的效果,不过代码需要稍微变化,有兴趣的哥们可以试试。