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

TreeView Colors, Fonts and Expansion Icons

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

  • TreeView Colors, Fonts and Expansion Icons

    When I change the size of the TreeView control, the plus/minus icons that indicate parents and node expansion do not resize. I've not found anything that would make them resize.

    I could use images from an imagelist for the plus/minus icons but I want the user to be able to pick any color for the TreeView control FG/BG colors, so that prevents me from have a library of colored plus/minus icons.

    So, that leads me to the solution below, where I generate bitmaps real-time (plus, minus, dotted line) to use in the TreeView. I draw the images on a Graphic Memory Bitmap and save them to bmp files, which are then added to the imagelist. The imagelist always has just the 3 images.

    Discussion here.

    I use a TreeView style that hides the plus-minus icons, and add the created btmaps to the imagelist

    I create the bitmaps at 128x128. My code calculates a desired image size based on the font size, which I then use in the imagelist. The imagelist handles the resizing of the 128x128 to the desired image size.



    Code:
    'Compilable Example:  (Jose Includes)
    #Compile Exe
    #Dim All
    %Unicode=1
    #Include "Win32API.inc"
    
    Enum Equates Singular
       IDC_TreeView = 500
       IDC_Color
       IDC_28
       IDC_48
    End Enum
    
    
    Global hDlg, hTreeView, hFont,hLst As Dword
    Global TextColor, BGColor, IconSize, FontSize As Long
    
    Function PBMain() As Long
       Dialog Default Font "Tahoma",12,1
       Dialog New Pixels, 0, "TreeView RealTime ImageList Images",,,700,1000, %WS_SysMenu, 0 To hDlg
    
       Control Add Button, hDlg, %IDC_Color,"Color",10,10,75,25
       Control Add Button, hDlg, %IDC_28,"28pt",100,10,75,25
       Control Add Button, hDlg, %IDC_48,"48pt",190,10,75,25
    
       TextColor = %rgb_Maroon : BGColor = %rgb_LightGray : FontSize = 48
       CreateFontAndIcons
       CreateTreeView
    
       Dialog Show Modal     hDlg Call DlgProc
    End Function
    
    CallBack Function DlgProc() As Long
       Local lptvcd As NMTVCustomDraw Ptr, hNode As Dword, temp$, iResult As Long
       Select Case Cb.Msg
          Case %WM_InitDialog
             AssignIcons(0)
             Treeview Get Root hDlg, %IDC_TreeView To hNode
             SetIcon(hNode)
             Treeview Unselect hDlg, %IDC_TreeView
          Case %WM_Help
          Case %WM_ContextMenu : Dialog End hDlg
          Case %WM_Command
             Select Case Cb.Ctl
                Case %IDC_Color
                   ToggleColors
                   Control Set Color hDlg, %IDC_TreeView, TextColor, BGColor
                   CreateColorIcons
                   ImageList Kill hLst
                   ImageList New Bitmap IconSize, IconSize,24,5 To hLst
                   ImageList Add Bitmap hLst, "empty.bmp"
                   ImageList Add Bitmap hLst, "plus.bmp"
                   ImageList Add Bitmap hLst, "minus.bmp"
                   Control ReDraw hDlg, %IDC_TreeView
                Case %IDC_28    : FontSize = 28 : CreateFontAndIcons
                Case %IDC_48    : FontSize = 48 : CreateFontAndIcons
                Case %IdCancel  : Dialog End hDlg
             End Select
          Case %WM_Notify
             Select Case Cb.NmId
                Case %IDC_TreeView
                   Select Case Cb.NmCode
                      Case %TVN_ItemExpanded
                         Treeview Get Select hDlg, %IDC_TreeView To hNode
                         Treeview Get Expanded hDlg, %IDC_TreeView, hNode To iResult
                         Treeview Get Text hDlg, %IDC_TreeView, hNode To temp$
                         SetIcon(hNode)
                      Case %TVN_SelChanged
                   End Select
             End Select
       End Select
    End Function
    
    Sub LoadTreeView
       Local hRoot,hItem, hTemp, hTemp2, hTemp3 As Dword
       Treeview Set ImageList hDlg, %IDC_TreeView, hLst
       Treeview Insert Item  hDlg, %IDC_TreeView, 0, %TVI_Last, 0,0,"C:\" To hRoot
       Treeview Insert Item  hDlg, %IDC_TreeView, hRoot, %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, hRoot, %TVI_Last, 0,0,"Father" To hTemp
       Treeview Insert Item  hDlg, %IDC_TreeView, hRoot, %TVI_Last, 0,0,"Uncle" To hTemp
       Treeview Insert Item  hDlg, %IDC_TreeView, hTemp, %TVI_Last, 0,0,"Cousin" To hTemp2
       Treeview Set Expanded hDlg, %IDC_TreeView, hTemp, %True
       Treeview Set Expanded hDlg, %IDC_TreeView, hRoot, %True
    End Sub
    
    Sub AssignIcons(Delay As Long)
       Local iReturn, iExpanded, hNode, hChild As Dword
       Treeview Get Root hDlg, %IDC_TreeView To hNode
       Do
          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)
          'do something here-----------
          SetIcon hNode
          'do something here-----------
          If hNode Then Treeview Select hDlg, %IDC_TreeView, hNode ': Sleep Delay 'for demo only - selection/pause is optional
       Loop While hNode
    End Sub
    
    Sub SetIcon(hNode As Dword)
       Local TreeViewItem As TVItemEX, IconChoice, iExpanded As Long, hChild As Dword
       Treeview Get Child hDlg, %IDC_TreeView, hNode To hChild
       Treeview Get Expanded hDlg, %IDC_TreeView, hNode To iExpanded
    
       If IsTrue  iExpanded                       Then IconChoice = 3  'minus
       If IsFalse iExpanded  And  IsFalse hChild  Then IconChoice = 1  'empty
       If IsFalse iExpanded  And  IsTrue  hChild  Then IconChoice = 2  'plus
    
       TreeView_GetItemEX hTreeView, TreeViewItem
       TreeViewItem.mask   = %TVIF_Image Or %TVIF_SelectedImage
       TreeviewItem.hItem  = hNode
       TreeViewItem.iImage = IconChoice-1
       TreeViewItem.iSelectedImage = IconChoice-1
       TreeView_SetItemEX hTreeView, TreeViewItem
    End Sub
    
    Sub ToggleColors
       Select Case TextColor
          Case %rgb_Maroon : TextColor = %Yellow     : BGColor = %Blue
          Case %Yellow     : TextColor = %White      : BGColor = %Black
          Case %White      : TextColor = %rgb_Maroon : BGColor = %rgb_LightGray
       End Select
    End Sub
    
    Sub CreateColorIcons 'create 3 icons
       Local hBMP As Dword
       Graphic Bitmap New IconSize, IconSize To hBmp
       Graphic Attach hBMP, 0
       Graphic Color TextColor, BGColor
       'plus
       Graphic Clear BGColor
       Graphic Box (0.43*IconSize,0.2*IconSize)-(0.57*IconSize,0.8*IconSize),,TextColor, TextColor
       Graphic Box (0.2*IconSize,0.43*IconSize)-(0.8*IconSize,0.57*IconSize),,TextColor, TextColor
       Graphic Save "plus.bmp"
       'plus
       Graphic Clear BGColor
       Graphic Box (0.2*IconSize,0.43*IconSize)-(0.8*IconSize,0.57*IconSize),,TextColor, TextColor
       Graphic Save "minus.bmp"
       'empty
       Graphic Clear BGColor
       Graphic Set Pos (0,0.4*IconSize)
       Graphic Print String$(IconSize,".")
       Graphic Save "empty.bmp"
       Graphic Bitmap End
    End Sub
    
    Sub CreateTreeView
       Local hNode As Dword
    
       Treeview Set ImageList hDlg, %IDC_TreeView, hLst
    
       Control Add Treeview, hDlg, %IDC_TreeView, "", 10,60,680,930, %TVS_HasLines Or %WS_TabStop Or %TVS_NonEvenHeight
       Control Handle hDlg, %IDC_TreeView To hTreeView
       Control Set Font hDlg, %IDC_TreeView, hFont
       Control Set Color hDlg, %IDC_TreeView, TextColor, BGColor
    
       LoadTreeView
    
       AssignIcons(0)
       Treeview Get Root hDlg, %IDC_TreeView To hNode
       SetIcon(hNode)
       Treeview Unselect hDlg, %IDC_TreeView
    
    End Sub
    
    Sub CreateFontAndIcons
       Local hNode As Dword
    
       IconSize = 2*FontSize
       Font New "Tahoma", FontSize, 1 To hFont
       Control Set Font hDlg, %IDC_TreeView, hFont
    
       CreateColorIcons
       ImageList New Bitmap IconSize, IconSize,24,5 To hLst
       ImageList Add Bitmap hLst, "empty.bmp"
       ImageList Add Bitmap hLst, "plus.bmp"
       ImageList Add Bitmap hLst, "minus.bmp"
       Treeview Set ImageList hDlg, %IDC_TreeView, hLst
    End Sub

  • #2
    One thing I don't have working perfectly with the code is where to place the horizontal dotted line that connects nodes.

    If you click on the 28pt font size, you'll see this, where the bitmap of dots does not align exactly with the lines drawn by the TreeView. I'll have to think on that some more to figure out how to get correct positioning at any font size.


    Click image for larger version

Name:	pb_2256.jpg
Views:	84
Size:	4.4 KB
ID:	807113

    For that matter, I use simple periods to draw the horizontal line - which is not exactly what the TreeView control does.

    Comment

    Working...
    X