PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Creating and displaying popup menu  (Read 2405 times)

Paul Squires

  • Administrator
  • Guru Member
  • *****
  • Posts: 9497
  • Windows 10
    • PlanetSquires Software
Creating and displaying popup menu
« on: October 31, 2009, 09:36:17 PM »

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: [Select]

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
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer