Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
XML RSS Feed
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitlePrevent the user from moving items in a ListView control
DescriptionThis example shows how to prevent the user from moving items in a ListView control in Visual Basic 6. It subclasses and discards notify events that indicate the beginning of an item move.
KeywordsListView, move, prevent
CategoriesControls, API
 
When the program starts, it calls Subroutine InstallWindowProc. That routine saves the hWnd of the ListView and its parent form. It then sets the parent form's WindowProc to NewWindowProc.
 
' Install the new WindowProc.
Public Sub InstallWindowProc(ByVal parent_hwnd As Long, _
    ByVal child_hwnd As Long)
    m_ParentHwnd = parent_hwnd
    m_ChildHwnd = child_hwnd

    m_OldWindowProc = SetWindowLong(m_ParentHwnd, _
        GWL_WNDPROC, AddressOf NewWindowProc)
End Sub
 
Subroutine NewWindowProc executes whenever the ListView's form receives an event. If it sees a WM_NCDESTROY message, then the form is being destroyed so the code calls UninstallWindowProc to restore the form's original WindowProc.

If NewWindowProc sees the WM_NOTIFY message, then it converts the parameter lParam into an NMHDR structure. If that structure indicates that the message is from the desired ListView and the message code is LVN_BEGINDRAG, then the program ignores the message.

Otherwise the program passes the message to the original WindowProc for normal processing.

 
' Look for WM_SHOWWINDOW.
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 notify_header As NMHDR

    ' If we're being destroyed,
    ' restore the original WindowProc.
    If msg = WM_NCDESTROY Then
        UninstallWindowProc
    End If

    If msg = WM_NOTIFY Then
        ' Copy lParam into the NMHDR structure.
        MoveMemory notify_header, ByVal lParam, _
            Len(notify_header)

        ' See if the user is starting an item drag.
        If (notify_header.code = LVN_BEGINDRAG) And _
           (notify_header.hwndFrom = m_ChildHwnd) _
        Then
            NewWindowProc = 1
            Exit Function
        End If
    End If

    NewWindowProc = CallWindowProc( _
        m_OldWindowProc, hWnd, msg, wParam, _
        lParam)
End Function
 
Subroutine UninstallWindowProc restores the form's original WindowProc.
 
' Restore the original WindowProc.
Public Sub UninstallWindowProc()
    SetWindowLong m_ParentHwnd, GWL_WNDPROC, m_OldWindowProc
    m_ParentHwnd = 0
    m_ChildHwnd = 0
End Sub
 
 
Copyright © 1997-2003 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated