unit Unit9; //主窗体
{
本单元目的: 演示如何使用CnPack停靠组件完成窗体停靠
说明: 只保存并显示那些停靠窗体, 如果子窗体没有停靠,那么就不会保存
所谓停靠包括停靠在主窗体上,也包括互相停靠子窗体.
重要提示:
本单元使用到了三个不常用但很有实用价值的技巧
1 字符串与枚举值的相互转换
2 根据字符串创建类/窗口
3 窗体名称数组的声明使用自定义顺序类型来完成的,并且初始化数据时也很灵活.
}
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, CnDockFormControl, CnVIDDockStyle, CnVSNETDockStyle, CnClasses, Menus,
IniFiles, jpeg, ExtCtrls, TypInfo;
type
TFormName = (Form10Name, Form11, Form12, Form13);
TForm9 = class(TForm)
CnDockServer1: TCnDockServer;
CnVSNETDockStyle1: TCnVSNETDockStyle;
mm1: TMainMenu;
Form101: TMenuItem;
Form102: TMenuItem;
mniForm111Menu: TMenuItem;
Form121: TMenuItem;
Form131: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Form102Click(Sender: TObject);
procedure mniForm111MenuClick(Sender: TObject);
procedure Form121Click(Sender: TObject);
procedure Form131Click(Sender: TObject);
private
{ Private declarations }
FFormList: array[TFormName] of TForm;
procedure IniFormList;
procedure ShowFm2(AFormName: TFormName);
public
{ Public declarations }
end;
var
Form9: TForm9;
// FormList: array[TFormName] of TForm = (nil, nil, nil, nil);
implementation
uses Unit10, Unit11, Unit12, Unit13;
{$R *.dfm}
procedure TForm9.Form102Click(Sender: TObject);
begin
{
if FormList[Form10Name] = nil then
FormList[Form10Name]:= TForm10Name.Create(Self);
ShowDockForm(FormList[Form10Name]);
}
ShowFm2(Form10Name);
end;
procedure TForm9.mniForm111MenuClick(Sender: TObject);
begin
{
if FormList[Form11] = nil then
FormList[Form11]:= TForm11.Create(Self);
ShowDockForm(FormList[Form11]);}
ShowFm2(Form11);
end;
procedure TForm9.ShowFm2(AFormName: TFormName);
var
tmpstrClass: string;
begin
tmpstrClass:= 'T'+GetEnumName(TypeInfo(TFormName), Integer(AFormName)); //枚举值转字符串
if (fFormList[AFormName] = nil) and (GetClass(tmpstrClass) <> nil) then
FFormList[AFormName]:= TFormClass(GetClass(tmpstrClass)).Create(Self);
ShowDockForm(FFormList[AFormName]);
end;
procedure TForm9.Form121Click(Sender: TObject);
begin
ShowFm2(Form12);
end;
procedure TForm9.Form131Click(Sender: TObject);
begin
ShowFm2(Form13);
end;
procedure TForm9.FormClose(Sender: TObject; var Action: TCloseAction);
begin
SaveDockTreeToFile(ExtractFilePath(Application.ExeName) + 'DockInfo.ini');
end;
procedure TForm9.FormCreate(Sender: TObject);
var
tmpIni: TIniFile;
tmpStrList: TStrings;
i, j: integer;
ParentName, LastDockSiteName: string;
Visible: Integer;
begin
//ChangeFileExt(Application.ExeName, '.ini');
IniFormList;
if FileExists(ExtractFilePath(Application.ExeName) + 'DockInfo.ini') then
begin
tmpIni:= TIniFile.Create(ExtractFilePath(Application.ExeName) + 'DockInfo.ini');
tmpStrList:= TStringList.Create;
tmpIni.ReadSections(tmpStrList);
for i := tmpStrList.Count - 1 downto 0 do
begin
if GetClass('T'+tmpStrList.Strings[i]) <> nil then
FFormList[TFormName(GetEnumValue(TypeInfo(TFormName), tmpStrList.Strings[i]))]:=
TFormClass(GetClass('T'+tmpStrList.Strings[i])).Create(Self);
//注意根据类名动态创建类时,必须先将该类注册.
end;
LoadDockTreeFromFile(ExtractFilePath(Application.ExeName) + 'DockInfo.ini');
end;
end;
procedure TForm9.IniFormList;
var
i: Integer;
begin
for i := Ord(Low(FFormList)) to Ord(High(FFormList)) do
FFormList[TFormName(i)]:= nil;
end;
initialization
RegisterClass(TForm10Name);
RegisterClass(TForm11);
RegisterClass(TForm12);
RegisterClass(TForm13);
finalization
UnRegisterClass(TForm10Name);
UnRegisterClass(TForm11);
UnRegisterClass(TForm12);
UnRegisterClass(TForm13);
end.
============================
unit Unit10; //停靠窗体之一,其他的类似
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Unit9, CnClasses, CnDockFormControl;
type
TForm10Name = class(TForm)
CnDockClient1: TCnDockClient;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form10: TForm10Name;
implementation
{$R *.dfm}
end.
=================================
//'DockInfo.ini'文件
[Form10Name]
ParentName=Form9@TopDockPanel_A_B_C_D_E_F_G
DockLeft=0
DockTop=18
DockRight=780
DockBottom=118
LastDockSiteName=!@#找不到这个窗体#@!
UnDockLeft=318
UnDockTop=206
LRDockWidth=100
TBDockHeight=118
UnDockWidth=434
UnDockHeight=250
VSPaneWidth=0
Visible=1
BorderStyle=0
WindowState=0
FormStyle=0
DockFormStyle=0
CanDocked=1
EachOtherDocked=1
LeftDocked=1
TopDocked=1
RightDocked=1
BottomDocked=1
DockClientData=
[Form9@TopDockPanel_A_B_C_D_E_F_G]
ParentName=Form9
DockLeft=0
DockTop=0
DockRight=780
DockBottom=118
LastDockSiteName=
UnDockLeft=0
UnDockTop=0
LRDockWidth=0
TBDockHeight=0
UnDockWidth=426
UnDockHeight=118
VSPaneWidth=0
Visible=1
BorderStyle=0
WindowState=0
FormStyle=0
DockFormStyle=3
CanDocked=0
EachOtherDocked=0
LeftDocked=0
TopDocked=0
RightDocked=0
BottomDocked=0
DockClientData=0000040000000000000000000000000000010000000000010000000001010000000000000000010100000000010A000000466F726D31304E616D6501FFFFFFFF
[Form9@TopDockPanel_A_B_C_D_E_F_G_PopupPanel]
ParentName=Form9
DockLeft=0
DockTop=0
DockRight=354
DockBottom=201
LastDockSiteName=
UnDockLeft=0
UnDockTop=0
LRDockWidth=0
TBDockHeight=0
UnDockWidth=354
UnDockHeight=201
VSPaneWidth=0
Visible=0
BorderStyle=0
WindowState=0
FormStyle=0
DockFormStyle=3
CanDocked=0
EachOtherDocked=0
LeftDocked=0
TopDocked=0
RightDocked=0
BottomDocked=0
DockClientData=0000040000000000000000000000000000010000000000010000000001FFFFFFFF
[Form9@BottomDockPanel_A_B_C_D_E_F_G]
ParentName=Form9
DockLeft=0
DockTop=424
DockRight=780
DockBottom=424
LastDockSiteName=
UnDockLeft=0
UnDockTop=0
LRDockWidth=0
TBDockHeight=0
UnDockWidth=426
UnDockHeight=0
VSPaneWidth=0
Visible=1
BorderStyle=0
WindowState=0
FormStyle=0
DockFormStyle=3
CanDocked=0
EachOtherDocked=0
LeftDocked=0
TopDocked=0
RightDocked=0
BottomDocked=0
DockClientData=0000040000000000000000000000000000010000000000010000000001FFFFFFFF
[Form9@BottomDockPanel_A_B_C_D_E_F_G_PopupPanel]
ParentName=Form9
DockLeft=0
DockTop=0
DockRight=354
DockBottom=201
LastDockSiteName=
UnDockLeft=0
UnDockTop=0
LRDockWidth=0
TBDockHeight=0
UnDockWidth=354
UnDockHeight=201
VSPaneWidth=0
Visible=0
BorderStyle=0
WindowState=0
FormStyle=0
DockFormStyle=3
CanDocked=0
EachOtherDocked=0
LeftDocked=0
TopDocked=0
RightDocked=0
BottomDocked=0
DockClientData=0000040000000000000000000000000000010000000000010000000001FFFFFFFF
[Form12]
ParentName=Form9@LeftDockPanel_A_B_C_D_E_F_G
DockLeft=0
DockTop=18
DockRight=159
DockBottom=303
LastDockSiteName=!@#找不到这个窗体#@!
UnDockLeft=318
UnDockTop=206
LRDockWidth=160
TBDockHeight=223
UnDockWidth=434
UnDockHeight=250
VSPaneWidth=0
Visible=1
BorderStyle=0
WindowState=0
FormStyle=0
DockFormStyle=0
CanDocked=1
EachOtherDocked=1
LeftDocked=1
TopDocked=1
RightDocked=1
BottomDocked=1
DockClientData=
[Form13]
ParentName=Form9@LeftDockPanel_A_B_C_D_E_F_G
DockLeft=163
DockTop=18
DockRight=326
DockBottom=303
LastDockSiteName=!@#找不到这个窗体#@!
UnDockLeft=318
UnDockTop=188
LRDockWidth=164
TBDockHeight=223
UnDockWidth=434
UnDockHeight=250
VSPaneWidth=0
Visible=1
BorderStyle=0
WindowState=0
FormStyle=0
DockFormStyle=0
CanDocked=1
EachOtherDocked=1
LeftDocked=1
TopDocked=1
RightDocked=1
BottomDocked=1
DockClientData=
[Form9@LeftDockPanel_A_B_C_D_E_F_G]
ParentName=Form9
DockLeft=0
DockTop=121
DockRight=326
DockBottom=424
LastDockSiteName=
UnDockLeft=0
UnDockTop=0
LRDockWidth=0
TBDockHeight=0
UnDockWidth=326
UnDockHeight=223
VSPaneWidth=0
Visible=1
BorderStyle=0
WindowState=0
FormStyle=0
DockFormStyle=3
CanDocked=0
EachOtherDocked=0
LeftDocked=0
TopDocked=0
RightDocked=0
BottomDocked=0
DockClientData=000004004601000000000000022F0100000100000000000100000000010100000000A30000000101460100000106000000466F726D3132010100000000460100000101460100000106000000466F726D313301FFFFFFFF
[Form9@LeftDockPanel_A_B_C_D_E_F_G_PopupPanel]
ParentName=Form9
DockLeft=0
DockTop=0
DockRight=354
DockBottom=223
LastDockSiteName=
UnDockLeft=0
UnDockTop=0
LRDockWidth=0
TBDockHeight=0
UnDockWidth=99
UnDockHeight=223
VSPaneWidth=0
Visible=0
BorderStyle=0
WindowState=0
FormStyle=0
DockFormStyle=3
CanDocked=0
EachOtherDocked=0
LeftDocked=0
TopDocked=0
RightDocked=0
BottomDocked=0
DockClientData=0000040000000000000000000000000000010000000000010000000001FFFFFFFF
[Form11]
ParentName=Form9@RightDockPanel_A_B_C_D_E_F_G
DockLeft=0
DockTop=18
DockRight=414
DockBottom=303
LastDockSiteName=!@#找不到这个窗体#@!
UnDockLeft=318
UnDockTop=206
LRDockWidth=415
TBDockHeight=100
UnDockWidth=434
UnDockHeight=250
VSPaneWidth=0
Visible=1
BorderStyle=0
WindowState=0
FormStyle=0
DockFormStyle=0
CanDocked=1
EachOtherDocked=1
LeftDocked=1
TopDocked=1
RightDocked=1
BottomDocked=1
DockClientData=
[Form9@RightDockPanel_A_B_C_D_E_F_G]
ParentName=Form9
DockLeft=366
DockTop=121
DockRight=780
DockBottom=424
LastDockSiteName=
UnDockLeft=0
UnDockTop=0
LRDockWidth=0
TBDockHeight=0
UnDockWidth=100
UnDockHeight=223
VSPaneWidth=0
Visible=1
BorderStyle=0
WindowState=0
FormStyle=0
DockFormStyle=3
CanDocked=0
EachOtherDocked=0
LeftDocked=0
TopDocked=0
RightDocked=0
BottomDocked=0
DockClientData=00000400000000000000000000000000000100000000000100000000010100000000000000000101000000000106000000466F726D313101FFFFFFFF
[Form9@RightDockPanel_A_B_C_D_E_F_G_PopupPanel]
ParentName=Form9
DockLeft=0
DockTop=0
DockRight=354
DockBottom=223
LastDockSiteName=
UnDockLeft=0
UnDockTop=0
LRDockWidth=0
TBDockHeight=0
UnDockWidth=99
UnDockHeight=223
VSPaneWidth=0
Visible=0
BorderStyle=0
WindowState=0
FormStyle=0
DockFormStyle=3
CanDocked=0
EachOtherDocked=0
LeftDocked=0
TopDocked=0
RightDocked=0
BottomDocked=0
DockClientData=0000040000000000000000000000000000010000000000010000000001FFFFFFFF
[Form9]
ParentName=
DockLeft=314
DockTop=146
DockRight=1102
DockBottom=616
LastDockSiteName=
UnDockLeft=0
UnDockTop=0
LRDockWidth=434
TBDockHeight=269
UnDockWidth=434
UnDockHeight=269
VSPaneWidth=0
Visible=1
BorderStyle=2
WindowState=0
FormStyle=0
DockFormStyle=0
CanDocked=1
EachOtherDocked=1
LeftDocked=1
TopDocked=1
RightDocked=1
BottomDocked=1
DockClientData=