cPopupMenu.cls
上传用户:hyb6888
上传日期:2016-01-24
资源大小:5186k
文件大小:3k
- VERSION 1.0 CLASS
- BEGIN
- MultiUse = -1 'True
- Persistable = 0 'NotPersistable
- DataBindingBehavior = 0 'vbNone
- DataSourceBehavior = 0 'vbNone
- MTSTransactionMode = 0 'NotAnMTSObject
- END
- Attribute VB_Name = "cPopupMenu"
- Attribute VB_GlobalNameSpace = False
- Attribute VB_Creatable = True
- Attribute VB_PredeclaredId = False
- Attribute VB_Exposed = False
- '类的名称为 cPopupMenu
- Option Explicit
- Private Type POINT
- x As Long
- y As Long
- End 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 Long
- Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal sCaption As String) As Long
- Private 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 Long
- Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
- Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINT) As Long
- Private Declare Function GetForegroundWindow Lib "user32" () As Long
- Private Declare Function GetMenuString 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 Long
- Private mSelMenuString As String
- Private mSelID As Long
- '得到所选择的菜单的字串
- Public Property Get SelMenuString() As String
- SelMenuString = mSelMenuString
- End Property
- Public Property Get SelID() As String
- SelID = mSelID
- End Property
- '返回所选菜单项
- Public Function Popup(ParamArray param()) As Long
- Dim iMenu As Long
- Dim hMenu As Long
- Dim nMenus As Long
- Dim p As POINT
- GetCursorPos p
- hMenu = CreatePopupMenu()
- nMenus = 1 + UBound(param)
- For iMenu = 1 To nMenus
- If Trim$(CStr(param(iMenu - 1))) = "-" Then
- AppendMenu hMenu, MF_SEPARATOR, iMenu, "" '其中第三个参数iMenu供返回时使用
- Else
- AppendMenu hMenu, MF_STRING + MF_ENABLED, iMenu, CStr(param(iMenu - 1))
- End If
- Next iMenu
- 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)
-
- mSelMenuString = Trim(buffer)
- mSelID = iMenu
-
- DestroyMenu hMenu
-
-
- Popup = iMenu
- End Function