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:
1 2 procedure addwindow(Sender: TForm; const Str: string);
3 procedure delwindow(Sender: TObject);
4 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).
5 6 { str is window's caption }7 8 procedure TXnewsFrame.AddWindow(Sender: TForm; const Str: string);
9 begin10 with TToolButton.Create(TaskBar) do11 begin12 AutoSize := true;
13 Left := TaskBar.Width;
14 Parent := TaskBar;
15 Style := tbsCheck;
16 Grouped := true;
17 AllowAllUp := false;
18 OnClick := TaskClick;
19 Caption := Str;
20 Hint := Str;
21 Tag := Integer(Sender); { store form in button's tag }22 Down := true;
23 end;
24 TaskBar.Update;
25 end;
26 27 procedure TXnewsFrame.DelWindow(Sender: TObject);
28 var29 i: integer;
30 begin31 with TaskBar do32 for i := ButtonCount - 1 downto 0 do33 if TForm(Buttons[i].Tag) = Sender then34 begin35 Buttons[i].Free;
36 break;
37 end;
38 end;
39 40 procedure TXnewsFrame.ActivateWindow(Sender: TObject);
41 var42 i: integer;
43 begin44 with TaskBar do45 for i := ButtonCount - 1 downto 0 do46 with Buttons[i] do47 if TForm(Tag) = Sender then48 begin49 Down := true;
50 break;
51 end;
52 end;
53 54 { taskbar's onclick event handler }55 56 procedure TXnewsFrame.TaskBarClick(Sender: TObject);
57 var58 F: TForm;
59 i: integer;
60 begin61 if (sender is TToolButton) then62 begin63 F := TForm(TComponent(Sender).Tag);
64 with F do65 if (F <> Self.ActiveMDIChild) ornot Visible then66 begin67 BringToFront;
68 if WindowState = wsMinimized then69 ShowWindow(Handle, SW_RESTORE);
70 end;
71 end;
72 end;