Author: Lou Adler
I am using multiple instances of a frame in my application. On the frame there are
a couple of TActions that have shortcuts. If I have only one frame on the form,
everything works fine and the shortcuts work. But if I am adding a secondary frame,
it is always only one of the two frames that executes the TAction (eg. one of them
will never have their TAction executed). The function I would like to see is, that
when pressing the shortcut, the frame with the active component should execute its
corresponding TActions. Is this possible with TFrames?
Answer:
With a bit of work. Override the host form's IsShortcut function. Pass the trapped
message to the active frame's Actionlist.IsShortcut method first, if it returns
true return True as result as well, otherwise return the result of the inherited
IsShortcut function. This can be made fairly generic:
1 2 function TMyform.IsShortcut(varmessage: TWMKey): Boolean; {override}3 var4 ctrl: TWinControl;
5 comp: TComponent;
6 i: Integer;
7 begin8 ctrl := ActiveControl;
9 if ctrl <> nilthen10 begin11 repeat12 ctrl := ctrl.Parent
13 until14 (ctrl = nil) or (ctrl is TCustomFrame);
15 if ctrl <> nilthen16 begin17 for i := 0 to ctrl.ComponentCount - 1 do18 begin19 comp := ctrl.Components[i];
20 if comp is TCustomActionList then21 begin22 result := TCustomActionList(comp).IsShortcut(message);
23 if result then24 Exit;
25 end;
26 end;
27 end;
28 end;
29 result inherited IsShortcut(message);
30 end;