Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: VB5,VB6
Expertise: Advanced
Jul 1, 2000



Application Security Testing: An Integral Part of DevOps

Prevent dragging elements in a ListView control

The ListView control doesn't expose any property that lets you disable the dragging of its elements. To do so, you must trap the WM_NOTIFY message that the ListView control sends its parent form when the drag operation begins, and "eat" it. Using the MSGHOOK.DLL subclassing library it's easy to accomplish it:


Const WM_NOTIFY = &H4E
Const LVN_FIRST = -100&

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As _
    Any, source As Any, ByVal bytes As Long)

Private Type NMHDR
   hwndFrom As Long
   idFrom As Long
   code As Long
End Type

Dim WithEvents FormHook As MsgHook

Private Sub Form_Load()
    ' start subclassing the current form
    Set FormHook = New MsgHook
    FormHook.StartSubclass Me

    ' fill the ListView1 control with data
    ' ... (omitted) ...
End Sub

' this event fires when the form is sent a message

Private Sub FormHook_BeforeMessage(uMsg As Long, wParam As Long, lParam As Long, _
    retValue As Long, Cancel As Boolean)
    ' the ListView might be notifying something to its parent form
    If uMsg = WM_NOTIFY Then
        ' copy the MNHDR structure pointed
        ' to by lParam to a local UDT
        Dim nmh As NMHDR
        CopyMemory nmh, ByVal lParam, Len(nmh)

        ' check whether the notification is from the ListView1 control
        ' and whether it's the beginning of a drag operation
        If nmh.hwndFrom = ListView1.hWnd And nmh.code = LVN_BEGINDRAG Then
            ' yes, cancel this operation
            retValue = 1
            Cancel = True
        End If
    End If
End Sub

Francesco Balena
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date