2005. június 26., vasárnap

Make shortcuts on a secondary TFrame work


Problem/Question/Abstract:

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:

function TMyform.IsShortcut(var Message: TWMKey): Boolean; {override}
var
  ctrl: TWinControl;
  comp: TComponent;
  i: Integer;
begin
  ctrl := ActiveControl;
  if ctrl <> nil then
  begin
    repeat
      ctrl := ctrl.Parent
    until
      (ctrl = nil) or (ctrl is TCustomFrame);
    if ctrl <> nil then
    begin
      for i := 0 to ctrl.ComponentCount - 1 do
      begin
        comp := ctrl.Components[i];
        if comp is TCustomActionList then
        begin
          result := TCustomActionList(comp).IsShortcut(Message);
          if result then
            Exit;
        end;
      end;
    end;
  end;
  result inherited IsShortcut(Message);
end;

Nincsenek megjegyzések:

Megjegyzés küldése