' show the menu at the current cursor location; ' the flags make the menu aligned to the right (!); enable the right button to select ' an item; prohibit the menu from sending messages and make it return the index of ' the selected item. ' the TrackPopupMenu function returns when the user selected a menu item or cancelled ' the window handle used here may be any window handle from your application ' the return value is the (1-based) index of the menu item or 0 in case of cancelling iMenu = TrackPopupMenu(hMenu, TPM_RIGHTBUTTON + TPM_LEFTALIGN + TPM_NONOTIFY + TPM_RETURNCMD, p.x, p.y, 0, GetForegroundWindow(), 0) Dim result As Long Dim buffer As String Const MF_BYPOSITION = &H400&
buffer = Space(255) result = GetMenuString(hMenu, (iMenu - 1), buffer, _ Len(buffer), MF_BYPOSITION) 'Debug.Print buffer mSelMenuString = Trim(buffer) ' release and destroy the menu (for sanity) DestroyMenu hMenu
' return the selected menu item's index Popup = iMenu
End Function
'结束
'以下是实例,在Form上添加一个ListBox控件
Option Explicit
Private Sub Form_Load() List1.AddItem "Right-Click here for a menu" End Sub
Private Sub List1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single) Dim oMenu As cPopupMenu Dim lMenuChosen As Long ' If Button = vbRightButton Then Set oMenu = New cPopupMenu ' ' Pass in the desired menu, use '-' for a separator ' lMenuChosen = oMenu.Popup("Menu 1", "Menu 2", "Menu 3", _ "-", "Menu 4") ' Debug.Print lMenuChosen Debug.Print oMenu.SelMenuString End If
' End Sub
用VB编写一个弹出菜单类下 |