dcsimg
Results 1 to 3 of 3

Thread: Click Event Treeview VB5 API

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2016
    Posts
    21

    Click Event Treeview VB5 API

    When a Treeview is created using CreateWindowEx in VB5, is there a Click Event that can be captured using the Handle of the CreateWindowEx?

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,843

    Re: Click Event Treeview VB5 API

    You'll have to intercept the hWnd's arriving messages and interpret them.

  3. #3
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    2,401

    Re: Click Event Treeview VB5 API

    Are you familiar with subclassing? (If not, see my sig for a quick introduction)

    You need to subclass the TreeView's parent hWnd, e.g. Form1.hWnd if that's the parent you specified in CreateWindowEx

    Then you want the WM_NOTIFY message
    Declares:
    Code:
    Public Const WM_NOTIFY = &H4E
    Public Const NM_FIRST = -0&   ' (0U-  0U)       ' // generic to all controlsl
    Public Const NM_CLICK = (NM_FIRST - 2)
    
    Public Type NMTREEVIEW   ' was NM_TREEVIEW
      hdr As NMHDR
      ' Specifies a notification-specific action flag.
      ' Is TVC_* for TVN_SELCHANGING, TVN_SELCHANGED, TVN_SETDISPINFO
      ' Is TVE_* for TVN_ITEMEXPANDING, TVN_ITEMEXPANDED
      Action As Long
      itemOld As TVITEM
      itemNew As TVITEM
      ptDrag As POINTAPI
    End Type
    Public Type POINTAPI
            X As Long
            Y As Long
    End Type
    Public Enum TVITEM_Mask
        TVIF_TEXT = &H1
        TVIF_IMAGE = &H2
        TVIF_PARAM = &H4
        TVIF_STATE = &H8
        TVIF_HANDLE = &H10
        TVIF_SELECTEDIMAGE = &H20
        TVIF_CHILDREN = &H40
        TVIF_INTEGRAL = &H80
        '6.0
        TVIF_STATEEX = &H100
        TVIF_EXPANDEDIMAGE = &H200
        
        
        TVIF_DI_SETITEM = &H1000
    End Enum
    Public Enum TVITEM_State
        TVIS_SELECTED = &H2
        TVIS_CUT = &H4
        TVIS_DROPHILITED = &H8
        TVIS_BOLD = &H10
        TVIS_EXPANDED = &H20
        TVIS_EXPANDEDONCE = &H40
        TVIS_EXPANDPARTIAL = &H80
        TVIS_OVERLAYMASK = &HF00
        TVIS_STATEIMAGEMASK = &HF000
        TVIS_USERMASK = &HF000
    End Enum
    Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    Public Type NMHDR
      hWndFrom As Long   ' Window handle of control sending message
      IDFrom As Long        ' Identifier of control sending message
      Code  As Long          ' Specifies the notification code
    End Type
    Public Type TVHITTESTINFO   ' was TV_HITTESTINFO
      pt As POINTAPI
      Flags As TVHT_flags
      hItem As Long
    End Type
    Public Enum TVHT_flags
      TVHT_NOWHERE = &H1   ' In the client area, but below the last item
      TVHT_ONITEMICON = &H2
      TVHT_ONITEMLABEL = &H4
      TVHT_ONITEMINDENT = &H8
      TVHT_ONITEMBUTTON = &H10
      TVHT_ONITEMRIGHT = &H20
      TVHT_ONITEMSTATEICON = &H40
      TVHT_ONITEM = (TVHT_ONITEMICON Or TVHT_ONITEMLABEL Or TVHT_ONITEMSTATEICON)
      
      TVHT_ABOVE = &H100
      TVHT_BELOW = &H200
      TVHT_TORIGHT = &H400
      TVHT_TOLEFT = &H800
      
      ' user-defined
      TVHT_ONITEMLINE = (TVHT_ONITEM Or TVHT_ONITEMINDENT Or TVHT_ONITEMBUTTON Or TVHT_ONITEMRIGHT)
    End Enum
    Public Declare Function GetCursorPos Lib "user32" (lpPoint As Any) As Long
    Public Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As Any) As Long  ' lpPoint As POINTAPI) As Long
    Public Enum TVMessages
      TV_FIRST = &H1100
      
    '#If UNICODE Then
      TVM_INSERTITEMW = (TV_FIRST + 50)
    '#Else
      TVM_INSERTITEM = (TV_FIRST + 0)
    '#End If
      
      TVM_DELETEITEM = (TV_FIRST + 1)
      TVM_EXPAND = (TV_FIRST + 2)
      TVM_GETITEMRECT = (TV_FIRST + 4)
      TVM_GETCOUNT = (TV_FIRST + 5)
      TVM_GETINDENT = (TV_FIRST + 6)
      TVM_SETINDENT = (TV_FIRST + 7)
      TVM_GETIMAGELIST = (TV_FIRST + 8)
      TVM_SETIMAGELIST = (TV_FIRST + 9)
      TVM_GETNEXTITEM = (TV_FIRST + 10)
      TVM_SELECTITEM = (TV_FIRST + 11)
      
    '#If UNICODE Then
      TVM_GETITEMW = (TV_FIRST + 62)
      TVM_SETITEMW = (TV_FIRST + 63)
      TVM_EDITLABELW = (TV_FIRST + 65)
    '#Else
      TVM_GETITEM = (TV_FIRST + 12)
      TVM_SETITEM = (TV_FIRST + 13)
      TVM_EDITLABEL = (TV_FIRST + 14)
    '#End If
      
      TVM_GETEDITCONTROL = (TV_FIRST + 15)
      TVM_GETVISIBLECOUNT = (TV_FIRST + 16)
      TVM_HITTEST = (TV_FIRST + 17)
      TVM_CREATEDRAGIMAGE = (TV_FIRST + 18)
      TVM_SORTCHILDREN = (TV_FIRST + 19)
      TVM_ENSUREVISIBLE = (TV_FIRST + 20)
      TVM_SORTCHILDRENCB = (TV_FIRST + 21)
      TVM_ENDEDITLABELNOW = (TV_FIRST + 22)
      
    '#If UNICODE Then
      TVM_GETISEARCHSTRINGW = (TV_FIRST + 64)
    '#Else
      TVM_GETISEARCHSTRING = (TV_FIRST + 23)
    '#End If
      
      TVM_SETTOOLTIPS = (TV_FIRST + 24)
      TVM_GETTOOLTIPS = (TV_FIRST + 25)
      TVM_SETINSERTMARK = (TV_FIRST + 26)
      TVM_SETUNICODEFORMAT = CCM_SETUNICODEFORMAT
      TVM_GETUNICODEFORMAT = CCM_GETUNICODEFORMAT
      TVM_SETITEMHEIGHT = (TV_FIRST + 27)
      TVM_GETITEMHEIGHT = (TV_FIRST + 28)
      TVM_SETBKCOLOR = (TV_FIRST + 29)
      TVM_SETTEXTCOLOR = (TV_FIRST + 30)
      TVM_GETBKCOLOR = (TV_FIRST + 31)
      TVM_GETTEXTCOLOR = (TV_FIRST + 32)
      TVM_SETSCROLLTIME = (TV_FIRST + 33)
      TVM_GETSCROLLTIME = (TV_FIRST + 34)
      TVM_SETBORDER = (TV_FIRST + 35)
    
      TVM_SETINSERTMARKCOLOR = (TV_FIRST + 37)
      TVM_GETINSERTMARKCOLOR = (TV_FIRST + 38)
      '5.0
      TVM_GETITEMSTATE = (TV_FIRST + 39)
      TVM_SETLINECOLOR = (TV_FIRST + 40)
      TVM_GETLINECOLOR = (TV_FIRST + 41)
      TVM_MAPACCIDTOHTREEITEM = (TV_FIRST + 42)
      TVM_MAPHTREEITEMTOACCID = (TV_FIRST + 43)
      TVM_SETEXTENDEDSTYLE = (TV_FIRST + 44)
      TVM_GETEXTENDEDSTYLE = (TV_FIRST + 45)
      TVM_SETHOT = (TV_FIRST + 58)
      TVM_SETAUTOSCROLLINFO = (TV_FIRST + 59)
      '6.0
      TVM_GETSELECTEDCOUNT = (TV_FIRST + 70)
      TVM_SHOWINFOTIP = (TV_FIRST + 71)
      TVM_GETITEMPARTRECT = (TV_FIRST + 72)
      
        
    End Enum   ' TVMessages
    
    Public Type TVITEM   ' was TV_ITEM
      Mask As TVITEM_Mask
      hItem As Long
      State As TVITEM_State
      StateMask As TVITEM_State
      pszText As Long
      cchTextMax As Long
      iImage As Long
      iSelectedImage As Long
      cChildren As Long
      lParam As Long
    End Type
    
    
    Public Function TreeView_HitTest(hwnd As Long, lpht As TVHITTESTINFO) As Long
        TreeView_HitTest = SendMessage(hwnd, TVM_HITTEST, 0, lpht)
    End Function
    Then in the WndProc:
    Code:
    Select Case uMsg
        Case WM_NOTIFY
            Dim nmtv As NMTREEVIEW
            Dim pt As POINTAPI
            CopyMemory nmtv, ByVal lParam, Len(nmtv)
    
            Select Case nmtv.hdr.Code
                Case NM_CLICK
                      Call GetCursorPos(pt)
                      Call ScreenToClient(hTV, pt) 'hTV is the TreeView hWnd (passed as the return value of CreateWindowEx)
                      tvhti.pt = pt
                      Call TreeView_HitTest(hTV, tvhti) '
                      If (tvhti.Flags And TVHT_ONITEMLINE) Then 
                           clicked =tvhti.hItem

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width