Search code examples
delphidelphi-xe2vcl

How to hide TActionMainMenuBar images


I have TActionMainMenuBar placed on the form, which looks like this:

ActionMainMenuBar

Now, it looks perfectly fine except that blank gap on the left where images should go. Since I don't have need to draw images in the menu, how can I hide that gap completely? Haven't been able to find any properties which I can use to hide this, and Google queries returned no results on the topic.


Solution

  • Below sample tries to demonstrate what it would take to use your own menu style. It just tries to gain the space from the unused images but you can override any aspect of the drawing, see 'xpactnctrls.pas' for possible implementation.

    type
      TBarStyle = class(TXPStyleActionBars)
      public
        function GetControlClass(ActionBar: TCustomActionBar;
          AnItem: TActionClientItem): TCustomActionControlClass; override;
      end;
    
      TMenuStyle = class(TXPStyleMenuItem)
      protected
        procedure CalcLayout; override;
      public
        procedure CalcBounds; override;
      end;
    
    var
      BarStyle: TBarStyle;
    
    function TBarStyle.GetControlClass(ActionBar: TCustomActionBar;
      AnItem: TActionClientItem): TCustomActionControlClass;
    begin
      Result := inherited GetControlClass(ActionBar, AnItem);
      if ActionBar is TCustomActionPopupMenu then
        Result := TMenuStyle;
    end;
    
    procedure TMenuStyle.CalcLayout;
    begin
      inherited;
      GlyphPos := Point(-16, GlyphPos.Y);
    end;
    
    procedure TMenuStyle.CalcBounds;
    var
      R: TRect;
    begin
      inherited;
      R := TextBounds;
      OffsetRect(R, -16, 0);
      TextBounds := R;
    end;
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      ActionMainMenuBar1.ActionManager.Style := BarStyle;
    end;
    
    initialization
      BarStyle := TBarStyle.Create;
      RegisterActnBarStyle(BarStyle);
    finalization
      UnregisterActnBarStyle(BarStyle);
      BArStyle.Free;