Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Displaying pop-up menu on right-click ListView ColumnHeader

5 views
Skip to first unread message

Peter Kashatus

unread,
Sep 21, 1998, 3:00:00 AM9/21/98
to
I need to display a pop-up menu when the user right-clicks the column
headers of a ListView control. Any suggestions.

Thanks,
Pete


Bertie Wooster

unread,
Sep 22, 1998, 3:00:00 AM9/22/98
to
Peter Kashatus wrote in message <3606BC41...@softworkscc.com>...

>I need to display a pop-up menu when the user right-clicks the column
>headers of a ListView control. Any suggestions.


Peter, search in DejaNews for a posting by Brad Martinez dated 22nd August
1998 called "ListView: How to detect right-click in columnheader". This was
an outline plan for doing it using subclassing which I haven't had time to
try yet. Let's make a pact: whoever gets it to work first posts the code!

Bertie

J. Giudice

unread,
Sep 22, 1998, 3:00:00 AM9/22/98
to
I may unclear on what you are doing, but can't you put a routine into
the event mouseDown for the list view and then bring up your pop up
menu. This is what I did when I needed to control it:

Private Sub lv1_MouseDown(Button As Integer, Shift As Integer, x As
Single, y As Single)
If Button = vbRightButton Then
If y < lv1.RowHeight(0) Then 'Column header was
clicked
'Your code here to do prep work if desired
frmMain.PopupMenu popMyMenu
End If
End If
End Sub

John Giudice
(Visit my VB Tips for other related ideas at:
http://www2.ultra.net/~goodidea/Technology-Info/tips-vb.htm)

++++++++++++++++++++++++++++++++++++++++++++++++++++++
Bertie Wooster wrote in message ...

Bertie Wooster

unread,
Sep 23, 1998, 3:00:00 AM9/23/98
to
If you click over the listview headers no events are generated -- that's the
problem. We hope that by subclassing we can react to the windows messages.

Bertie
PS I tried it with the new-fangled ListView in VB6, which unfortunately
shows the same behaviour.


J. Giudice wrote in message <6u9mel$ks2$1...@strato.ultra.net>...

Bertie Wooster

unread,
Sep 23, 1998, 3:00:00 AM9/23/98
to
For "click" read "right-click". Apologies!

Chris Eastwood

unread,
Sep 23, 1998, 3:00:00 AM9/23/98
to
Hi Guys

I've just answered another question in the API newsgroup about restricting
the resizing of certain column headers when I 'stumbled' (probably a good
word for it) accross how to do exactly what you want !

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
headers
'
' 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
'
Private Type HDHITTESTINFO
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
Long
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
Message
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 ptAPI As POINTAPI
Dim htInfo As HDHITTESTINFO
Dim lColumnRightClickedOn As Long

CopyMemory nNotify, ByVal lParam, Len(nNotify)

Select Case nNotify.hdr.code
Case HDN_BEGINTRACKA, HDN_BEGINTRACKW
'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
'normally.
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

Regards

Chris Eastwood
Software Engineer
ACNielsen Ltd

Bertie Wooster

unread,
Sep 23, 1998, 3:00:00 AM9/23/98
to
Excellent, Chris -- much appreciated.

Bertie


Chris Eastwood wrote in message ...

0 new messages