Popup-Menue (Ls)

' Pop a menu at coordinates mx, my (pixels)
' or current cursor position if 0,0
' pstrItem is a semicolon-delimited string
' ie: "Item 1;Item 2;-;Item 3"
' A separator is created from a '-'
' Use of ampersand to underline character is Ok
' Returns number of item chosen, or 0 if user
' clicks off the menu or presses <Esc>

Option Public

Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

x As Long
y As Long
End Type

Type MSG
hwnd As Long
message As Long
wParam As Long
lParam As Long
time As Long
End Type

Declare Function GetCursorPos Lib "user32" Alias "GetCursorPos" (lpPoint As
Declare Function CreatePopupMenu Lib "user32" Alias "CreatePopupMenu" () As Long
Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (Byval hMenu As
Long, Byval wFlags As Long, Byval wIDNewItem As Integer, Byval lpNewItem As
Any) As Long
Declare Function TrackPopupMenu Lib "user32" Alias "TrackPopupMenu" (Byval
hMenu As Long, Byval wFlags As Long, Byval x As Long, Byval y As Long, Byval
nReserved As Long, Byval hwnd As Long, lprc As Rect) As Long
Declare Function DestroyMenu Lib "user32" Alias "DestroyMenu" (Byval hMenu As
Long) As Long
Declare Function GetMessage Lib "user32" Alias "GetMessageA" (lpMsg As MSG,
Byval hwnd As Long, Byval wMsgFilterMin As Long, Byval wMsgFilterMax As Long)
As Long
Declare Function GetActiveWindow Lib "user32" Alias "GetActiveWindow" () As Long

Function PopMenu (pstrItem As String, mx As Long, my As Long) As Long

Const MF_ENABLED = &H0
Const MF_SEPARATOR = &H800
Const SEP = ";"
Dim msgdata As MSG
Dim rectdata As RECT
Dim Cursor As POINTAPI
Redim strItem(1 To 20) As String
Dim i As Long
Dim j As Long
Dim last As Long
Dim hMenu As Long
Dim id As Integer
Dim junk As Long

If Right$(pstrItem, 1) <> SEP Then pstrItem = pstrItem + SEP
j = 1
i = Instr(j, pstrItem, SEP)
If i Then
last = last + 1
strItem(last) = Mid$(pstrItem, j, i - j)
j = i + 1
End If
Loop Until i = 0

hMenu = CreatePopupMenu()

id = 1
For i = 1 To last
If strItem(i) <> "-" Then
junk = AppendMenu(hMenu, MF_ENABLED, id, strItem(i))
id = id + 1
junk = AppendMenu(hMenu, MF_SEPARATOR, 0, "")
End If

If mx = 0 And my = 0 Then
Call GetCursorPos(Cursor)
mx = Cursor.x
my = Cursor.y
End If

junk = TrackPopupMenu(hMenu, TPM_LEFTALIGN, mx, my, 0, GetActiveWindow(),
junk = GetMessage(msgdata, GetActiveWindow(), 0, 0)

i = Abs(msgdata.wparam)
If msgdata.message = 273 Then
PopMenu = i
End If
Call DestroyMenu(hMenu)
End Function
Sub Initialize

End Sub

This was first published in November 2000

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.