-
Nov 20th, 2011, 09:34 AM
#1
Thread Starter
Addicted Member
VB6 - Prevent ListView selection/highlight
Ever wanted to display a report using the ListView control, but didn't want to do anything with the data? Obviously, the highlight effect is a bit annoying.
Of course, someone would say, "Why not use a custom routine, which includes, say, a picturebox?" Well, if the data didn't fit in the form/screen, a scrollbar would be required.
Now, to the point.
After searching around the web, I couldn't find a suitable code for this situation.
Then I realized that subclassing was necessary, so I searched the MSDN library and I found that the appropriate message was LVN_ITEMCHANGING, which is sent when the selection is about to change (obviously).
Enough talking. Let's see the code:
vb Code:
'I have excluded the subclassing code, since there are plenty of examples around the internet.
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpDest As Any, lpSource As Any, ByVal cBytes As Long)
Private Const WM_NOTIFY As Long = &H4E
Private Const LVN_FIRST As Long = -100
Private Const LVN_ITEMCHANGING As Long = LVN_FIRST
Private Const LVIS_SELECTED As Long = &H2
Public Type POINTAPI
X As Long
Y As Long
End Type
Public Type NMHDR
hWndFrom As Long
idfrom As Long
code As Long
End Type
Private Type NMLISTVIEW
hdr As NMHDR
iItem As Long
iSubItem As Long
uNewState As Long
uOldState As Long
uChanged As Long
ptAction As POINTAPI
lParam As Long
End Type
Public Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim nmlv As NMLISTVIEW
If uMsg = WM_NOTIFY Then
CopyMemory nmlv, ByVal lParam, Len(nmlv) 'lParam contains a pointer to NMLISTVIEW structure. Copy it to a local variable.
If nmlv.hdr.code = LVN_ITEMCHANGING Then
If (nmlv.uNewState And LVIS_SELECTED) <> 0 Then 'uNewState is a bit mask
WindowProc = 1 'According to MSDN, if the application processes the message, the function should return TRUE.
Exit Function
End If
End If
End If
WindowProc = CallWindowProc(OldWndProc, hWnd, uMsg, wParam, lParam)
End Function
Explanation
We subclass the parent form and capture the WM_NOTIFY message. Then, we wait for the notification code LVN_ITEMCHANGING, which is sent just before the highlight changes. The rest of the code is obvious.
The result will be a "selectionless" listview. However, mouse events, such as MouseDown, will still fire.
Of course, the SelectedItem property will always be Nothing.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|