Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile Exe
#Dim All
%Unicode=1
#Include "Win32API.inc"
%IDC_TreeView = 100
%IDC_Cut = 200
%IDC_Copy = 250
%IDC_PasteChild = 300
%IDC_PasteSibling = 400
%IDC_PasteContent = 500
%IDC_Reset = 600
Global hDlg As Dword, PasteContent$
Function PBMain() As Long
Local BoxStyle As Long
BoxStyle = %WS_Child Or %WS_Visible Or %ES_MultiLine Or %WS_VScroll Or %ES_AutoHScroll _
Or %WS_HScroll Or %ES_AutoVScroll Or %ES_WantReturn Or %ES_NoHideSel Or %WS_TabStop
Dialog Default Font "Tahoma", 12, 1
Dialog New Pixels, 0, "TreeView",200,200,385,590, %WS_SysMenu, 0 To hDlg
Control Add TextBox, hDlg, %IDC_PasteContent, "", 200,10,180,430, BoxStyle, %WS_Ex_ClientEdge
Control Add Button, hDlg, %IDC_Reset,"Reset", 200,450,110,25
Control Add Button, hDlg, %IDC_Copy,"Copy", 25,450,110,25
Control Add Button, hDlg, %IDC_Cut,"Cut", 25,480,110,25
Control Add Button, hDlg, %IDC_PasteSibling,"Paste_Sibling", 25,510,110,25
Control Add Button, hDlg, %IDC_PasteChild,"Paste_Child", 25,540,110,25
ResetTreeView
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local hNode As Dword, temp$
Select Case Cb.Msg
Case %WM_Command
Select Case Cb.Ctl
Case %IDC_Reset
ResetTreeView
Case %IDC_Cut
Treeview Get Select Cb.Hndl, %IDC_TreeView To hNode
If hNode = 0 Then ? "No slection!" : Exit Function
WalkThroughNode (hNode)
Control Set Text hDlg, %IDC_PasteContent, PasteContent$
Treeview Delete hDlg, %IDC_TreeView, hNode
Case %IDC_Copy
Treeview Get Select Cb.Hndl, %IDC_TreeView To hNode
If hNode = 0 Then ? "No slection!" : Exit Function
WalkThroughNode (hNode)
Control Set Text hDlg, %IDC_PasteContent, PasteContent$
Case %IDC_PasteSibling
Clipboard Get Text To temp$
If Len(temp$) = 0 Then ? "Clipboard Empty!" : Exit Function
PasteIntoTreeView 1
Case %IDC_PasteChild
Clipboard Get Text To temp$
If Len(temp$) = 0 Then ? "Clipboard Empty!" : Exit Function
PasteIntoTreeView 0
End Select
End Select
End Function
Sub ResetTreeView
Local hItem As Dword, hTemp As Dword, hTemp2 As Dword, hTemp3 As Dword
Control Kill hDlg, %IDC_TreeView
Control Add Treeview, hDlg, %IDC_TreeView, "", 10,10,180,430
Treeview Insert Item hDlg, %IDC_TreeView, 0, %TVI_Last, 0,0,"Root" To hItem
Treeview Insert Item hDlg, %IDC_TreeView, hItem, %TVI_Last, 0,0,"Mother" To hTemp
Treeview Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_Last, 0,0,"Dan" To hTemp2
Treeview Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_Last, 0,0,"Bob" To hTemp3
Treeview Set Expanded hDlg, %IDC_TreeView, hTemp, %True
Treeview Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_Last, 0,0,"Foot" To hTemp2
Treeview Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_Last, 0,0,"Arm" To hTemp2
Treeview Set Expanded hDlg, %IDC_TreeView, hTemp3, %True
Treeview Insert Item hDlg, %IDC_TreeView, hItem, %TVI_Last, 0,0,"Father" To hTemp
Treeview Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_Last, 0,0,"Helen" To hTemp2
Treeview Insert Item hDlg, %IDC_TreeView, hTemp2, %TVI_Last, 0,0,"Left" To hTemp3
Treeview Insert Item hDlg, %IDC_TreeView, hTemp2, %TVI_Last, 0,0,"Right" To hTemp3
Treeview Set Expanded hDlg, %IDC_TreeView, hTemp2, %True
Treeview Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_Last, 0,0,"Any" To hTemp3
Treeview Set Expanded hDlg, %IDC_TreeView, hTemp, %True
Treeview Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_Last, 0,0,"Leg" To hTemp2
Treeview Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_Last, 0,0,"Finger" To hTemp2
Treeview Set Expanded hDlg, %IDC_TreeView, hTemp3, %True
Treeview Set Expanded hDlg, %IDC_TreeView, hItem, %True
End Sub
Function WalkThroughNode(ByVal hNode As Dword) As Dword
'walks through (selects) children+subnodes of specified Node
'returns last node
'returns 0 if no children or subnodes
Local iReturn As Dword, hStartNode As Dword, D As Long, tmp$
hStartNode = hNode
Treeview Get Text hDlg, %IDC_TreeView, hStartNode To PasteContent$
PasteContent$ = "001 " + PasteContent$
D = 1
Do
Function = hNode
Treeview Get Child hDlg, %IDC_TreeView, hNode To iReturn 'get child (1st choice)
If iReturn = 0 Then Treeview Get Next hDlg, %IDC_TreeView, hNode To iReturn Else Incr D 'or sibling (2nd choice)
If iReturn = 0 Then 'no child or sibling
Do 'get sibling of first parent with sibling
Treeview Get Parent hDlg, %IDC_TreeView, hNode To hNode 'parent
If hNode Then Decr D
If hNode = hStartNode Then iReturn = 0 : Exit Loop 'if reach starting node, stop
Treeview Get Next hDlg, %IDC_TreeView, hNode To iReturn 'sibling child of parent
Loop Until iReturn Or (hNode = 0) 'stop when find sibling of parent with sibling, or no more choices
End If
hNode = iReturn 'possible values: 0, zero (no parent/no sibling), <>0 (parent or sibling)
'do something here-----------
If hNode And D > 0 Then Treeview Get Text hDlg, %IDC_TreeView, hNode To tmp$ : PasteContent$ += $CrLf + Format$(D,"000") + " " + tmp$
'If hNode and D > 0 Then TreeView Select hDlg, %IDC_TreeView, hNode : Sleep 300 'for demo only - selection/pause is optional
Loop While hNode And D > 0
End Function
Function PasteIntoTreeView(Sibling As Long) As String 'PasteContent$ is a Global string variable
Local record$, caption$, recordnext$, hItem, hTemp,h As Dword, i,DepthCurrent, DepthNext As Long
ReDim hParent(100) As Dword, Parent(100) As String
'determine parent of initial position
Treeview Get Select hDlg, %IDC_TreeView To hItem
If Sibling = 1 Then
Treeview Get Parent hDlg, %IDC_TreeView, hItem To hParent(0) 'sibling
Else
hParent(0) = hItem 'child
End If
'build interior
Treeview Get Select hDlg, %IDC_TreeView To hItem
For i = 1 To ParseCount(Pastecontent,$CrLf)
'Get info about Current and Next records
record$ = Parse$(Pastecontent,$CrLf,i)
depthcurrent = Val(record$)
caption$ = Parse$(record$,$Spc,2)
recordnext$ = Parse$(PasteContent,$CrLf,i+1)
depthnext = Val(recordnext$)
'put current record into TreeView
If DepthNext > DepthCurrent Then 'is parent node
Treeview Insert Item hDlg, %IDC_TreeView, hParent(depthcurrent-1), %TVI_Last, 0,0,caption$ To hItem 'add parent
hParent(DepthCurrent) = hItem 'save parent node value
Else 'is child node
Treeview Insert Item hDlg, %IDC_TreeView, hParent(depthcurrent-1), %TVI_Last, 0,0,caption$ To hTemp 'add child
End If
Treeview Set Expanded hDlg, %IDC_TreeView, hParent(depthcurrent-1), 1 'expand parent
Next i
End Function
http://www.garybeene.com/sw/gbsnippets.htm