// Creating MDI = Multiple Document Interface with Tabbed Interface (with Sample Codes)
// Unit 1 Form
// Unit 2 Form
// Running Application
// Sample Code
unit Unit1;
interface
uses
// ... please put your own...
type
TMainForm = class(TForm)
mdiChildrenTabs: TTabSet;
ToolBar1: TToolBar;
ToolButton1: TToolButton;
procedure mdiChildrenTabsChange(Sender: TObject; NewTab: Integer;
var AllowChange: Boolean);
procedure ToolButton1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
Procedure MDIChildCreated(const childHandle : THandle);
Procedure MDIChildDestroyed(const childHandle : THandle);
end;
var
MainForm: TMainForm;
implementation
{$R *.dfm}
uses Unit2;
// ****************************************************************************** //
procedure TMainForm.mdiChildrenTabsChange(Sender: TObject; NewTab: Integer;
var AllowChange: Boolean);
// ****************************************************************************** //
var
cHandle: Integer;
k: Integer;
begin
cHandle := Integer(mdiChildrenTabs.Tabs.Objects[NewTab]);
if mdiChildrenTabs.Tag = -1 then Exit;
for k := 0 to MDIChildCount - 1 do
begin
if MDIChildren[k].Handle = cHandle then
begin
MDIChildren[k].Show;
Break;
end;
end;
end;
// ****************************************************************************** //
procedure TMainForm.ToolButton1Click(Sender: TObject);
// ****************************************************************************** //
var FormChild : TBaseChildForm;
begin
FormChild:=TBaseChildForm.Create(nil);
FormChild.Show;
end;
// ****************************************************************************** //
procedure TMainForm.MDIChildCreated(const childHandle: THandle);
// ****************************************************************************** //
begin
mdiChildrenTabs.Tabs.AddObject(TForm(FindControl(childHandle)).Caption, TObject(childHandle));
mdiChildrenTabs.TabIndex := -1 + mdiChildrenTabs.Tabs.Count;
end;
// ****************************************************************************** //
procedure TMainForm.MDIChildDestroyed(const childHandle: THandle);
// ****************************************************************************** //
var
idx: Integer;
begin
idx := mdiChildrenTabs.Tabs.IndexOfObject(TObject(childHandle));
mdiChildrenTabs.Tabs.Delete(idx);
end;
end.
// ********************************** Unit 2 *********************************** //
unit Unit2;
interface
uses
// ... please put your own...
type
TBaseChildForm = class(TForm)
Button1: TButton;
Panel1: TPanel;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
procedure WMMDIACTIVATE(var msg : TWMMDIACTIVATE) ; message WM_MDIACTIVATE;
public
{ Public declarations }
end;
var
BaseChildForm: TBaseChildForm;
implementation
{$R *.dfm}
uses Unit1;
{ TBaseChildForm }
// ****************************************************************************** //
procedure TBaseChildForm.Button1Click(Sender: TObject);
// ****************************************************************************** //
begin
panel1.Caption:='Hello World';
end;
// ****************************************************************************** //
procedure TBaseChildForm.FormCreate(Sender: TObject);
// ****************************************************************************** //
begin
MainForm.MDIChildCreated(self.Handle)
end;
// ****************************************************************************** //
procedure TBaseChildForm.FormDestroy(Sender: TObject);
// ****************************************************************************** //
begin
MainForm.MDIChildDestroyed(self.Handle);
end;
// ****************************************************************************** //
procedure TBaseChildForm.FormClose(Sender: TObject; var Action: TCloseAction);
// ****************************************************************************** //
begin
action:=caFree;
end;
// ****************************************************************************** //
procedure TBaseChildForm.WMMDIACTIVATE(var msg: TWMMDIACTIVATE);
// ****************************************************************************** //
var
active: TWinControl;
idx: Integer;
begin
active := FindControl(msg.ActiveWnd) ;
if Assigned(active) then
begin
idx := MainForm.mdiChildrenTabs.Tabs.IndexOfObject(TObject(msg.ActiveWnd));
MainForm.mdiChildrenTabs.Tag := -1;
MainForm.mdiChildrenTabs.TabIndex := idx;
MainForm.mdiChildrenTabs.Tag := 0;
end;
end;
end.