Support Forums > General Board

Creating and displaying popup menu


Paul Squires:
A common task in programming is to display a popup menu when you right-click on some area of a control. There are various ways to do this but responding to the WM_CONTEXTMENU message is usually a good approach. Creating the popup menu is pretty easy. However, you do need to define your own equates to identify each menu option.

Check out this simple example that displays a menu when right-clicked on a ListView.

--- Code: ---%IDC_POPUPMENU_OPTION1 = %WM_USER + 100

Function FORM1_CUSTOM ( _
                      hWndForm      As Dword, _  ' handle of Form
                      wMsg          As Long,  _  ' type of message
                      wParam        As Dword, _  ' first message parameter
                      lParam        As Long   _  ' second message parameter
                      ) As Long

    Static hPopupMenu As Dword
    Select Case wMsg
       If wParam = HWND_FORM1_LISTVIEW1 Then
          If hPopupMenu Then DestroyMenu hPopupMenu
          hPopupMenu = CreatePopupMenu()
              AppendMenu hPopupMenu, %MF_STRING, %IDC_POPUPMENU_OPTION1, "Option1"
              AppendMenu hPopupMenu, %MF_STRING, %IDC_POPUPMENU_OPTION2, "Option2"
              AppendMenu hPopupMenu, %MF_SEPARATOR, 0, ""
              AppendMenu hPopupMenu, %MF_STRING, %IDC_POPUPMENU_OPTION3, "Option3"
          TrackPopupMenu hPopupMenu, %TPM_LEFTALIGN Or %TPM_LEFTBUTTON, _
                                       LoWrd(lParam), HiWrd(lParam), 0, hWndForm, ByVal %Null
       End If
   End Select
End Function

Function FORM1_WM_COMMAND ( _
                          hWndForm     As Dword, _  ' handle of Form
                          hWndControl  As Dword, _  ' handle of Control
                          wNotifyCode  As Long,  _  ' notification code
                          wID          As Long   _  ' item, control, or accelerator identifer
                          ) As Long

   Select Case wID
         MsgBox "option 1 selected"
         MsgBox "option 2 selected"
         MsgBox "option 3 selected"
   End Select
End Function

--- End code ---


[0] Message Index

Go to full version