Rightclick Listview 
Author Message
 Rightclick Listview
Hi all

Is it possible to capture a mouse rightclick in the header of the ListView


Tue, 24 Apr 2001 03:00:00 GMT  
 Rightclick Listview

>Hi all

>Is it possible to capture a mouse rightclick in the header of the ListView


 It is possible, it's also a bit tricky unless you know about the WinAPI and

I posted this here about a month or two ago, but it can't hurt (can it?) to
post it again...


The code here show's how to stop the resizing of certain column headers as
well, it's pretty well documented so you shouldn't have too much trouble
with it.

Make sure that you have a good subclassing control (Get the free one from
http://www.softcircuits.com), or rewrite it (I certainly would in a full
app) using addressof processing.

Anyway, here goes :

Take one empty form, add a ListView (ListView1), Subclassing control
(SubClass1),  and paste in the following code :

Option Explicit
' Subclassing example for Listview
' Restrict Column 0/1 resizing and detect right mouse click in column
' Chris Eastwood 1998

' Point structure to get mouse co-ords
Private Type POINTAPI
    x As Long
    y As Long
End Type

' Header Control Hit-Test Structure
    pt As POINTAPI
    flags As Long
    iItem As Long
End Type

' Header / Notification Structures
Private Type NMHDR
    hwndFrom As Long
    idFrom As Long
    code As Long
End Type

Private Type NMHEADER
    hdr As NMHDR
    iItem As Integer
    iButton As Integer
    pitem As Long
End Type

' API Call Declarations
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As
Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long,
lpPoint As POINTAPI) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As
Any) As Long
Private Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" _
        (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
' API Messages
Private Const WM_NOTIFY = &H4E
Private Const HDM_FIRST = &H1200
Private Const HDM_HITTEST = HDM_FIRST + 6
Private Const HDN_FIRST = -300                      ' Header Notification
Private Const HDN_BEGINTRACKA = (HDN_FIRST - 6)     ' Resizing Message
Private Const HDN_BEGINTRACKW = (HDN_FIRST - 26)    ' Resizing Message
Private Const NM_FIRST = 0
Private Const NM_RCLICK = NM_FIRST - 5              ' Standard Right Click
Notify Message

Private Sub Form_Load()
' Set the listview in report mode and add some dummy column headers
    ListView1.View = lvwReport

    With ListView1.ColumnHeaders
        .Add , , "This"
        .Add , , "Is"
        .Add , , "A"
        .Add , , "Test"
    End With

    With Subclass1
        .hwnd = ListView1.hwnd
        .Messages(WM_NOTIFY) = True
    End With

End Sub

Private Sub Subclass1_WndProc(Msg As Long, wParam As Long, lParam As Long,
Result As Long)
    Dim nNotify As NMHEADER
    Dim lResult As Long
    Dim lColumnRightClickedOn As Long

    CopyMemory nNotify, ByVal lParam, Len(nNotify)

    Select Case nNotify.hdr.code
        'Notification.iItem in this case
        'contains the index of the column being resized
        '(starting at 0).  If you want to stop
        'the column from being resized, set the result of
        'this message to True, otherwise let it process
            If nNotify.iItem = 0 Then  ' dont allow col 0 resize
                Result = True
            End If

        Case NM_RCLICK
            ' Get current cursor position
            GetCursorPos htInfo.pt
            ' Convert it into relevant co-ords to the header
            ScreenToClient nNotify.hdr.hwndFrom, htInfo.pt
            lColumnRightClickedOn = SendMessage(nNotify.hdr.hwndFrom,
HDM_HITTEST, 0, htInfo)
            Me.Caption = "Right Click on Column " & lColumnRightClickedOn
            ' If lColumnRightClickedOn < 0 then an either
            ' an error occurred, or no column was clicked on
        Case Else

            Result = Subclass1.CallWndProc _
                        (Msg, wParam, lParam)
    End Select

End Sub

Hope that helps you on your way


Chris Eastwood
Software Engineer
ACNielsen Ltd


Tue, 24 Apr 2001 03:00:00 GMT  
 [ 2 post ] 

 Relevant Pages 

1. Rightclick in listview

2. RightClick on ListView ColumnHeader

3. VB4.0 ListView RightCLick

4. Rightclick in listview

5. ListView RightClick on ColumnHeader

6. RightClick Menu Versus Customized UI File(.vsu)

7. RightClick->Goto Difinition

8. How do you create a rightclick popup menu?

9. explorers rightclick menu...

10. Rightclick menu

11. RightClick a TextBox

12. MSflexgrid, how to determine which cell received Rightclick


Powered by phpBB® Forum Software