Hola JuniorSoft.
La idea que te propongo es agrupar la creación del MDIChild y del ToolButton correspondiente como hago en este ejemplo simplificado, que usa un
TToolBar un
TPanel (
alBottom) y dentro de él tres
TButton.
Código Delphi
[-]
type
TfrmMain = class(TForm)
...
private
FChildTags: Integer;
procedure OpenMDIChild(AClass: TFormClass; const btnCaption: string; aAction: TAction );
public
property ChildTag: Integer read FChildTags;
procedure DeleteToolButton(const TagButton: Integer);
end;
...
implementation
uses Unit2, Unit3, Unit4;
procedure TfrmMain.FormCreate(Sender: TObject);
begin
FChildTags := 1; end;
procedure TfrmMain.OpenMDIChild(AClass: TFormClass; const btnCaption: string; aAction: TAction);
var
i, inx: Integer;
nb: TToolButton;
begin
for i:= MDIChildCount-1 downto 0 do
if MDIChildren[i] is AClass then
begin
if MDIChildren[i].WindowState = wsMinimized then
MDIChildren[i].WindowState:= wsNormal;
MDIChildren[i].BringToFront;
Exit;
end;
with AClass.Create(Self) do
begin
Tag := FChildTags;
nb := TToolButton.Create(ToolBar1);
nb.Height := ImageList1.Height;
nb.Width := ImageList1.Width;
nb.Caption := btnCaption;
inx := ToolBar1.ButtonCount - 1;
if inx > -1 then
nb.Left := ToolBar1.Buttons[inx].Left + ToolBar1.Buttons[inx].Width
else
nb.Left := 0;
nb.Action := aAction;
nb.Parent := ToolBar1;
nb.Tag := ChildTag;
Inc(FChildTags);
end;
end;
procedure TfrmMain.DeleteToolButton(const TagButton: Integer);
var
i: Integer;
begin
for i:= ToolBar1.ButtonCount-1 downto 0 do
if ToolBar1.Buttons[i].Tag = TagButton then
ToolBar1.Buttons[i].Free;
end;
procedure TfrmMain.btnChild1Click(Sender: TObject);
begin
OpenMDIChild(TChild1, 'Child1', TAction(ActionList1.Actions[0]));
end;
procedure TfrmMain.btnChild2Click(Sender: TObject);
begin
OpenMDIChild(TChild2, 'Child2', TAction(ActionList1.Actions[1]));
end;
procedure TfrmMain.btnChild3Click(Sender: TObject);
begin
OpenMDIChild(TChild3, 'Child3', TAction(ActionList1.Actions[2]));
end;
procedure TfrmMain.Action1Execute(Sender: TObject);
begin
ShowMessage(TToolButton(Sender).Name);
end;
procedure TfrmMain.Action2Execute(Sender: TObject);
begin
ShowMessage(TToolButton(Sender).Name);
end;
procedure TfrmMain.Action3Execute(Sender: TObject);
begin
ShowMessage(TToolButton(Sender).Name);
end;
...
Salida:
Como verás es sólo un ejemplo orientativo, si nos explicas mas detalladamente la situación, tal vez lo podamos ajustar.
Saludos
Pd: Te ajunto los archivos fuentes de la prueba.