Write a pop-up menu class with VB

zhaozj2021-02-11  158

'Class name is cpopupmenu

Option Explicit'Private Type POINT x As Long y As LongEnd Type'Private Const MF_ENABLED = & H0 & Private Const MF_SEPARATOR = & H800 & Private Const MF_STRING = & H0 & Private Const TPM_RIGHTBUTTON = & H2 & Private Const TPM_LEFTALIGN = & H0 & Private Const TPM_NONOTIFY = & H80 & Private Const TPM_RETURNCMD = & H100 & Private Declare Function CreatePopupMenu Lib "user32" () As LongPrivate Declare Function AppendMenu Lib "user32" Alias ​​"AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal sCaption As String) As LongPrivate Declare Function TrackPopupMenu Lib "user32" (ByVal hMenu As Long , ByVal wFlags As Long, ByVal x As Long, ByVal y As Long, ByVal nReserved As Long, ByVal hwnd As Long, nIgnored As Long) As LongPrivate Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As LongPrivate Declare Function GetCursorPos LIB "User32" (LPPOINT AS POINT) AS LongPrivate Declare Function GetForeGroup "User32" () As longprivate declare function getmenus ring Lib "user32" Alias ​​"GetMenuStringA" (ByVal hMenu As Long, ByVal wIDItem As Long, ByVal lpString As String, ByVal nMaxCount As Long, ByVal wFlag As Long) As LongPrivate mSelMenuString As StringPublic Property Get SelMenuString () As String SelMenuString = mSelMenuStringEnd Property'Public Function Popup (paramarray param ()) AS Long Dim Imenu As Long Dim Hmenu As Long Dim Nmenus As long 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 = 1 To Nmenus' The appendmenu function has been superseeded by the insertmenuitem' function, but it is a bit Easier to use. if trim $ (param (iMenu - 1))) = "-" THEN 'IF The Parameter Is A Single Dash, A Separetor Is Drawn Appendmenu Hmenu, MF_SEPARATOR, IMENU, "Else Appendmenu HMENU, MF_STRINU HMENU, MF_STRING MF_ENABED, IMENU, CSTR (Param (IMenu - 1)) End if 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, px, py, 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

'The following is an example, add a listbox control on the Form

Option expedition

Private sub flow_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

转载请注明原文地址:https://www.9cbs.com/read-5542.html

New Post(0)