Author: Jonas Bilinkevicius
I want to create a TToolBar that will have corresponding buttons to the MDI
children in my form, so that the user can switch between the children by clicking
on the buttons - not going through the MDI menu. How do I do that ? Adding a button
on MDI child creation, deleting the button when the corresponding window is closed,
respond to CTRL+TAB, CTRL+F4 messages and so on.
Answer:
I did something like this. In my mdi parent form, I have three public procedures:
procedure addwindow(Sender: TForm; const Str: string);
procedure delwindow(Sender: TObject);
procedure activatewindow(Sender: TObject);
In the child form's oncreate, ondestroy, onformactivate, I call the corresponding
parent's method sending self as the parameter. I store sender in the toolbutton's
tag property so I know which button corresponds to which child window.
The code looks like this (TXnewsFrame is the mdi parent form; Taskbar is my
toolbar).
1 { str is window's caption }2 3 procedure TXnewsFrame.AddWindow(Sender: TForm; const Str: string);
4 begin5 with TToolButton.Create(TaskBar) do6 begin7 AutoSize := true;
8 Left := TaskBar.Width;
9 Parent := TaskBar;
10 Style := tbsCheck;
11 Grouped := true;
12 AllowAllUp := false;
13 OnClick := TaskClick;
14 Caption := Str;
15 Hint := Str;
16 Tag := Integer(Sender); { store form in button's tag }17 Down := true;
18 end;
19 TaskBar.Update;
20 end;
21 22 procedure TXnewsFrame.DelWindow(Sender: TObject);
23 var24 i: integer;
25 begin26 with TaskBar do27 for i := ButtonCount - 1 downto 0 do28 if TForm(Buttons[i].Tag) = Sender then29 begin30 Buttons[i].Free;
31 break;
32 end;
33 end;
34 35 procedure TXnewsFrame.ActivateWindow(Sender: TObject);
36 var37 i: integer;
38 begin39 with TaskBar do40 for i := ButtonCount - 1 downto 0 do41 with Buttons[i] do42 if TForm(Tag) = Sender then43 begin44 Down := true;
45 break;
46 end;
47 end;
48 49 { taskbar's onclick event handler }50 51 procedure TXnewsFrame.TaskBarClick(Sender: TObject);
52 var53 F: TForm;
54 i: integer;
55 begin56 if (sender is TToolButton) then57 begin58 F := TForm(TComponent(Sender).Tag);
59 with F do60 if (F <> Self.ActiveMDIChild) ornot Visible then61 begin62 BringToFront;
63 if WindowState = wsMinimized then64 ShowWindow(Handle, SW_RESTORE);
65 end;
66 end;
67 end;