Dynamic establishment of pop-up menu technology and its application example

zhaozj2021-02-16  55

Establish a standard EXE new project to add a listbox and a Text control on the form.

Then paste the following code in the form: (Form code)

Option expedition

Private Sub Form_Load () list1.additem "Right-click here" End Sub

Private Sub List1_MouseUp (Button As Integer, Shift As Integer, x As Single, y As Single) Dim cMenu As cPopupMenu Dim ret As Long If Button = vbRightButton Then Set cMenu = New cPopupMenu ret = cMenu.Popup ( "undo", "- "," Cut "," copy "," paste "," delete ") text1.text = RET END IFEND SUB

Then add a new class in the project, name: cpopupmenu and paste the following code in the code form of class cpopupmenu:

Option expedition

Private Type Point X As Long Y As Longend Type

Private Const MF_ENABLED = & H0 & Private Const MF_DISABLED = & H2 & 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 GetCursorpos LIB "User32" (LPPONT AS POINT) AS LongPrivate Declare Function GetForeGroup "User32" () AS Long

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 longpublic function popup (paramarray param ()) AS Long Dim iMenu As long Dim HMENU As Long Dim Nmenus As long Dim P AS Point getCursorpos P 'Get the current cursor position hmenu = createpopupmenu () Create an empty pop-up Menu nmenus = 1 ubound (param) 'Get menu Number for iMenu = 1 To Nmenus if Trim $ (CSTR (Param (IMenu - 1))) = "-" The' Draw Split Symbol Appendmenu Hmenu, MF_SEPArator, Imenu, " "Else appendmenu HMENU, MF_STRING MF_ENABED, IMENU, CSTR (param (iMenu - 1)) End If Next iMenu 'TrackPopupMenu function returns the menu item selected by the user or deselect iMenu = TrackPopupMenu (hMenu, TPM_RIGHTBUTTON TPM_LEFTALIGN TPM_NONOTIFY TPM_RETURNCMD, px, py, 0, GetForegroundWindow (), 0) DestroyMenu hMenu' release hMenu Popup = iMenu ' Return the selected menu index number, return to 0nd function

Then save the project, press F5 to run, click to see if there is a menu, is there any menu?

--- By Tang Dang Gang 2004-7-9

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

New Post(0)