Skip to content
The Road to Delphi — Delphi, VCL, VCL Styles, UxTheme and WinAPI

Adding VCL Styles support to a TPopupMenu in 2 lines of code.

, ,

UPDATE

This is an old article if you are looking how add vcl styles support to the Menus, dialogs and more try the VCL Styles Utils  project.


The TPopupMenu component has not support for Vcl Styles directly, so if you use this control in a form with Vcl Styles enabled you will get a result like this.

To fix this you have 2 ways :

  1. Write a style hook for a TPopUpMenu (The hard way)
  2. Or use the TPopupActionBar component (the easy way)

In this post I will show you how use the option 2, taking advantage of two facts

  1. The TPopupActionBar component has full support for Vcl Styles
  2. Descends directly from the TPopupMenu component.

So adding the Vcl.ActnPopup unit to your project and using a interposer class (before of the form declaration which contains the PopupMenu), you can add vcl styles to the TPopupMenu component.

[sourcecode language=”delphi” wraplines=”false”]
uses Vcl.ActnPopup;
type TPopupMenu=class(Vcl.ActnPopup.TPopupActionBar);
[/sourcecode]

This is the result after of add the 2 above lines of code.

25 responses

  1. Hraouf Avatar
    Hraouf

    Thanks a lot Rodrigo, very helpful well done

    1. Thanks for your feedback

  2. Good tip, would it be simpler though to just change the declaration of the TPopupMenu to TPopupActionBar in the .dfm and .pas? Then there is no need for an interposer class.

    1. Sure, replace the component is a valid option too.

  3. Is there a QC report for that omission?

    1. François, as far I know there is not QC, report for this.

  4. Is there any way to use “something like this” to style TMainMenu?

    1. I’m working in a workaround just now for skin the popup part of an TMainMenu, in the meanwhile you can use a TActionMainMenuBar component which supports the vcl styles pretty well.

      1. Could you show me example, please?

        1. My comment means that you must use a TActionMainMenuBar component instead of a TMainMenu. Exist several samples of using this component in the internet.

  5. grubste Avatar
    grubste

    The trick with the TPopupActionBar instead of the TPopupMenu has one flaw: if one menuitem of the menu is set to default:=true, then the application crashes with a stack overflow when trying to show the menu. This only happens if you change the VCL style.
    Does this happen to anyone else than me?

    1. This is a bug of the TPopupActionBar component, and is not related to the vcl styles (the bug occurs even in a plain vanilla app with a TPopupActionBar and no vcl styles enabled). you must report this to the QC page http://qc.embarcadero.com/

  6. Hi Rodrigo. I have a doubt about using VCL Themes.
    When i use themes, it looks like some of my custom modifications gets overriten.
    An example would be the ‘Font Color’ of my DBEdits. I had set it to BLUE and when i use styles, it just goes BLACK. Is there an workaround about this?

    att,

  7. Shuhin Ashayeri Avatar
    Shuhin Ashayeri

    Hi
    Right To Left direction not supported in this method!!!

  8. Why “AutoHotkeys:= maManual”invalid?Thanks

    1. Can you explain your comment?

      1. PopupMenu1.AutoHotkeys:= maManual;
        //add 2 menu item ‘复制’,’粘贴’
        //But the menu item still show hot keys,like this ‘复制(X)’,’粘贴(Y)’…

  9. Thank you Rodrigo for this.
    Is there a way to make the TMenuItem Break property work too?
    Using this trick that property is just ignored.
    I guess the “Hard way” is needed for this.

  10. Sebastian Avatar
    Sebastian

    HI,
    I’m also interested in the MenuItem Property Break and the property Bitmap. Both will be ignored if i use the interposer class TPopupActionBar. Is there a simple way to handle this or do i have to write my own TPopupMenu StyleHook?

  11. […] native look and feel  (exists some workarounds for this like use a TPopupActionBar  as described here, but this only works partially, and doesn’t support the child menus of a TMainMenu).  Since […]

Leave a Reply