Displays a shortcut menu at the specified location and tracks the selection of items on the shortcut menu. The shortcut menu can appear anywhere on the

screen.
Code:
    Const MF_STRING As Int32 = 0
    Const MF_GRAYED As Int32 = 1
    Const MF_DISABLED As Int32 = 2
    Const MF_CHECKED As Int32 = 8
    Const MF_APPEND As Int32 = 256
    Const MF_SEPARATOR As Int32 = 2048
    Const TPM_LEFTALIGN As Int32 = 0
    Const TPM_RIGHTBUTTON As Int32 = 2
    Const TPM_RETURNCMD As Int32 = 256
    Private Structure POINTAPI
        Public x, y As Int32
    End Structure
    Private Declare Function apiCreatePopupMenu Lib "user32" Alias "CreatePopupMenu" () As Int32
    Private Declare Function apiTrackPopupMenuEx Lib "user32" Alias "TrackPopupMenuEx" (ByVal hMenu As Int32, ByVal wFlags As Int32, ByVal x As Int32, ByVal y As Int32, ByVal hWnd As Int32, ByVal lptpm As Int32) As Int32
    Private Declare Function apiAppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Int32, ByVal wFlags As Int32, ByVal wIDNewItem As Int32, ByVal lpNewItem As String) As Int32
    Private Declare Function apiDestroyMenu Lib "user32" Alias "DestroyMenu" (ByVal hMenu As Int32) As Int32
    Private Declare Function apiGetCursorPos Lib "user32" Alias "GetCursorPos" (ByRef lpPoint As POINTAPI) As Boolean
    Private Declare Function apiMoveWindow Lib "user32" Alias "MoveWindow" (ByVal hWnd As Int32, ByVal x As Int32, ByVal y As Int32, ByVal nWidth As Int32, ByVal nHeight As Int32, ByVal bRepaint As Boolean) As Boolean
    Private hMenu As Int32
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = "Right click on the form"
    End Sub
    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
        If e.Button = Windows.Forms.MouseButtons.Right Then
            Dim ret As Int32 = PopupMenu(Me.Handle.ToInt32)
            If ret = 1 Then
                MessageBox.Show("New File Clicked")
            ElseIf ret = 4 Then
                MessageBox.Show("Exit Clicked")
                Me.Close()
            End If
        End If
    End Sub
    Public Function PopupMenu(ByVal hWnd As Int32) As Int32
        Dim ret As Int32, p As New POINTAPI
        hMenu = apiCreatePopupMenu()
        apiGetCursorPos(p)
        apiMoveWindow(hMenu, p.x, p.y, 200, 200, True)
        apiAppendMenu(hMenu, MF_STRING, 1, "New File")
        apiAppendMenu(hMenu, MF_GRAYED Or MF_DISABLED, 2, "Open File")
        apiAppendMenu(hMenu, MF_SEPARATOR, 3, 0)
        apiAppendMenu(hMenu, MF_CHECKED, 4, "Exit")
        ret = apiTrackPopupMenuEx(hMenu, TPM_LEFTALIGN Or TPM_RETURNCMD Or TPM_RIGHTBUTTON, p.x, p.y, hWnd, 0)
        apiDestroyMenu(hMenu)
        Return ret
    End Function