Search code examples
delphiribbonribbon-control

Executing Commands from the Microsoft Ribbon?


I have created my first non Delphi Ribbon using the Microsoft Ribbon Framework thanks to the help and advice provided in this thread.

Following the guide that A.Bouchez posted in that thread I have managed to compile my project and see the Microsoft Ribbon in action.

However, I cannot seem to get the Ribbon to respond to input when a Command is executed.

I always use the TActionManager to manage my Events, so all I need is to link each TAction from the TActionManager into the Ribbon. Following the tutorial linked above, I tried the following to no avail:

// actNew is the name of a TAction set in the TActionManager
procedure TfrmMain.actNewExecute(Sender: TObject);
begin
  ShowMessage('execute new event');
end;

procedure TfrmMain.CommandCreated(const Sender: TUIRibbon; const Command: TUICommand);
begin
  inherited;

  case Command.CommandId of
    cmdNew: // cmdNew was defined in the Ribbon Designer
    begin
      // link the ribbon commands to the TActions
      actNew.OnExecute(Command as TUICommandAction); // obviously will not work
    end;
  end;
end;

So, how do I assign my TActions to the Ribbon?

Thanks.


Solution

  • I found out how to execute the commands from viewing the samples provided (dont know how I missed them!). The events seem to have to be defined independent of TActions, so I guess that is the way to go.

    It is possible though linking the Actions OnExecute handler inside the procedure that is used for calling commands for the Ribbon, example:

    private
      CommandNew: TUICommandAction;
      procedure CommandNewExecute(const Args: TUICommandActionEventArgs);
    
      procedure UpdateRibbonControls;
    strict protected
      procedure RibbonLoaded; override;
      procedure CommandCreated(const Sender: TUIRibbon; const Command: TUICommand); override;
    
    implementation
    
    procedure TfrmMain.RibbonLoaded;
    begin
      inherited;
    
      Color:= ColorAdjustLuma(Ribbon.BackgroundColor, -25, False);
      UpdateRibbonControls;
    end;
    
    // set command states here
    procedure TfrmMain.UpdateRibbonControls;
    begin
      if Assigned(CommandNew) then
        CommandNew.Enabled:= True;
    end;
    
    // assign the commands
    procedure TfrmMain.CommandCreated(const Sender: TUIRibbon; const Command: TUICommand);
    begin
      inherited;
    
      case Command.CommandId of
        cmdNew: // command id defined in the ribbon designer
        begin
          CommandNew:= Command as TUICommandAction;
          CommandNew.OnExecute:= NewExecute;
        end;
      end;
    end;
    
    // command events
    procedure TfrmMain.NewExecute(const Args: TUICommandActionEventArgs);
    begin
      actNew.OnExecute(nil); // < this is calling the event code from a TAction      
    end;
    

    The Samples folder inside the Ribbon Framework will demonstrate this more clearer. The Framework can be found here: http://www.bilsen.com/windowsribbon/index.shtml