delphi - How to detect TMenuItem right click? -
platform:delphi 2010
- drop tmainmenu on form1
- drop tpopupmenu on form1
- add mainmenu1 , popupmenu items (mainmenu --> file -->item1 , popupmenu-->popup item1)
- item1.onrgihtclick show popupmenu
- f9
- 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
here's menu structure below sample
file1 edit1 fileitem11 edititem11 fileitem21 edititem21
and 2 popup menu items. 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 fselecteditem := mainmenu1.finditem(getmenuitemid(msg.lparam, msg.wparam), fkcommand); if (not ftracking) , (fselecteditem <> nil) 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 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;
Comments
Post a Comment