Jump to content

fundamentals of converting treeview to structure


Recommended Posts

Hi, i have searched but couldnt find any similar answer, as the title says: i cant figure out the fundamentals of converting a treeview hwnd with unkown children to an structure of any sort

i've looked at _GUICtrlTreeView_IsParent _GUICtrlTreeView_ChildCount, but i cant put the pieces together, could need some help with this one

 

From:

#1parent

  #1parent Child

  #1parent Child

    #1parent Child Child

 

To:

<parent>

 <child></child>

 <child></child>

   <child>

      <childOfChild></childOfChild>

   </child>

</parent>

5ztqj2L.png

I would like to get pointed at the right direction

Edited by tarretarretarre
Link to comment
Share on other sites

This might point you in the right direction:

Essentially, the logic is

     Get TreeView Item Count

     For 1 to ItemCount

        Get Item Info

        Get Next Item

     Next

I'm not too sure how you want to handle the output formatting. For example...An item that is four levels deep...would you call that <childOfChildOfChild>?  Perhaps your tree only goes three levels deep and it's not an issue.  Either way, you can figure out how you want to format it. :)

Edited by spudw2k
Link to comment
Share on other sites

tarretarretarre, Here here here is all what you need need need.

The key point is that the structure of a treeview is uniquely defined by the levels of the items. When you have figured that out, the rest is easy.

This code creates a more or less random treeview, and writes the structure to a disk file, TreeView.txt.

#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>
#include <Array.au3>
#include <File.au3>

Global $hTV

Example()

Func Example()

  ; Create GUI
  Local $hGui = GUICreate( "Save TreeView to file", 400, 350, 600, 300, $GUI_SS_DEFAULT_GUI )

  ; Create TreeView
  Local $idTV = GUICtrlCreateTreeView( 4, 4, 392, 292, $GUI_SS_DEFAULT_TREEVIEW, $WS_EX_CLIENTEDGE )
  $hTV = GUICtrlGetHandle( $idTV )

  ; Add items
  Local $hItem0 = _GUICtrlTreeView_Add( $hTV, 0, 0 )
  _GUICtrlTreeView_AddChild( $hTV, $hItem0, 1 )
  _GUICtrlTreeView_AddChild( $hTV, $hItem0, "This" )
  Local $hItem3 = _GUICtrlTreeView_AddChild( $hTV, $hItem0, "is" )
  _GUICtrlTreeView_AddChild( $hTV, $hItem3, 4 )
  _GUICtrlTreeView_AddChild( $hTV, $hItem3, "a" )
  _GUICtrlTreeView_AddChild( $hTV, $hItem3, "very" )
  Local $hItem7 = _GUICtrlTreeView_AddChild( $hTV, $hItem3, "nice" )
  _GUICtrlTreeView_AddChild( $hTV, $hItem7, 8 )
  _GUICtrlTreeView_AddChild( $hTV, $hItem7, "TreeView" )
  Local $hItem10 = _GUICtrlTreeView_Add( $hTV, $hItem0, ", (comma)" )
  _GUICtrlTreeView_AddChild( $hTV, $hItem10, 11 )
  _GUICtrlTreeView_AddChild( $hTV, $hItem10, "indeed." )
  _GUICtrlTreeView_Add( $hTV, $hItem0, 13 )

  ; Expand TreeView
  _GUICtrlTreeView_Expand( $hTV, $hItem0 )
  _GUICtrlTreeView_Expand( $hTV, $hItem10 )

  ; Create button
  Local $idButSave = GUICtrlCreateButton( "Save", 160, 310, 80, 30 )

  ; Show GUI
  GUISetState( @SW_SHOW, $hGui )

  ; Message loop
  While 1

    Local $iMsg = GUIGetMsg()

    If $iMsg = 0 Or $iMsg = $GUI_EVENT_MOUSEMOVE Then ContinueLoop

    Switch $iMsg

      Case $idButSave
        SaveTreeView()

      Case $GUI_EVENT_CLOSE
        ExitLoop

    EndSwitch

  WEnd

  GUIDelete( $hGui )
  Exit

EndFunc

Func SaveTreeView()
  Local $aLines[100][2], $iLines = 0
  Local $hItem = _GUICtrlTreeView_GetFirstItem( $hTV )

  ; Get structure and information
  While $hItem
    $aLines[$iLines][0] = _GUICtrlTreeView_Level( $hTV, $hItem )   ; The level decides the structure
    $aLines[$iLines][1] = _GUICtrlTreeView_GetText( $hTV, $hItem ) ; The text is the information
    $hItem = _GUICtrlTreeView_GetNext( $hTV, $hItem )
    $iLines += 1
  WEnd
  ReDim $aLines[$iLines][2]
  _ArrayDisplay( $aLines )

  ; Save structure and information
  _FileWriteFromArray( "TreeView.txt", $aLines )
EndFunc

This code reads TreeView.txt, and recreates the treeview from the structure.

#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>
#include <Array.au3>
#include <File.au3>

Global $hTV

Example()

Func Example()

  ; Create GUI
  Local $hGui = GUICreate( "Create TreeView from file", 400, 350, 600, 300, $GUI_SS_DEFAULT_GUI )

  ; Create TreeView
  Local $idTV = GUICtrlCreateTreeView( 4, 4, 392, 292, $GUI_SS_DEFAULT_TREEVIEW, $WS_EX_CLIENTEDGE )
  $hTV = GUICtrlGetHandle( $idTV )

  ; Create button
  Local $idButCreate = GUICtrlCreateButton( "Create", 160, 310, 80, 30 )

  ; Show GUI
  GUISetState( @SW_SHOW, $hGui )

  ; Message loop
  While 1

    Local $iMsg = GUIGetMsg()

    If $iMsg = 0 Or $iMsg = $GUI_EVENT_MOUSEMOVE Then ContinueLoop

    Switch $iMsg

      Case $idButCreate
        CreateTreeView()

      Case $GUI_EVENT_CLOSE
        ExitLoop

    EndSwitch

  WEnd

  GUIDelete( $hGui )
  Exit

EndFunc

Func CreateTreeView()
  ; Delete TreeView
  _GUICtrlTreeView_DeleteAll( $hTV )

  ; Read structure and information
  Local $aLines, $iLines
  _FileReadToArray( "TreeView.txt", $aLines, 0 )
  $iLines = UBound( $aLines )
  _ArrayDisplay( $aLines )

  ; Create TreeView structure and information
  Local $aLine, $aLevels[100], $iLevel, $iLevelPrev = 0, $hItem

  ; Add root
  $aLine = StringSplit( $aLines[0], "|", 2 )
  $hItem = _GUICtrlTreeView_Add( $hTV, 0, $aLine[1] )
  $aLevels[0] = $hItem                     ; $aLevels[$iLevel] contains the last item of that level

  For $i = 1 To $iLines - 1
    $aLine = StringSplit( $aLines[$i], "|", 2 )
    $iLevel = $aLine[0]
    If $iLevel <> $iLevelPrev Then
      If $iLevel > $iLevelPrev Then        ; A child of the previous level
        $hItem = _GUICtrlTreeView_AddChild( $hTV, $aLevels[$iLevelPrev], $aLine[1] )
      Else ; $iLevel < $iLevelPrev         ; A sibling of the level
        $hItem = _GUICtrlTreeView_Add( $hTV, $aLevels[$iLevel], $aLine[1] )
      EndIf
      $aLevels[$iLevel] = $hItem           ; $aLevels[$iLevel] contains the last item of that level
      $iLevelPrev = $iLevel
    Else ; $iLevel = $iLevelPrev           ; A sibling of the level
      $hItem = _GUICtrlTreeView_Add( $hTV, $aLevels[$iLevel], $aLine[1] )
      $aLevels[$iLevel] = $hItem           ; $aLevels[$iLevel] contains the last item of that level
    EndIf
  Next

  _GUICtrlTreeView_Expand( $hTV )
EndFunc
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...