'类的名称为cPopupMenu OptionExplicit ' Private Type POINT x AsLong y AsLong End Type ' PrivateConst MF_ENABLED =&H0& PrivateConst MF_SEPARATOR =&H800& PrivateConst MF_STRING =&H0& PrivateConst TPM_RIGHTBUTTON =&H2& PrivateConst TPM_LEFTALIGN =&H0& PrivateConst TPM_NONOTIFY =&H80& PrivateConst TPM_RETURNCMD =&H100& Private Declare Function CreatePopupMenu Lib "user32" () AsLong Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu AsLong, ByVal wFlags AsLong, ByVal wIDNewItem AsLong, ByVal sCaption AsString) AsLong Private Declare Function TrackPopupMenu Lib "user32" (ByVal hMenu AsLong, ByVal wFlags AsLong, ByVal x AsLong, ByVal y AsLong, ByVal nReserved AsLong, ByVal hwnd AsLong, nIgnored AsLong) AsLong Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu AsLong) AsLong Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINT) AsLong Private Declare Function GetForegroundWindow Lib "user32" () AsLong Private Declare Function GetMenuString Lib "user32" Alias "GetMenuStringA" (ByVal hMenu AsLong, ByVal wIDItem AsLong, ByVal lpString AsString, ByVal nMaxCount AsLong, ByVal wFlag AsLong) AsLong Private mSelMenuString AsString PublicPropertyGet SelMenuString() AsString SelMenuString = mSelMenuString End Property ' PublicFunction Popup(ParamArray param()) AsLong Dim iMenu AsLong Dim hMenu AsLong Dim nMenus AsLong Dim p As POINT ' get the current cursor pos in screen coordinates GetCursorPos p ' create an empty popup menu hMenu = CreatePopupMenu() ' determine # of strings in paramarray nMenus =1+UBound(param) ' put each string in the menu For iMenu =1To nMenus ' the AppendMenu function has been superseeded by the InsertMenuItem ' function, but it is a bit easier to use. IfTrim$(CStr(param(iMenu -1))) ="-"Then ' if the parameter is a single dash, a separator is drawn AppendMenu hMenu, MF_SEPARATOR, iMenu, "" Else AppendMenu hMenu, MF_STRING + MF_ENABLED, iMenu, CStr(param(iMenu -1)) EndIf Next iMenu ' 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 AsLong Dim buffer AsString 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控件 OptionExplicit PrivateSub Form_Load() List1.AddItem "Right-Click here for a menu" End Sub PrivateSub List1_MouseUp(Button AsInteger, Shift AsInteger, x AsSingle, y AsSingle) Dim oMenu As cPopupMenu Dim lMenuChosen AsLong ' 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 EndIf ' End Sub