1

Is there any way to embed a TPopupMenu directly on a form, as if it was a panel always open? or maybe just the TMenuItems.

hikari
  • 3,393
  • 1
  • 33
  • 72
  • No, it's not possible. Of course there are many ways to create a good-looking and user-friendly menu, but embedding a constantly open context menu isn't possible. Perhaps a simple `TToolBar` will work for you? – Andreas Rejbrand Aug 15 '20 at 11:35
  • Or maybe a way to keep the menu open after you click on an item – hikari Aug 15 '20 at 11:43
  • *That* is possible. But it will close if your app loses focus or if you click somewhere outside the menu. – Andreas Rejbrand Aug 15 '20 at 11:44
  • I thought of embedding it to a (~popup) form as a way to select a few options/filters, a simple menu is very handy for that but not if it closes after a click or loses focus too easily. It has auto-checkmarks, good looking ~hot-tracking, easy separators and icons.. – hikari Aug 15 '20 at 11:47
  • Menus are expected to disappear once an item has been clicked. Consider showing/hiding tool windows. – AmigoJack Aug 15 '20 at 11:57
  • Nothing I tried looks nearly as good and practical as a PopupMenu :( I came across this very nice solution (2nd answer) https://stackoverflow.com/questions/5983217/how-to-select-a-menu-item-without-closing-the-menu the only problem is with VCL styles, an item with Auto-check does not repaint to show the checkmark until you hover away from it onto another item. – hikari Aug 15 '20 at 12:30
  • 1
    Then you might want to create a custom control. Also, if you want to make a robust and bug-free application, you might want to abandon VCL styles altogether... – Andreas Rejbrand Aug 15 '20 at 12:33
  • It's just a hobby/free app. – hikari Aug 15 '20 at 12:42
  • 1
    @hikari please link answers then, not questions - I assume https://stackoverflow.com/a/25834877/4299358 – AmigoJack Aug 15 '20 at 13:47
  • Yeah that one, it just has that VCL styles issue. – hikari Aug 15 '20 at 18:07
  • @AndreasRejbrand "*A menu ... isn't a window*" - yes it is. It's just not a window that you have direct access to, but its HWND can be obtained with some work. Its class name is "#32768" – Remy Lebeau Aug 15 '20 at 19:33
  • @RemyLebeau: Yes, it makes sense that it is implemented as a window under the hood. (By the way: Do you know if this has always been the case?) I've removed my incorrect comment. Still, the practical consequences are not changed: you don't want to try to make a Win32 menu into a stationary control on a form. – Andreas Rejbrand Aug 15 '20 at 19:54
  • @RemyLebeau: This got me wondering: surely the mouse cursor isn't a window, or is it? – Andreas Rejbrand Aug 15 '20 at 19:57
  • @AndreasRejbrand that, I don't know – Remy Lebeau Aug 15 '20 at 20:03
  • @Andreas - [It isn't](https://learn.microsoft.com/en-us/windows-hardware/drivers/display/pointer-control). Note the "Drv" prefix for GDI functions mentioned in the link (when it's needed to fallback on GDI) which specifies them as device driver interface functions. – Sertac Akyuz Aug 16 '20 at 01:22
  • @SertacAkyuz: Thanks for confirming that. I'd been very surprised if that was a window too! – Andreas Rejbrand Aug 16 '20 at 09:21

1 Answers1

5

No, it is not possible to embed a menu on a form. The reason is partly that a menu isn't an ordinary window that you can easily manipulate.

So you need to find a different solution. And there are many options you can chose from:

  1. Using a TToolBar:

    A screen recording of a form with two vertical toolbar (in different visual styles) with auto-check buttons.

    It doesn't look particularly modern and out of the box you don't get much control over the appearance, though. Also, I don't know exactly how robust this solution is. I stopped using toolbars many years ago.

  2. Using a TCheckListBox:

    Screenshot of a form with a check list box.

    In this case, I'd recommend you to create a subclass TCheckListBoxEx which toggles an item if you double-click its caption.

  3. Creating a custom control:

    This is what I'd do if it is about a central GUI in an important application, because this way you get full control over the appearance and behaviour and can make it really robust. I have done a modern such menu at work, but currently I am at home so I cannot show you it. Here, however, is a menu I made more than ten years ago for a hobby project:

    Screen recording of a custom-control-based solution with auto-toggling items.

  4. If you don't need the menu to be attached to the form like a control, but only need it not to close when you select an item in it, there are (hacky) ways to achieve that. But that is a different Q.

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • Had a look at TToolbar, it's terrible tbh – hikari Aug 16 '20 at 06:26
  • @hikari: The `TToolBar` is mainly supposed to be used for the classic kind of toolbars that you find in for instance Explorer or WordPad in Windows 95. For that intended purpose, it is brilliant (albeit slightly dated visually). – Andreas Rejbrand Aug 16 '20 at 09:23
  • The benefit of the toolbar in this case is that it can be driven by actions (e.g. in a `TActionList`), just like menu items can (should) be. – Andreas Rejbrand Aug 16 '20 at 09:24