Date: 02-16-2022
Return to Index
created by gbSnippets
'A TreeView does not have a simple icon on/off display property
'You have to attach/detach an imagelist, as well as assign each
'node individually.
'Note: when you detach the ImageList, the TreeView losses the
'icon assignment, so you have to store the assignment somewhere.
'The User value for each node is a great place, avoiding the need
'to maintain a separate array. You can even keep 2 values in User
'(corresponding to selected/non-selected image settings) by using
'the MAK function, and extracting them with the Lo/Hi function.
'Primary Code:
'The two lines of code show how to attach/detach an imagelist.
TreeView Set ImageList hDlg, %IDC_TreeView, hImgListTreeView 'attach
TreeView Set ImageList hDlg, %IDC_TreeView, 0 'detach
'this additional code is used to assign icons to a node. A selected
'and non-selected image can be assigned.
Local T as TV_Item, iIcon&, hNode as Dword
T.hItem = hNode
T.mask = %TVIF_IMAGE Or %TVIF_SELECTEDIMAGE
T.iImage = iIcon&
T.iSelectedImage = iIcon&
TreeView_SetItem(hTree, T)
'Compilable Example: (Jose Includes)
'This example stores a single image value in the User property
'of each node.
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Include "CommCtrl.inc"
#Resource "gbsnippets.pbr"
%IDC_TreeView = 100 : %IDC_Button = 200
Global hDlg As Dword, hTree as Dword, hLst as Dword
Function PBMain() As Long
Local hItem AS Dword, hTemp AS Dword, hTemp2 AS Dword, hTemp3 AS Dword
Dialog New Pixels, 0, "TreeView",200,200,155,250, %WS_SYSMENU, 0 TO hDlg
Control Add TreeView, hDlg, %IDC_TreeView, "", 10,10,130,200
Control Handle hDlg, %IDC_TreeView TO hTree
Control Add Button, hDlg, %IDC_Button,"Toggle Icons", 25,220,95,20
TreeView Insert Item hDlg, %IDC_TreeView, 0, %TVI_LAST, 0,0,"Root" TO hItem
TreeView Set User hDlg, %IDC_TreeView, hItem,1
TreeView Insert Item hDlg, %IDC_TreeView, hItem, %TVI_LAST, 0,0,"Mother" TO hTemp
TreeView Set User hDlg, %IDC_TreeView, hTemp,1
TreeView Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_LAST, 0,0,"Dan" TO hTemp2
TreeView Set User hDlg, %IDC_TreeView, hTemp2,2
TreeView Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_LAST, 0,0,"Bob" TO hTemp3
TreeView Set User hDlg, %IDC_TreeView, hTemp3,1
TreeView Set Expanded hDlg, %IDC_TreeView, hTemp, %True
TreeView Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_LAST, 0,0,"Foot" TO hTemp2
TreeView Set User hDlg, %IDC_TreeView, hTemp2,2
TreeView Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_LAST, 0,0,"Arm" TO hTemp2
TreeView Set User hDlg, %IDC_TreeView, hTemp2,2
TreeView Set Expanded hDlg, %IDC_TreeView, hTemp3, %True
TreeView Insert Item hDlg, %IDC_TreeView, hItem, %TVI_LAST, 0,0,"Father" TO hTemp
TreeView Set User hDlg, %IDC_TreeView, hTemp,1
TreeView Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_LAST, 0,0,"Helen" TO hTemp2
TreeView Set User hDlg, %IDC_TreeView, hTemp2,2
TreeView Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_LAST, 0,0,"Any" TO hTemp3
TreeView Set User hDlg, %IDC_TreeView, hTemp3,1
TreeView Set Expanded hDlg, %IDC_TreeView, hTemp, %True
TreeView Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_LAST, 0,0,"Leg" TO hTemp2
TreeView Set User hDlg, %IDC_TreeView, hTemp2,2
TreeView Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_LAST, 0,0,"Finger" TO hTemp2
TreeView Set User hDlg, %IDC_TreeView, hTemp2,2
TreeView Set Expanded hDlg, %IDC_TreeView, hTemp3, %True
TreeView Set Expanded hDlg, %IDC_TreeView, hItem, %True
'create imagelist for Tree
ImageList New Icon 16,16,32,3 To hLst
ImageList Add Icon hLst, "open" '1
ImageList Add Icon hLst, "closed" '2
ImageList Add Icon hLst, "data" '3
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local hNode As Dword
Static DisplayIcons&
If CB.Msg = %WM_Command AND CB.Ctl = %IDC_Button Then
DisplayIcons& = DisplayIcons& XOR 1
If DisplayIcons& Then
TreeView Set ImageList hDlg, %IDC_TreeView, hLst 'attach
AssignIcons 'sets the icon for each Tree node
Else
TreeView Set ImageList hDlg, %IDC_TreeView, 0 'detach
End If
End If
End Function
Sub AssignIcons
'this walks through a complete Tree, assigning icons from the node User property
Local iReturn As Dword, hNode as Dword, hRoot as Dword, T as TV_Item, iIcon&
TreeView Get Root hDlg, %IDC_TreeView to hRoot
hNode = hRoot
Do
'assign icon to hNode-----------
T.hItem = hNode
T.mask = %TVIF_IMAGE Or %TVIF_SELECTEDIMAGE
TreeView Get User hDlg, %IDC_TreeView, hNode To iIcon&
T.iImage = iIcon&
T.iSelectedImage = iIcon&
TreeView_SetItem(hTree, T)
'----------------------------
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 '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
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)
Loop While hNode
End Sub
'gbs_00272
'Date: 03-10-2012
'A TreeView does not have a simple icon on/off display property
'You have to attach/detach an imagelist, as well as assign each
'node individually.
'Note: when you detach the ImageList, the TreeView losses the
'icon assignment, so you have to store the assignment somewhere.
'The User value for each node is a great place, avoiding the need
'to maintain a separate array. You can even keep 2 values in User
'(corresponding to selected/non-selected image settings) by using
'the MAK function, and extracting them with the Lo/Hi function.
'Primary Code:
'The two lines of code show how to attach/detach an imagelist.
TreeView Set ImageList hDlg, %IDC_TreeView, hImgListTreeView 'attach
TreeView Set ImageList hDlg, %IDC_TreeView, 0 'detach
'this additional code is used to assign icons to a node. A selected
'and non-selected image can be assigned.
Local T as TV_Item, iIcon&, hNode as Dword
T.hItem = hNode
T.mask = %TVIF_IMAGE Or %TVIF_SELECTEDIMAGE
T.iImage = iIcon&
T.iSelectedImage = iIcon&
TreeView_SetItem(hTree, T)
'Compilable Example: (Jose Includes)
'This example stores a single image value in the User property
'of each node.
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Include "CommCtrl.inc"
#Resource "gbsnippets.pbr"
%IDC_TreeView = 100 : %IDC_Button = 200
Global hDlg As Dword, hTree as Dword, hLst as Dword
Function PBMain() As Long
Local hItem AS Dword, hTemp AS Dword, hTemp2 AS Dword, hTemp3 AS Dword
Dialog New Pixels, 0, "TreeView",200,200,155,250, %WS_SYSMENU, 0 TO hDlg
Control Add TreeView, hDlg, %IDC_TreeView, "", 10,10,130,200
Control Handle hDlg, %IDC_TreeView TO hTree
Control Add Button, hDlg, %IDC_Button,"Toggle Icons", 25,220,95,20
TreeView Insert Item hDlg, %IDC_TreeView, 0, %TVI_LAST, 0,0,"Root" TO hItem
TreeView Set User hDlg, %IDC_TreeView, hItem,1
TreeView Insert Item hDlg, %IDC_TreeView, hItem, %TVI_LAST, 0,0,"Mother" TO hTemp
TreeView Set User hDlg, %IDC_TreeView, hTemp,1
TreeView Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_LAST, 0,0,"Dan" TO hTemp2
TreeView Set User hDlg, %IDC_TreeView, hTemp2,2
TreeView Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_LAST, 0,0,"Bob" TO hTemp3
TreeView Set User hDlg, %IDC_TreeView, hTemp3,1
TreeView Set Expanded hDlg, %IDC_TreeView, hTemp, %True
TreeView Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_LAST, 0,0,"Foot" TO hTemp2
TreeView Set User hDlg, %IDC_TreeView, hTemp2,2
TreeView Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_LAST, 0,0,"Arm" TO hTemp2
TreeView Set User hDlg, %IDC_TreeView, hTemp2,2
TreeView Set Expanded hDlg, %IDC_TreeView, hTemp3, %True
TreeView Insert Item hDlg, %IDC_TreeView, hItem, %TVI_LAST, 0,0,"Father" TO hTemp
TreeView Set User hDlg, %IDC_TreeView, hTemp,1
TreeView Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_LAST, 0,0,"Helen" TO hTemp2
TreeView Set User hDlg, %IDC_TreeView, hTemp2,2
TreeView Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_LAST, 0,0,"Any" TO hTemp3
TreeView Set User hDlg, %IDC_TreeView, hTemp3,1
TreeView Set Expanded hDlg, %IDC_TreeView, hTemp, %True
TreeView Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_LAST, 0,0,"Leg" TO hTemp2
TreeView Set User hDlg, %IDC_TreeView, hTemp2,2
TreeView Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_LAST, 0,0,"Finger" TO hTemp2
TreeView Set User hDlg, %IDC_TreeView, hTemp2,2
TreeView Set Expanded hDlg, %IDC_TreeView, hTemp3, %True
TreeView Set Expanded hDlg, %IDC_TreeView, hItem, %True
'create imagelist for Tree
ImageList New Icon 16,16,32,3 To hLst
ImageList Add Icon hLst, "open" '1
ImageList Add Icon hLst, "closed" '2
ImageList Add Icon hLst, "data" '3
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local hNode As Dword
Static DisplayIcons&
If CB.Msg = %WM_Command AND CB.Ctl = %IDC_Button Then
DisplayIcons& = DisplayIcons& XOR 1
If DisplayIcons& Then
TreeView Set ImageList hDlg, %IDC_TreeView, hLst 'attach
AssignIcons 'sets the icon for each Tree node
Else
TreeView Set ImageList hDlg, %IDC_TreeView, 0 'detach
End If
End If
End Function
Sub AssignIcons
'this walks through a complete Tree, assigning icons from the node User property
Local iReturn As Dword, hNode as Dword, hRoot as Dword, T as TV_Item, iIcon&
TreeView Get Root hDlg, %IDC_TreeView to hRoot
hNode = hRoot
Do
'assign icon to hNode-----------
T.hItem = hNode
T.mask = %TVIF_IMAGE Or %TVIF_SELECTEDIMAGE
TreeView Get User hDlg, %IDC_TreeView, hNode To iIcon&
T.iImage = iIcon&
T.iSelectedImage = iIcon&
TreeView_SetItem(hTree, T)
'----------------------------
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 '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
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)
Loop While hNode
End Sub
'gbs_00272
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm