Wilson WindowWare Tech Support

WinBatch WinBatch+Compiler WebBatch
Home | Tech Database | Tech BBS | White Papers | Purchase


Sample Code Demonstrates How to Create Simple Popup Menus

Keywords:      popup menus

The following sample code shows how to create simple Popup menus when the user right-clicks:
#DefineFunction CreatePopupMenu()
user32=StrCat(DirWindows(1),"User32.DLL")
return DLLCall(user32,long:"CreatePopupMenu")
#EndFunction

#DefineFunction DestroyMenu(hmenu)
user32=StrCat(DirWindows(1),"User32.DLL")
return DLLCall(user32,long:"DestroyMenu",long:hmenu)
#EndFunction

#DefineFunction AppendMenu(hmenu, wFlags, wIDNewItem, lpNewItem)
user32=StrCat(DirWindows(1),"User32.DLL")
return DLLCall(user32,long:"AppendMenuA",long:hmenu, long:wFlags, long:wIDNewItem, lpstr:lpNewItem)
#EndFunction

#DefineFunction TrackPopupMenu(hmenu, wFlags, x, y, nReserved, hwnd, rect)
user32=StrCat(DirWindows(1),"User32.DLL")
return DLLCall(user32,long:"TrackPopupMenu",long:hmenu, long:wFlags, long:x, long:y, long:nReserved, long:hwnd, long:rect)
#EndFunction

MF_STRING = 0
TPM_RETURNCMD = 256
TPM_LEFTBUTTON = 0

hwnd=dllhwnd("")

;create menu
hmenu = CreatePopupMenu()
AppendMenu(hmenu, MF_STRING, 1, "Edit")
AppendMenu(hmenu, MF_STRING, 2, "Exit")

boxopen("", "")

:begin
s=0
while s==0
  if MouseInfo(4)==1 ;right click
    ;mouse coordinates
    mc = MouseInfo(3)
    s = TrackPopupMenu(hmenu, TPM_RETURNCMD|TPM_LEFTBUTTON, itemextract(1, mc, " "), itemextract(2, mc, " "), 0, hwnd, 0)
  endif
endwhile

switch s
  
  case 1 ;edit
    message("", "edit")
    break

  case 2 ;exit
    message("", "exit")
    DestroyMenu(hmenu)
    boxdestroy(1)
    exit
    break

endswitch
goto begin

;set MIIM_SUBMENU  MENUITEMINFO 
;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/menus_39f3.asp

;insert
;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/menus_4c6l.asp

Article ID:   W15285