Handling right-click events in ListView column header

Started by Jean-pierre Leroy, February 20, 2015, 06:28:42 AM

Previous topic - Next topic

Jean-pierre Leroy

I know how to handle right-clik events in a listview control with the WM_CONTEXTMENU message.

I would like to know if there is a solution to handle the right-click events specifically in the ListView column header.

My goal is to display two distinct menus: one menu when the user right-click on the ListView and a specific one when the user right-click on the ListView header.

Is-it possible to do that ?

Regards,
Jean-Pierre

Jean-pierre Leroy

Dear FF Users, finally I found an elegant solution for that.

Here is the code I put in the custom handler of the form containing the ListView; I hope that would be useful for other programmers.


'--------------------------------------------------------------------------------
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   
    Local P_Info      As LV_HitTestInfo
   
    Select Case wMsg                           
                               
        Case %WM_CONTEXTMENU
       
            If wParam = HWND_FORM1_LISTVIEW1 Then
           
                ' get screen position in screen coordinates
                P_Info.pt.x = LoWrd(lParam) 
                P_Info.pt.y = HiWrd(lParam)
     
                ' convert to ListView coordinates
                ScreenToClient(wParam, P_Info.pt)
               
                ' Determines which list-view item or subitem is located at a given position
                ListView_SubItemHitTest(wParam, P_Info)                                   
               
                '-----------------------
                ' on the ListView header
                '-----------------------
                If P_Info.iItem = - 1 And P_Info.iSubItem <> - 1 Then
                    If hPopupMenu Then DestroyMenu hPopupMenu                   
                    hPopupMenu = CreatePopupMenu()
                    AppendMenu hPopupMenu, %MF_STRING, %IDC_POPUPMENU_HEADERMENU_OPTION1, "Save user column order"
                    AppendMenu hPopupMenu, %MF_STRING, %IDC_POPUPMENU_HEADERMENU_OPTION2, "Restore user column order"
                    AppendMenu hPopupMenu, %MF_SEPARATOR, 0, ""
                    AppendMenu hPopupMenu, %MF_STRING, %IDC_POPUPMENU_HEADERMENU_OPTION3, "Restore default column order"                   
                    TrackPopupMenu hPopupMenu, %TPM_LEFTALIGN Or %TPM_LEFTBUTTON, LoWrd(lParam), HiWrd(lParam), 0, hWndForm, ByVal %Null                                     
                End If                                               
               
                '------------------------
                ' on the ListView content
                '------------------------
                If P_Info.iItem <> - 1 Then                                                                                       
                    If hPopupMenu Then DestroyMenu hPopupMenu                   
                    hPopupMenu = CreatePopupMenu()
                    AppendMenu hPopupMenu, %MF_STRING, %IDC_POPUPMENU_SINGLEROW_OPTION1, "Menu for selected row - Option1"
                    AppendMenu hPopupMenu, %MF_STRING, %IDC_POPUPMENU_SINGLEROW_OPTION2, "Menu for selected row - Option2"
                    AppendMenu hPopupMenu, %MF_SEPARATOR, 0, ""
                    AppendMenu hPopupMenu, %MF_STRING, %IDC_POPUPMENU_SINGLEROW_OPTION3, "Menu for selected row - Option3"                   
                    TrackPopupMenu hPopupMenu, %TPM_LEFTALIGN Or %TPM_LEFTBUTTON, LoWrd(lParam), HiWrd(lParam), 0, hWndForm, ByVal %Null                               
                End If                                                                 
                                                                                               
            End If                     
               
    End Select
   
End Function