6

platform:delphi 2010

  1. drop Tmainmenu on form1
  2. drop Tpopupmenu on form1
  3. add mainmenu1 and popupmenu items (mainmenu --> file -->item1 , popupmenu-->popup item1)
  4. item1.onRgihtClick show popupmenu
  5. f9
  6. file-->item1 right click, popupmenu , select item1 bla bla bla....
object Form1: TForm1 Left = 0 Top = 0 Caption = 'Form1' ClientHeight = 222 ClientWidth = 447 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] Menu = MainMenu1 OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 object MainMenu1: TMainMenu Left = 136 Top = 64 object file1: TMenuItem Caption = 'file' object recentfile1: TMenuItem Caption = 'item 1' end end end object PopupMenu1: TPopupMenu Left = 24 Top = 136 object popupitem1: TMenuItem Caption = 'popup item' OnClick = popupitem1Click end end end 
11
  • 3
    Having a right-click menu for a menu item would be very contrary to any kind of expected behavior of a menu. Why not put the extra menu items in a submenu instead? Commented Sep 24, 2010 at 14:47
  • 1
    @Michael: Yet this behaviour is common in Microsoft Windows, if the items of the popup menu are files (in some sense). In Windows 7, click Start/Recent. Here you can right-click any menu item and get a new popup menu. Commented Sep 24, 2010 at 14:49
  • 2
    I said it's not expected behavior, not that it's unheard of. There are exceptions to the rule, yes, but nothing in this question suggests that it makes sense for this to be one of them. You don't get right-click options on your MRU list in Word, for example. (No, I'm not counting 2007 and 2010 here, because they're not regular menus - and neither is the start menu, for that matter.) Commented Sep 24, 2010 at 15:06
  • 1
    @Andreas: No need to pay for EE: en.wikipedia.org/wiki/… Commented Sep 24, 2010 at 15:29
  • 3
    @Michael - + 1 for mentioning working examples not being regular menus. But, this is a technical question. It can, but does not have to justify its context. IOW, nothing in the question also suggests that it does not make sense for the OP's particular design - it is only a simplified problem presentation. Commented Sep 24, 2010 at 15:43

1 Answer 1

3

Here's the menu structure for the below sample

File1 Edit1 FileItem11 EditItem11 FileItem21 EditItem21 

and two popup menu items. The code:  

type TForm1 = class(TForm) MainMenu1: TMainMenu; File1: TMenuItem; FileItem11: TMenuItem; FileItem21: TMenuItem; Edit1: TMenuItem; EditItem11: TMenuItem; EditItem21: TMenuItem; PopupMenu1: TPopupMenu; PopupItem11: TMenuItem; PopupItem21: TMenuItem; procedure PopupItem11Click(Sender: TObject); procedure PopupItem21Click(Sender: TObject); private FSelectedItem: TMenuItem; FTracking: Boolean; procedure MenuRButtonUp(var Msg: TMessage); message WM_MENURBUTTONUP; public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} { TForm1 } procedure TForm1.MenuRButtonUp(var Msg: TMessage); var Cmd: UINT; begin if not FTracking then FSelectedItem := MainMenu1.FindItem(GetMenuItemID(Msg.LParam, Msg.WParam), fkCommand); if (not FTracking) and (FSelectedItem <> nil) then begin FTracking := True; LongBool(Cmd) := TrackPopupMenuEx(PopupMenu1.Handle, TPM_RECURSE or TPM_BOTTOMALIGN or TPM_RETURNCMD, Mouse.CursorPos.X, Mouse.CursorPos.Y, Handle, nil); FTracking := False; if Cmd <> 0 then PopupMenu1.DispatchCommand(Cmd); end; inherited; end; procedure TForm1.PopupItem11Click(Sender: TObject); begin Caption := 'Popup Item 1 clicked on ' + FSelectedItem.Caption; end; procedure TForm1.PopupItem21Click(Sender: TObject); begin // whatever.. Caption := 'Popup Item 2 clicked on ' + FSelectedItem.Caption; end; 
Sign up to request clarification or add additional context in comments.

5 Comments

-1, for saying that standard menus do not support richt-clicking. They do. This is possible via the WM_MENURBUTTONUP message, which was introduced for this exact purpose: msdn.microsoft.com/en-us/library/ms647610.aspx. Do a search through the archives at deja.com, there are plenty of examples of using WM_MENURBUTTONUP in Delphi.
great job , great code. thank you very much to everyone. p.s. @sertac bey, ilgi ve alakaniz icin size ayri tesekkur ederim :)
@Remy - Ok, removed what i said.
Would there be a way to extend this to also handle a right-click on the topmost menu items (File1 and Edit1)?
@Aladdin - It doesn't look like so. Probably you would need to interfere WM_NCRBUTTONUP, check for a hit test of HTMENU and then call GetMenuBarInfo for each item to find the item which you released the right button on.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.