TREEVIEW CHECKBOX

Started by Brian Chirgwin, September 14, 2009, 10:40:25 PM

Previous topic - Next topic

Brian Chirgwin


Using the treeview control, how do you get notifications that a checkbox was clicked on (checked/unchecked)?

I have the code the set the checkbox state (checked/unchecked), but I haven't figured out how to get notifications when a user clicks a checkbox in a treeview. 

Any help in the right direction would be appreciated.

David Kenny

#1
Brian,

You check the %UM_CHECKSTATECHANGE message (that you posted).  Sound strange, but read on.  The sample code below is the entire Custom routine for the form that contains the TreeView. You need to check the NM_Click message to see if it was on a checkbox an post the %UM_CHECKSTATECHANGE msg if it was.  And then, of course, process the %UM_CHECKSTATECHANGE msg. You may also notice it's a Tri-State checkbox being used here.

'------------------------------------------------------------------------------------------------------------------------
Function FORM1_CUSTOM ( _
                      hWndForm      As Dword, _  ' handle of Form
                      wMsg          As Long,  _  ' type of message
                      wParam        As Dword, _  ' first message parameter
                      lParam        As Long   _  ' second message parameter
                      ) As Long

  Local pNMTV As NM_TREEVIEW Ptr
  Local sText As String
  Local nLine As Long
 
  Select Case wMsg

     Case %WM_NOTIFY
         pNMTV = lParam
       
         Select Case @pNMTV.hdr.idfrom
       
              Case IDC_FORM1_TREEVIEW1      ' notify message from the treeview control
                  Select Case @pNMTV.hdr.code
                       
                        Case %TVN_SELCHANGED
                        Case %NM_CLICK    'left click
              Local ht As TV_HITTESTINFO
              Local dwpos As Dword
              Local iTmp As Integer
              dwpos = GetMessagePos()
              iTmp = LoWrd(dwpos)
              ht.pt.x = iTmp
              iTmp = HiWrd(dwpos)
              ht.pt.y = iTmp
              Call MapWindowPoints(%HWND_DESKTOP, @pNMTV.hdr.hWndFrom, ht.pt, 1)
              Call TreeView_HitTest(@pNMTV.hdr.hwndFrom, ht)
              If ht.flags = %TVHT_ONITEMSTATEICON Then
                PostMessage hWndForm, %UM_CHECKSTATECHANGE, @pNMTV.hdr.hWndFrom, ht.hItem
              End If
                        Case %NM_DBLCLK   'left double-click
                        Case %NM_RCLICK   ' right click

                        Case %NM_RDBLCLK   'right double-click

                  End Select
                 
         End Select                     
Case %UM_CHECKSTATECHANGE
Local TVITEM_ As TV_ITEM
Local lState As Long
TVITEM_.mask  = %TVIF_STATE Or %TVIF_HANDLE Or %TVIF_CHILDREN
TVITEM_.hItem = LParam
TVITEM_.stateMask = %TVIS_STATEIMAGEMASK

TreeView_GetItem WParam, TVITEM_
lState = TVITEM_.State
Shift Right lState, 12

If lState = 3 Then
  TVITEM_.State = IndexToStateImageMask(1)
  TreeView_SetItem WParam, TVITEM_
  lState = 1
End If

ChangeChildState WParam, LParam, lState
ChangeParentState WParam, LParam, lState


  End Select
End Function


This Microsoft link also contains useful information, though the sample code is in C.:
http://support.microsoft.com/kb/261289

David