Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Three State Checkbox TreeView

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

  • Three State Checkbox TreeView

    This code demostrate how to emulate a three state checkbox TreeView,
    if some child items are selected the parent turns gray.

    Code:
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    '
    ' This sample shows how to emulate a Three State Checkbox TreeView
    '
    ' Enjoy
    ' :-)
    
    ' by Steve Mc Gregor
    ' [email protected]
    '
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    #Compile Exe
    
        %NOANIMATE       = 1
        %NOBUTTON        = 1
        %NOCOMBO         = 1
        %NODATETIMEPICK  = 1
        %NODRAGLIST      = 1
        %NOHEADER        = 1
        %NOHOTKEY        = 1
    '    %NOIMAGELIST     = 1
        %NOPIADDRESS     = 1
        %NOLIST          = 1
    '    %NOLISTVIEW      = 1
        %NOMONTHCAL      = 1
        %NONATIVEFONTCTL = 1
        %NOPAGESCROLLER  = 1
        %NOPROGRESS      = 1
        %NOREBAR         = 1
        %NOSTATUSBAR     = 1
        %NOTABCONTROL    = 1
        %NOTOOLBAR       = 1
        %NOTOOLTIPS      = 1
        %NOTRACKBAR      = 1
    '    %NOTREEVIEW      = 1
        %NOUPDOWN        = 1
    
    #Resource "3STCHKTV.PBR"
    #Include "WIN32API.INC"
    #Include "COMMCTRL.INC"
    
    %ID_TREEVIEW = 1001
    
    %UNCHECKED = 1
    %CHECKED = 2
    %GRAYED = 3
    
    %UM_CHECKSTATECHANGE = %WM_USER + 100
    
    Global hInst As Long
    
    Function ChangeChildState (ByVal hTreeView As Long, ByVal hItem As Long, ByVal iState As Long) As Long
      Local hTmpItem As Long
      Local TVITEM As TV_ITEM
      Local iState2 As Long
    
      TVITEM.mask  = %TVIF_HANDLE Or %TVIF_CHILDREN
      TVITEM.hItem = hItem
      TVITEM.stateMask = %TVIS_STATEIMAGEMASK
    
      TreeView_GetItem hTreeView, TVITEM
    
      If TVITEM.cChildren = 1 Then
        hTmpItem = TreeView_GetChild(hTreeView,hItem)
        Do
          TVITEM.mask = %TVIF_STATE Or %TVIF_HANDLE
          TVITEM.hItem = hTmpItem
          TVITEM.stateMask = %TVIS_STATEIMAGEMASK
          TVITEM.state     = INDEXTOSTATEIMAGEMASK(iState)
          TreeView_SetItem hTreeView, TVITEM
    
          ChangeChildState hTreeView, hTmpItem, iState
    
          hTmpItem = TreeView_GetNextSibling(hTreeView,hTmpItem)
          If hTmpItem = %NULL Then Exit Loop
        Loop
      End If
    
    End Function
    '===========================================================================
    Function ChangeParentState (ByVal hTreeView As Long, ByVal hItem As Long, ByVal iState As Long) As Long
      Local hParentItem As Long
      Local hTmpItem As Long
      Local TVITEM As TV_ITEM
      Local iState2 As Long
    
      hParentItem = TreeView_GetParent(hTreeView, hItem)
      If hParentItem <> %NULL Then
        hTmpItem = TreeView_GetChild(hTreeView,hParentItem)
        Do
          TVITEM.mask = %TVIF_STATE Or %TVIF_HANDLE
          TVITEM.hItem = hTmpItem
          TVITEM.stateMask = %TVIS_STATEIMAGEMASK
          TreeView_GetItem hTreeView, TVITEM
          iState2 = TVITEM.state
          Shift Right iState2, 12
    
          Select Case iState
            Case 2 ' check
              If iState2 = 1 Or iState2 = 3 Then
                TVITEM.mask = %TVIF_STATE Or %TVIF_HANDLE
                TVITEM.hItem = hParentItem
                TVITEM.stateMask = %TVIS_STATEIMAGEMASK
                TVITEM.state = INDEXTOSTATEIMAGEMASK(3)
                TreeView_SetItem hTreeView, TVITEM
                ChangeParentState hTreeview, hParentItem, 3
                Exit Loop
              End If
    
            Case 1 ' uncheck
              If iState2 = 2 Or iState2 = 3 Then
                TVITEM.mask = %TVIF_STATE Or %TVIF_HANDLE
                TVITEM.hItem = hParentItem
                TVITEM.stateMask = %TVIS_STATEIMAGEMASK
                TVITEM.state = INDEXTOSTATEIMAGEMASK(3)
                TreeView_SetItem hTreeView, TVITEM
                ChangeParentState hTreeview, hParentItem, 3
                Exit Loop
              End If
    
            Case 3
              TVITEM.mask = %TVIF_STATE Or %TVIF_HANDLE
              TVITEM.hItem = hParentItem
              TVITEM.stateMask = %TVIS_STATEIMAGEMASK
              TVITEM.state = INDEXTOSTATEIMAGEMASK(3)
              TreeView_SetItem hTreeView, TVITEM
              ChangeParentState hTreeview, hParentItem, 3
              Exit Loop
          End Select
    
          hTmpItem = TreeView_GetNextSibling(hTreeview, hTmpItem)
          If hTmpItem = %NULL Then
            TVITEM.mask = %TVIF_STATE Or %TVIF_HANDLE
            TVITEM.hItem = hParentItem
            TVITEM.stateMask = %TVIS_STATEIMAGEMASK
            TVITEM.state = INDEXTOSTATEIMAGEMASK(iState)
            TreeView_SetItem hTreeview, TVITEM
            ChangeParentState hTreeview, hParentItem, iState
            Exit Loop
          End If
        Loop
      End If
    End Function
    '===========================================================================
    CallBack Function WndProc () As Long
    
      Select Case CBMsg
        Case %WM_INITDIALOG
          '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          'Inicia ImageList
          '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          Static hImageList As Long
          Local hIcon      As Long
          hImageList = ImageList_Create( 16, 16, 1, 4, 1 )
    
          hIcon = LoadIcon(hInst, "UNCHECKED")
          ImageList_addIcon hImageList, hIcon
    
          hIcon = LoadIcon(hInst,"UNCHECKED")
          ImageList_addIcon hImageList, hIcon
    
          hIcon = LoadIcon(hInst,"CHECKED")
          ImageList_addIcon hImageList, hIcon
    
          hIcon = LoadIcon(hInst,"GRAYED")
          ImageList_addIcon hImageList, hIcon
    
          Call TreeView_SetImageList(GetDlgItem(CBHndl,%ID_TREEVIEW) , hImageList, %TVSIL_STATE)
    
          ' Generate Items
          Local TVINSERT    As TV_INSERTSTRUCT
          Local zText       As Asciiz * 64
    
          Local lTmp1 As Long
          Local lTmp2 As Long
          Local lTmp3 As Long
    
          Local hItem1 As Long
          Local hItem2 As Long
          Local hItem3 As Long
    
          For lTmp1 = 1 To 15
            zText = "Item " + Format$(lTmp1)
            TVINSERT.hParent = 0
            TVINSERT.Item.Item.mask = %TVIF_TEXT Or %TVIF_HANDLE Or %TVIF_CHILDREN Or %TVIF_STATE
            TVINSERT.Item.Item.stateMask = %TVIS_STATEIMAGEMASK
            TVINSERT.Item.Item.state = %UNCHECKED
            TVINSERT.Item.Item.pszText = VarPtr(zText)
            TVINSERT.Item.Item.cchTextMax  = SizeOf(zText)
            TVINSERT.Item.Item.cchildren = 1
            hItem1 = TreeView_InsertItem (GetDlgItem(CBHndl,%ID_TREEVIEW),TVINSERT)
            For lTmp2 = 1 To 15
              zText = "Item " + Format$(lTmp1) + "." + Format$(lTmp2)
              TVINSERT.hParent = hItem1
              TVINSERT.Item.Item.mask = %TVIF_TEXT Or %TVIF_HANDLE Or %TVIF_CHILDREN Or %TVIF_STATE
              TVINSERT.Item.Item.stateMask = %TVIS_STATEIMAGEMASK
              TVINSERT.Item.Item.state = %UNCHECKED
              TVINSERT.Item.Item.pszText = VarPtr(zText)
              TVINSERT.Item.Item.cchTextMax  = SizeOf(zText)
              TVINSERT.Item.Item.cchildren = 1
              hItem2 = TreeView_InsertItem (GetDlgItem(CBHndl,%ID_TREEVIEW),TVINSERT)
              For lTmp3 = 1 To 15
                zText = "Item " + Format$(lTmp1) + "." + Format$(lTmp2) + "." + Format$(lTmp3)
                TVINSERT.hParent = hItem2
                TVINSERT.Item.Item.mask = %TVIF_TEXT Or %TVIF_HANDLE Or %TVIF_CHILDREN Or %TVIF_STATE
                TVINSERT.Item.Item.stateMask = %TVIS_STATEIMAGEMASK
                TVINSERT.Item.Item.state = %UNCHECKED
                TVINSERT.Item.Item.pszText = VarPtr(zText)
                TVINSERT.Item.Item.cchTextMax  = SizeOf(zText)
                TVINSERT.Item.Item.cchildren = 0
                hItem3 = TreeView_InsertItem (GetDlgItem(CBHndl,%ID_TREEVIEW),TVINSERT)
              Next
            Next
          Next
    
        Case %WM_NOTIFY
          Select Case LoWrd(CBwParam)
            Case %ID_TREEVIEW
              Local TVITEM As TV_ITEM
              Local Msg As NMHDR Ptr
              Local KeyPress As TV_KEYDOWN Ptr
    
              Msg = CBlParam
    
              Select Case @Msg.code
    
                Case %NM_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, @Msg.hWndFrom, ht.pt, 1)
                  Call TreeView_HitTest(@Msg.hwndFrom, ht)
                  If ht.flags = %TVHT_ONITEMSTATEICON Then
                    PostMessage CBHndl, %UM_CHECKSTATECHANGE, @Msg.hWndFrom, ht.hItem
                  End If
    
                Case %TVN_KEYDOWN
                  KeyPress = CBlParam
                  Select Case @KeyPress.wVKey
                    Case %VK_SPACE
                      PostMessage CBHndl, %UM_CHECKSTATECHANGE, @Msg.hWndFrom, TreeView_GetSelection(@Msg.hWndFrom)
    
                  End Select
              End Select
    
        End Select
    
        Case %UM_CHECKSTATECHANGE
          Local lState As Long
          TVITEM.mask  = %TVIF_STATE Or %TVIF_HANDLE Or %TVIF_CHILDREN
          TVITEM.hItem = CBlParam
          TVITEM.stateMask = %TVIS_STATEIMAGEMASK
    
          TreeView_GetItem CBwParam, TVITEM
          lState = tvItem.state
          Shift Right lState, 12
    
          If lState = 3 Then
            TVITEM.state = INDEXTOSTATEIMAGEMASK(1)
            TreeView_SetItem CBwParam, TVITEM
            lState = 1
          End If
    
          ChangeChildState CBwParam, CBlParam, lState
          ChangeParentState CBwParam, CBlParam, lState
    
        Case %WM_DESTROY
          ImageList_Destroy hImageList
    
      End Select
    End Function
    
    Function WinMain (ByVal hInstance  As Long, _
                      ByVal hPrevInstance As Long, _
                      lpszCmdLine         As Asciiz Ptr, _
                      ByVal nCmdShow      As Long) Export As Long
      hInst = hInstance
    
      Local hDlg As Long
    
      Dialog New 0, "TreeView Sample", , , 250,270, %DS_MODALFRAME Or %WS_POPUP Or %WS_CAPTION Or %WS_SYSMENU To hDlg
    
      Local iccex As INIT_COMMON_CONTROLSEX
      iccex.dwSize = SizeOf(iccex)
      iccex.dwICC = %ICC_TREEVIEW_CLASSES
      Call InitCommonControlsEx(iccex)
    
      Control Add "SysTreeView32", _
                  hDlg, _
                  %ID_TREEVIEW, _
                  "", _
                  4, _
                  4, _
                  242, _
                  262, _
                  %WS_CHILD Or %WS_CLIPSIBLINGS Or %WS_CLIPCHILDREN Or %WS_TABSTOP Or %WS_VISIBLE Or _
                  %TVS_CHECKBOXES Or %TVS_DISABLEDRAGDROP Or %TVS_HASBUTTONS Or %TVS_HASLINES Or %TVS_LINESATROOT Or %TVS_INFOTIP, _
                  %WS_EX_CLIENTEDGE
    
      Dialog Show Modal hDlg, Call WndProc
    End Function
    The RC Script
    ================

    Code:
    /////////////////////////////////////////////////////////////////////////////
    //
    // Icon
    //
    
    CHECKED                 ICON    DISCARDABLE     "Checked.ico"
    GRAYED                  ICON    DISCARDABLE     "Grayed.ico"
    UNCHECKED               ICON    DISCARDABLE     "Unchecked.ico"
    I make the icons capturing a checkbox image with Alt-PrintScreen they are easy
    to make.

    Enjoy.

    Steve

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

  • #2
    If you wan't the icons i can send them by mail, just E-mail me to
    [email protected]

    Steve

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

    Comment

    Working...
    X