Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitlePrevent the user from dragging icons in a ListView
DescriptionThis example shows how to prevent the user from dragging icons in a ListView in Visual Basic.
KeywordsListView, drag, drag items, subclass
CategoriesControls, API
 
Normally the user can drag items around in a ListView's icon or small icon views. This example prevents the use from moving items.

When it starts, the program subclasses the form containing the ListView control. Now when Windows sends the form a message, the NewWindowProc function executes to handle it.

 
Private Sub Form_Load()
Dim i As Integer

    ' Make some ListView items.
    Set ListView1.SmallIcons = imlIcons
    Set ListView1.Icons = imlIcons
    For i = 1 To 10
        ListView1.ListItems.Add , , "Item " & i, 1, 1
    Next i

    ' Subclass.
    ' (Comment this out to see the normal behavior.)
    OldWindowProc = SetWindowLong( _
        hwnd, GWL_WNDPROC, _
        AddressOf NewWindowProc)
End Sub
 
Subroutine NewWindowProc first checks for the WM_NCDESTROY message. It receives this message when the form is being destroyed. At that time, the routine restores the form's original WindowProc so it can close normally.

The routine next checks for the WM_NOTIFY message. If it gets this message, the form copies the associated data into an NMHDR structure and checks the code field for the value LVN_BEGINDRAG, which indicates that the user is starting to drag an item. If it see this message, the new WindowProc ignores it.

The routine passes all other messages to the form's original WindowProc for normal processing.

 
' Display message names.
Public Function NewWindowProc(ByVal hwnd As Long, ByVal msg _
    As Long, ByVal wParam As Long, ByVal lParam As Long) As _
    Long
Const WM_NCDESTROY = &H82
Const WM_NOTIFY = &H4E
Const LVN_FIRST = -100&
Const LVN_BEGINDRAG = (LVN_FIRST - 9)

Dim nm_hdr As NMHDR

    ' If we're being destroyed,
    ' restore the original WindowProc.
    If msg = WM_NCDESTROY Then
        SetWindowLong _
            hwnd, GWL_WNDPROC, _
            OldWindowProc
    ElseIf msg = WM_NOTIFY Then
        ' Copy info into the NMHDR structure.
        CopyMemory nm_hdr, ByVal lParam, Len(nm_hdr)

        ' See if this is the start of a drag.
        If nm_hdr.code = LVN_BEGINDRAG Then
            ' A drag is beginning. Ignore this event.
            ' Indicate we have handled this.
            NewWindowProc = 1
            ' Do nothing else.
            Exit Function
        End If
    End If

    NewWindowProc = CallWindowProc( _
        OldWindowProc, hwnd, msg, wParam, _
        lParam)
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated