Bay Six Software Forum Index Bay Six Software
Beyond the Basics
 
 FAQFAQ   SearchSearch   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

WMLiberty Demos - Double-clicks in a ListView
 
WMLiberty -> Demos -> API-Created Controls -> Double-clicks in a listview (Download)

Code:
' Trapping double-clicks in a listview
' By Brent D. Thorn, 12/2007
' PUBLIC DOMAIN

    'Define a few structs
    Struct LVITEM, _
        mask As ULong, _
        iItem As Long, _
        iSubItem As Long, _
        state As ULong, _
        stateMask As ULong, _
        pszText As Ptr, _
        cchTextMax As Long, _
        iImage As Long, _
        lParam As Long, _
        iIndent As Long
    Struct NMHDR, _
        hwndFrom As ULong, _
        idFrom As ULong, _
        code As Long
    Struct NMITEMACTIVATE, _
        hdr.hwndFrom As ULong, _
        hdr.idFrom As ULong, _
        hdr.code As Long, _
        iItem As Long, _
        iSubItem As Long, _
        uNewState As ULong, _
        uOldState As ULong, _
        uChanged As ULong, _
        ptAction.x As Long, _
        ptAction.y As Long, _
        lParam As Long, _
        uKeyFlags As ULong

    Open "WMLiberty" For DLL As #wmlib

    Open "Listview NM_DBLCLK Demo" For Window As #1
    #1 "TrapClose Quit"

    'Get instance handle
    CallDLL #kernel32, "GetModuleHandleA", _
        _NULL As Long, _
        hInst As Long
    'Create listview control
    hLV = CreateWindowEx(_WS_EX_CLIENTEDGE, "SysListview32", "", _
                         _WS_CHILD Or _WS_VISIBLE Or 3, _
                         5, 5, 300, 300, _
                         HWnd(#1), 0, hInst, 0)

    'Add a few items to play with
    For i = 0 To 4
        Call ListView.InsertItem hLV, i, "Double-click me"
    Next

    'Set up "notify" event trapping
    Callback lpfn, OnNotify( ULong, ULong, ULong, ULong ), Long
    Call SetWMHandler HWnd(#1), _WM_NOTIFY, lpfn, -1

    Call DoEvents
Sub DoEvents
[Loop]
    Scan
    CallDLL #kernel32, "Sleep", 50 As Long, r As Void
    GoTo [Loop]
End Sub
Sub Quit Me$
    Close #Me$
    Close #wmlib
    End
End Sub

'Callback function to handle WM_NOTIFY messages
Function OnNotify( hWnd, uMsg, wParam, lParam )
    NMHDR.struct = lParam
    Select Case NMHDR.code.struct
    Case -3 'NM_DBLCLK
        NMITEMACTIVATE.struct = lParam
        iItem = NMITEMACTIVATE.iItem.struct
        If iItem < 0 Then
            Print "Double-clicked listview background"
        Else
            'For some reason, placing a NOTICE here causes problems.
            Print "Double-clicked item #"; iItem
        End If
    End Select
End Function

'*** Wrapper Functions ***
Function CreateWindowEx( dwExStyle, ClassName$, lpWindowName$, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam )
    CallDLL #user32, "CreateWindowExA", _
        dwExStyle As ULong, _
        ClassName$ As Ptr, _
        lpWindowName$ As Ptr, _
        dwStyle As ULong, _
        x As Long, _
        y As Long, _
        nWidth As Long, _
        nHeight As Long, _
        hWndParent As ULong, _
        hMenu As ULong, _
        hInstance As ULong, _
        lpParam As ULong, _
        CreateWindowEx As ULong
End Function
Sub ListView.InsertItem hWnd, iItem, Text$
    LVITEM.mask.struct = 1 'LVIF_TEXT
    LVITEM.iItem.struct = iItem
    LVITEM.iSubItem.struct = 0
    LVITEM.pszText.struct = Text$
    CallDLL #user32, "SendMessageA", _
        hWnd As long, _
        4103 As long, _ 'LVM_INSERTITEM
        0 As long, _
        LVITEM As struct, _
        r As long
End Sub
Sub SetWMHandler hWnd, uMsg, lpfnCB, lSuccess
    CallDLL #wmlib, "SetWMHandler", _
        hWnd As ULong, _
        uMsg As ULong, _
        lpfnCB As ULong, _
        lSuccess As Long, _
        r As Long
End Sub

Powered by phpBB © 2001, 2005 phpBB Group