Announcement

Collapse
No announcement yet.

Trapping a mouse click on a TreeView

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trapping a mouse click on a TreeView

    Sorry if this is a lame question...

    I can't seem to figure out how to trap the mouse button
    click on a treeview. What messages should I be looking for
    in the dialog callback?

    Thanks,
    Scott


    ------------------
    Scott Wolfington
    http://www.boogietools.com


    [This message has been edited by Scott Wolfington (edited March 05, 2003).]
    Scott Wolfington
    [url="http://www.boogietools.com"]http://www.boogietools.com[/url]

  • #2
    These few come to mind:

    WM_LBUTTONDOWN
    WM_RBUTTONDOWN
    WM_MBUTTONDOWN
    WM_LBUTTONUP
    WM_RBUTTONUP
    WM_MBUTTONUP
    WM_LBUTTONDBLCLK
    WM_RBUTTONDBLCLK
    WM_MBUTTONDBLCLK
    WM_MOUSEMOVE
    WM_MOUSEWHEEL

    Cheers,
    Cecil

    ------------------

    Comment


    • #3
      Scott --

      Microsoft didn't make this one easy. In your window proc...

      Code:
          DIM tpNotify AS LOCAL NMHDR PTR
       	
          ...ELSEIF wMsg = %WM_NOTIFY THEN
           
              tpNotify = lParam
       
              IF @tpNotify.Code = %NM_CLICK THEN
              	'somebody clicked a tree item
              END IF
      (I use the "tp" prefix to mean "type pointer". It's optional.)

      It gets worse... Then you have to use %TVM_HITTEST to get the handle of the tree item at the current cursor location, then %TVM_GETITEM to get the item number that goes with that item.

      Are you really trying to detect the click, or detect that an item has been selected? Remember that item selection can be done with the keyboard too...

      -- Eric


      ------------------
      Perfect Sync: Perfect Sync Development Tools
      Email: mailto:[email protected][email protected]</A>



      [This message has been edited by Eric Pearson (edited February 22, 2001).]
      "Not my circus, not my monkeys."

      Comment


      • #4
        Very doubt that Scott is interesting exactly in mouse down.
        Typically %TVN_SELCHANGED is enough.
        But coming back to click.
        WM_PARENTNOTIFY with Lowrd(CbWparam) = WM_.BUTTONDOWN occurs before WM_NOTIFY.


        ------------------
        E-MAIL: [email protected]

        Comment


        • #5
          Semen --

          It really makes very little difference which technique you use, if all you are doing is detecting an item-click. But the WM_NOTIFY system is much more flexible that WM_PARENTNOTIFY -- it provides notification about many more events -- so in real-world treeview apps I tend to use it for everything.

          -- Eric

          ------------------
          Perfect Sync: Perfect Sync Development Tools
          Email: mailto:[email protected][email protected]</A>

          [This message has been edited by Eric Pearson (edited February 22, 2001).]
          "Not my circus, not my monkeys."

          Comment


          • #6
            About real programs - agree.
            I added some statements to Peter's sample to illustrate my previous post.
            Selected item is shown in caption.

            Code:
               #Compile Exe
               #Dim All
               #Register None
               #Include "win32api.inc"
               #Include "commctrl.inc"
            
               Function TVInsertItem(ByVal hTree As Long, ByVal hParent As Long, sTxt As String) As Long
                  Local tv_insert As TV_INSERTSTRUCT
                  Local tv_item As TV_ITEM
                  If hParent Then
                     tv_item.mask = %TVIF_CHILDREN Or %TVIF_HANDLE
                     tv_item.hItem = hParent
                     tv_item.cchildren = 1
                     TreeView_SetItem hTree, tv_item
                  End If
                  tv_insert.hParent = hParent
                  tv_insert.Item.Item.mask = %TVIF_TEXT
                  tv_insert.Item.Item.pszText = StrPtr(sTxt)
                  tv_insert.Item.Item.cchTextMax = Len(sTxt)
                  Function = TreeView_InsertItem(hTree, tv_insert)
               End Function
            
               CallBack Function DlgProc
                  %ID_TREE = 100
                  Dim hParent As Long, hRoot As Long, hTree As Static Long
                  Dim lpNmh As NMHDR Ptr, hItem As Long, lpTV As NM_TREEVIEW Ptr, pitem As TV_ITEM
                  Dim TmpAsciiz As Asciiz * %MAX_PATH, i As Long, j As Long, Answer As String
            
                  Select Case CbMsg
                     Case %WM_INITDIALOG
                        Control Add "SysTreeView32", CbHndl, %ID_TREE, "", 5, 5, 190, 90, _
                            %WS_CHILD Or %WS_VISIBLE Or %TVS_HASBUTTONS Or %TVS_HASLINES Or _
                            %TVS_LINESATROOT Or %TVS_SHOWSELALWAYS, %WS_EX_CLIENTEDGE
                        Control Handle CbHndl, %ID_TREE To hTree
              
                        hRoot = TVInsertItem(hTree, 0, "Root")
                        For i = 1 To 10
                           hParent = TVInsertItem(hTree, hRoot,  "Item" + Format$(i))
                           For j = 1 To 10
                              TVInsertItem hTree, hParent, "Subitem" + Format$(i) + "." + Format$(j)
                           Next
                        Next
                        
                     Case %WM_NOTIFY
                        lpNmh = CbLparam
                        If (@lpNmh.Code = %TVN_SELCHANGED) And (@lpNmh.idFrom = %ID_TREE) Then
                            lpTV = CbLparam
                            i = @lpTV.ItemNew.hItem
                            While i
                               pItem.hitem = i
                               pItem.mask = %TVIF_TEXT
                               pItem.pszText = VarPtr(TmpAsciiz)
                               pItem.cchTextMax = SizeOf(TmpAsciiz)
                               TreeView_GetItem hTree, pItem
                               If Answer <> "" Then Answer = Trim$(TmpAsciiz) + "; " + Answer Else Answer = Trim$(TmpAsciiz)
                               i = TreeView_GetParent (hTree, pitem.hitem)
                            Wend
                            SetWindowText CbHndl, ByVal StrPtr(Answer)
                         End If
                  End Select
            
               End Function
            
               Function PbMain
                  Local hDlg As Long
                  Dialog New 0, "TreeView32",,, 200, 100, %WS_SYSMENU Or %WS_MINIMIZEBOX Or %WS_CAPTION To hDlg
                  Dialog Show Modal hDlg Call DlgProc
               End Function
            ------------------
            E-MAIL: [email protected]

            Comment


            • #7
              Thanks guys,

              Eric, you're right... I need to detect an item selection
              and like you said, item selection can be done with the
              keyboard too. How would I go about handling keyboard selection
              of an item?

              The example you gave me is definitely still useful because I
              need it for trapping right clicks (%NM_RCLICK) to pop up
              context sensitive menus. Again, I'll still need to know which
              item I'm on though, so I'll take a look at the %TVM_HITTEST
              and %TVM_GETITEM you mentioned. Thanks for your help on this!

              Regards,
              Scott


              ------------------
              Scott Wolfington
              http://www.boogietools.com


              [This message has been edited by Scott Wolfington (edited March 05, 2003).]
              Scott Wolfington
              [url="http://www.boogietools.com"]http://www.boogietools.com[/url]

              Comment


              • #8
                Semen-

                Great example. You posted that like seconds before my
                last post. :-) Thank you very much!

                Scott


                ------------------
                Scott Wolfington
                http://www.boogietools.com


                [This message has been edited by Scott Wolfington (edited March 05, 2003).]
                Scott Wolfington
                [url="http://www.boogietools.com"]http://www.boogietools.com[/url]

                Comment

                Working...
                X