; $aItems[0] is the number of the items
; "" = separator
Local $aItems[6] = [5, "Hello!", "AutoIt Script", "Popup", "", "Cancel"]
$iSel = _Popup($aItems)
MsgBox(64, "Selection", $aItems[$iSel])
Exit
Func _Popup($aItems, $hWnd = 0, $iX = Default, $iY = Default)
; Get the handle of the AutoIt window if $hWnd is 0
Local $hWndBg
If IsHWnd($hWnd) Then
$hWndBg = $hWnd
Else
$hWndBg = WinGetHandle(AutoItWinGetTitle())
EndIf
If IsKeyword($iX) Then $iX = MouseGetPos(0)
If IsKeyword($iY) Then $iY = MouseGetPos(1)
Local $hPopup = DllCall("user32.dll", "handle", "CreatePopupMenu")
If @error Then Return SetError(1, 0, 0)
$hPopup = $hPopup[0]
For $i = 1 To $aItems[0]
Local $iFlag = 0 ; MF_STRING = 0x00000000
If $aItems[$i] == "" Then $iFlag = 0x00000800 ; MF_SEPARATOR = 0x00000800
; Actually, InsertMenuItem is better than AppendMenu
Local $aRet = DllCall("user32.dll", "bool", "AppendMenuW", "handle", $hPopup, "uint", $iFlag, "uint", $i, "wstr", $aItems[$i])
If @error Then Return SetError(2, $i, 0)
Next
; 0x0182 = TPM_NONOTIFY + TPM_RETURNCMD + TPM_RIGHTBUTTON
Local $aSel = DllCall("user32.dll", "bool", "TrackPopupMenuEx", "handle", $hPopup, "uint", 0x0182, "int", $iX, "int", $iY, "hwnd", $hWndBg, "ptr", 0)
If @error Then Return SetError(3, 0, 0)
Local $aRet = DllCall("user32.dll", "bool", "DestroyMenu", "handle", $hPopup)
If @error Then Return SetError(4, 0, 0)
Return $aSel[0]
EndFunc ;==>_Popup