Current location: Hot Scripts Forums » Programming Languages » Windows .NET Programming » TreeView Control - Background Image?


TreeView Control - Background Image?

Reply
  #1 (permalink)  
Old 04-02-07, 06:10 PM
tim8w tim8w is offline
Wannabe Coder
 
Join Date: Nov 2004
Posts: 183
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy TreeView Control - Background Image?

Is there a way to set the background image of a TreeView control like you used to be able to do in VB6 using the Sheridan controls?

Sheridan VB6 SSTree control Properties:

PictureBackground
PictureBackgroundMaskColor
PictureBackgroundStyle
PictureBackgroundUseMask
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 04-11-07, 01:43 PM
tim8w tim8w is offline
Wannabe Coder
 
Join Date: Nov 2004
Posts: 183
Thanks: 0
Thanked 0 Times in 0 Posts
Smile Resolved: TreeView Control - Background Image?

Here's the answer I got from Steven J Whitely in another forum. A new class is created which inherits from TreeView.

Here is the modified class:
vbnet Code:
  1. Imports System.ComponentModel
  2.  
  3. ' -----------------------------------------------------------------------------
  4. ' Copyright © 2007 Stephen J Whiteley
  5. '
  6. ' This source code is provided AS IS, with no warranty expressed or implied,
  7. ' including without limitation, warranties of merchantability or
  8. ' fitness for a particular purpose or any warranty of title or
  9. ' non-infringement. This disclaimer must be passed on whenever the Software
  10. ' is distributed in either source form or as a derivative works.
  11. ' It may be used for both commercial and non-commercial applications.
  12. '
  13. ' -----------------------------------------------------------------------------
  14. ' Notes:
  15. ' Properties Added:
  16. '    ExpanderStyle
  17. '    ShadedParentStyle
  18. '    CollapsibleParent
  19. '    SelectableParent
  20. '   
  21. ' Properties/methods Removed/Readonly:
  22. '    DrawMode
  23. '    FullRowSelect
  24. '    ShowRootLines
  25. '    ShowLines
  26. '    LineColor
  27. '    LabelEdit
  28. '    ShowPlusMinus
  29. '    Checkboxes
  30. '
  31. ' Shadowed:
  32. '    ImageList
  33. '    Font
  34. '
  35. ' Custom node can be used as a node: contains a 'post text' property
  36. ' which is text which is drawn after the main text.
  37. '
  38. ' Bugs:
  39. ' The horizontal scroll bar does not work correctly when 'posttext' is
  40. ' added to a node
  41. '   
  42. ' -----------------------------------------------------------------------------
  43. ' Version:
  44. ' -----------------------------------------------------------------------------
  45. ' 1.0 2007-02-17 SJW
  46. '     Initial Release
  47. ' 1.1 2007-04-11 TCA
  48. '     Modified to add background image
  49. '
  50. ' -----------------------------------------------------------------------------
  51.  
  52. Public Class Tree : Inherits TreeView
  53.  
  54.     Public Enum TreeExpanderStyle
  55.         PlusMinus = 0
  56.         Arrow = 1
  57.     End Enum
  58.  
  59.     Private CollapsedString As String = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAARpJREFUOE+lk9tKAmEUhfWhfIiew1eyUkkvPCaNdZdHbPCACIqCklZERQZCZhNZeZylS5jRhvlHcAb2zcD61tqLfzsBOGx9BNgZXdwffCKYLCEgFXF2IcN3XoA3nsNJJAtPOK1Ptd5Z+21NdUDwsgxVBRZLFdPZEj9/CyjjOd6VKd6GEzwPfnH/OobryG0OCEilvWK58SIGMLbRmW6ac+fpG1fynRjgX+9sjE0AY1PcfPiCVLAAnMby+s4UGqfWVZDI98QJjqMZ08LoTHG5PUI82xUDPJH0v7YZmyk08U3rA9HMHsBuYbvOFOcaQ4RTt9YJWFix2Ueq8rgpjDszNp0pDl1bAPjCzMoz/hO+xEPvwdYhbS75UGdNtwLNm+LI5h1FwAAAAABJRU5ErkJggg=="
  60.     Private CollapsedArrowString As String = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAANFJREFUOE+l00kKhDAQheHq+x/KlaALxXkeF04o4g1ep0J3Y0OEiILoIvnyK9QLAD26GHhyKzc7joNpmmhZFtq2jfZ9p+M4lGsvgTOyrqtEVKWXQN/3YGQcR1nCiDZg2zbatsUZmef5HlBVFZqmQdd1smQYBn3AsizkeY6yLP8Q7U8wTRNpmv4QLhAl+gUMRFGEJEnA76KE6rrWBwzDQBAE4KdAKMsyKoriHvBBSJTQF9H+B7zZdV3yPI9836cwDCmOY/2CO7PxaJDkJN85TbX2Db5d1YfJcQ3TAAAAAElFTkSuQmCC"
  61.     Private CollapsedIcon As Image
  62.     Private ExpandedString As String = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAO9JREFUOE+lk9sKAVEUhnkoD+E5vJJzuHEshzs1DiGUQ0RRigsKJceRcWZ+tppxmjXKTK2bqe/791p7Lz0AnaaPCbSUDI8mK3iiBbgjebjCOThCGdiDKVh9SZi9nFylWvue9wyVBZ5YEaIIXK4ijqcrtvsLeOGMGX/EeH7AYLJDdyjAYDQpC9yRwk+43d/QAnZstWQGN3prWuC890wdW4IrHZ4W2AJpuWfW52cxuNha0gKLP/E1sNdkBmebC1pg9nFv01aCU/W5ukC6KgrmqjN1AbtnNThentIC9sKUhvf5j3yJ/+6DpkV6bPK/yRJ3A/PE7e2oP8DgAAAAAElFTkSuQmCCAPjCzMoz/hO+xEPvwdYhbS75UGdNtwLNm+LI5h1FwAAAAABJRU5ErkJggg=="
  63.     Private ExpandedArrowString As String = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAM9JREFUOE/N0kkKg0AQBdCf+x/KlaALxXkeF04o4g0qXU1sDPQqBhLhI72o17+gH0SEWx8Dd3JrWLa/c/t3Ac/ziOM4Dtm2LWNZFpmmKWMYhsq1tVphmiYw0Pc9tW1LVVVRnueUpilFUURBEEjAdd23tdVhWRZckaZpqCxLiSRJIocFwpfogW3bwMg4juDqXdfRifCwQCCawPd9PbDvO9Z1VQgPMcJ/0QRZloGRMAz1wHEcOJF5njEMA14I6rpGURQSieNYD3z6Hv7oIf1shSf3G9UMQ+Vu/QAAAABJRU5ErkJggg=="
  64.     Private ExpandedIcon As Image
  65.     '
  66.     Private Const ICON_SIZE As Integer = 16 ' All Icons are this size
  67.     Private Const EXPANDER_LOCATION As Integer = 18 ' Position to draw the expand/collapse
  68.     Private Const ICON_START As Integer = 19 ' Position that the TEXT is drawn for a node
  69.     ' Text Formatting
  70.     Private TextFormat As New StringFormat(StringFormatFlags.NoWrap)
  71.     '
  72.     Private HighlightBorderPen As New Pen(Color.FromArgb(255, 127, 157, 185))
  73.     Private HighlightColor As Color = Color.FromArgb(255, 204, 230, 255)
  74.     Private HighlightBrush As New SolidBrush(HighlightColor)
  75.     Private RootColor As Color = Color.FromArgb(255, 204, 204, 204)
  76.     '
  77.     Private _collapsibleParent As Boolean
  78.     Private _selectableParent As Boolean
  79.     Private _parentShaded As Boolean
  80.     Private _haveImages As Boolean
  81.     Private _expanderStyle As TreeExpanderStyle
  82.     Private _nodeCount As Integer
  83.     '
  84.     ' Fonts so we dn't have to create them each and every time
  85.     Private NodeBrush As Brush
  86.  
  87.     Sub New()
  88.         ' ---------------------------------------------------------------------
  89.         ' New Treeview
  90.         ' ---------------------------------------------------------------------
  91.         '
  92.         MyBase.LineColor = SystemColors.GrayText
  93.         _expanderStyle = TreeExpanderStyle.PlusMinus
  94.         Call LoadExpanderImages()
  95.         '
  96.         ' Set the style to double buffered and do all the drawing in
  97.         ' the paint event (onPaint) of the treeview.
  98.         '
  99.         ' This reduces the flicker, but is a lot more work
  100.         '
  101.         MyBase.SetStyle(ControlStyles.DoubleBuffer _
  102.              Or ControlStyles.UserPaint _
  103.              Or ControlStyles.AllPaintingInWmPaint, _
  104.              True)
  105.         MyBase.UpdateStyles() ' Update the styles
  106.         '
  107.         ' Set the treeview styles
  108.         Call SetTreeviewStyle()
  109.     End Sub
  110.  
  111.     Private Sub LoadExpanderImages()
  112.         ' ---------------------------------------------------------------------
  113.         ' Loads the included and defined expander images
  114.         ' ---------------------------------------------------------------------
  115.         '
  116.         Dim b() As Byte
  117.         '
  118.         ' Expanded Image
  119.         Select Case _expanderStyle
  120.             Case TreeExpanderStyle.Arrow
  121.                 b = Convert.FromBase64String(ExpandedArrowString)
  122.             Case Else
  123.                 b = Convert.FromBase64String(ExpandedString)
  124.         End Select
  125.         Dim ms As New System.IO.MemoryStream()
  126.         ms.Write(b, 0, b.Length)
  127.         ExpandedIcon = Image.FromStream(ms)
  128.         '
  129.         ' Collapsed Image
  130.         Select Case _expanderStyle
  131.             Case TreeExpanderStyle.Arrow
  132.                 b = Convert.FromBase64String(CollapsedArrowString)
  133.             Case Else
  134.                 b = Convert.FromBase64String(CollapsedString)
  135.         End Select
  136.         ms.Position = 0
  137.         ms.Write(b, 0, b.Length)
  138.         CollapsedIcon = Image.FromStream(ms)
  139.         ms.Close()
  140.         '
  141.     End Sub
  142.  
  143.     Public ReadOnly Property NodesVisible() As Integer
  144.         Get
  145.             ' returns the number of visible nodes
  146.             Return _nodeCount
  147.         End Get
  148.     End Property
  149.  
  150.     Public Property ExpanderStyle() As TreeExpanderStyle
  151.         Get
  152.             Return _expanderStyle
  153.         End Get
  154.         Set(ByVal value As TreeExpanderStyle)
  155.             If _expanderStyle <> value Then
  156.                 _expanderStyle = value
  157.                 ' Load the expander styles and redraw
  158.                 ' the whole treeview
  159.                 Call LoadExpanderImages()
  160.                 MyBase.Invalidate()
  161.             End If
  162.         End Set
  163.     End Property
  164.  
  165.     Public Property ShadedParentStyle() As Boolean
  166.         Get
  167.             Return _parentShaded
  168.         End Get
  169.         Set(ByVal value As Boolean)
  170.             _parentShaded = value
  171.             Me.Invalidate()
  172.         End Set
  173.     End Property
  174.  
  175.     Public Property CollapsibleParent() As Boolean
  176.         Get
  177.             Return _collapsibleParent
  178.         End Get
  179.         Set(ByVal value As Boolean)
  180.             _collapsibleParent = value
  181.             If _collapsibleParent = True Then
  182.                 ' If the PARENT it collapsable then
  183.                 ' the parent must also be selectable
  184.                 _selectableParent = True
  185.             End If
  186.             Call SetTreeviewStyle()
  187.         End Set
  188.     End Property
  189.  
  190.     Public Property SelectableParent() As Boolean
  191.         Get
  192.             Return _selectableParent
  193.         End Get
  194.         Set(ByVal value As Boolean)
  195.             _selectableParent = value
  196.             If _selectableParent = False Then
  197.                 'if the PARENT is NOT Selectable
  198.                 ' then it ca'nt be collapsable
  199.                 _collapsibleParent = False
  200.             End If
  201.             Call SetTreeviewStyle()
  202.         End Set
  203.     End Property
  204.  
  205.     Private Sub SetTreeviewStyle()
  206.         ' ---------------------------------------------------------------------
  207.         ' Sets the various Treeview settings
  208.         ' ---------------------------------------------------------------------
  209.         '
  210.         MyBase.ShowRootLines = _collapsibleParent
  211.         If _collapsibleParent = False Then
  212.             ' Ensure that the root nodes are expanded
  213.             For Each n As TreeNode In MyBase.Nodes
  214.                 If n.IsExpanded = False Then n.Expand()
  215.             Next
  216.         End If
  217.         '
  218.         ' Unselect the node if it's a parent and are not allowed
  219.         ' to select parent nodes
  220.         If _selectableParent = False Then
  221.             If MyBase.SelectedNode IsNot Nothing AndAlso MyBase.SelectedNode.Level = 0 Then
  222.                 MyBase.SelectedNode = Nothing
  223.             End If
  224.         End If
  225.         '
  226.         ' ensure a node is selected
  227.         If MyBase.SelectedNode Is Nothing Then
  228.             For Each n As TreeNode In MyBase.Nodes
  229.                 If _selectableParent Then
  230.                     MyBase.SelectedNode = n
  231.                     Exit For
  232.                 End If
  233.                 If n.IsExpanded = True Then
  234.                     ' Select the FIRST Node in this list
  235.                     If n.Nodes.Count > 0 Then
  236.                         MyBase.SelectedNode = n.Nodes(0)
  237.                         Exit For
  238.                     End If
  239.                 End If
  240.             Next
  241.         End If
  242.         '
  243.         Dim g As Graphics = Me.CreateGraphics
  244.         Dim sz As SizeF = g.MeasureString(CollapsedString.Substring(0, 2), Me.Font, 125)
  245.         Dim newHeight As Integer = Convert.ToInt32(Math.Ceiling(sz.Height))
  246.         If newHeight > MyBase.ItemHeight Then MyBase.ItemHeight = newHeight
  247.         g.Dispose()
  248.         '
  249.         NodeBrush = New SolidBrush(Me.ForeColor)
  250.         '
  251.     End Sub
  252.  
  253.     Private Sub CalculateNodes()
  254.         ' ---------------------------------------------------------------------
  255.         ' Calculate the current number of nodes that can be visible
  256.         ' in the treeview
  257.         ' ---------------------------------------------------------------------
  258.         'Dim currentCount As Integer = _nodeCount
  259.         _nodeCount = NodeRecursiveCount(MyBase.Nodes)
  260.         'If _nodeCount <> currentCount Then
  261.         ' RaiseEvent NewNodeCount
  262.         'End If
  263.     End Sub
  264.  
  265.     Private Function NodeRecursiveCount(ByVal nodeItems As TreeNodeCollection) As Integer
  266.         ' ---------------------------------------------------------------------
  267.         ' Recursive function to determine how many nodes are visible
  268.         ' ---------------------------------------------------------------------
  269.         Dim count As Integer
  270.         For Each n As TreeNode In nodeItems
  271.             count += 1
  272.             If n.IsExpanded Then count += NodeRecursiveCount(n.Nodes)
  273.         Next
  274.         Return count
  275.     End Function
  276.  
  277. #Region "    SHADOWED PROPERTIES    "
  278.  
  279.     Public Shadows Property ImageList() As ImageList
  280.         Get
  281.             Return MyBase.ImageList
  282.         End Get
  283.         Set(ByVal value As ImageList)
  284.             MyBase.ImageList = value
  285.             _haveImages = (MyBase.ImageList IsNot Nothing)
  286.         End Set
  287.     End Property
  288.  
  289.     <Browsable(True), [ReadOnly](False)> _
  290.     Public Shadows Property Font() As Font
  291.         Get
  292.             Return MyBase.Font
  293.         End Get
  294.         Set(ByVal value As Font)
  295.             MyBase.Font = value
  296.             Call SetTreeviewStyle()
  297.         End Set
  298.     End Property
  299.  
  300.     <Browsable(False), [ReadOnly](True)> _
  301.     Public Shadows ReadOnly Property DrawMode() As TreeViewDrawMode
  302.         Get
  303.             Return MyBase.DrawMode
  304.         End Get
  305.     End Property
  306.  
  307.     <Browsable(False), [ReadOnly](True)> _
  308.     Public Shadows ReadOnly Property FullRowSelect() As Boolean
  309.         Get
  310.             Return MyBase.FullRowSelect
  311.         End Get
  312.     End Property
  313.  
  314.     <Browsable(False), [ReadOnly](True)> _
  315.     Public Shadows ReadOnly Property ShowRootLines() As Boolean
  316.         Get
  317.             Return MyBase.ShowRootLines
  318.         End Get
  319.     End Property
  320.  
  321.     <Browsable(False), [ReadOnly](True)> _
  322.     Public Shadows ReadOnly Property ShowLines() As Boolean
  323.         Get
  324.             Return MyBase.ShowLines
  325.         End Get
  326.     End Property
  327.  
  328.     <Browsable(False), [ReadOnly](True)> _
  329.     Public Shadows ReadOnly Property LabelEdit() As Boolean
  330.         Get
  331.             Return MyBase.LabelEdit
  332.         End Get
  333.     End Property
  334.  
  335.     <Browsable(False), [ReadOnly](True)> _
  336.     Public Shadows ReadOnly Property ShowPlusMinus() As Boolean
  337.         Get
  338.             Return MyBase.ShowPlusMinus
  339.         End Get
  340.     End Property
  341.  
  342.     <Browsable(False), [ReadOnly](True)> _
  343.   Public Shadows ReadOnly Property LineColor() As Color
  344.         Get
  345.             Return MyBase.LineColor
  346.         End Get
  347.     End Property
  348.  
  349.     <Browsable(False), [ReadOnly](True)> _
  350.   Public Shadows ReadOnly Property CheckBoxes() As Boolean
  351.         Get
  352.             Return MyBase.CheckBoxes
  353.         End Get
  354.     End Property
  355.  
  356. #End Region
  357.  
  358.     Protected Overrides Sub InitLayout()
  359.         ' ---------------------------------------------------------------------
  360.         ' New Treeview: Initialize the Layout
  361.         ' ---------------------------------------------------------------------
  362.         MyBase.InitLayout()
  363.         '
  364.         ' Overridden properties
  365.         MyBase.DrawMode = TreeViewDrawMode.OwnerDrawAll
  366.         MyBase.ShowLines = True
  367.         MyBase.FullRowSelect = True
  368.         MyBase.ShowPlusMinus = True
  369.         MyBase.ShowRootLines = True
  370.         MyBase.CheckBoxes = False
  371.         MyBase.LineColor = Color.Black
  372.         '
  373.     End Sub
  374.  
  375.     Protected Overrides Sub OnAfterCollapse(ByVal e As System.Windows.Forms.TreeViewEventArgs)
  376.         ' ---------------------------------------------------------------------
  377.         ' After collapsing - redo the node count
  378.         ' ---------------------------------------------------------------------
  379.         MyBase.OnAfterCollapse(e)
  380.         Call CalculateNodes()
  381.     End Sub
  382.  
  383.     Protected Overrides Sub OnAfterExpand(ByVal e As System.Windows.Forms.TreeViewEventArgs)
  384.         ' ---------------------------------------------------------------------
  385.         ' After Expanding - redo the node count
  386.         ' ---------------------------------------------------------------------
  387.         MyBase.OnAfterExpand(e)
  388.         Call CalculateNodes()
  389.     End Sub
  390.  
  391.     Protected Overrides Sub OnParentFontChanged(ByVal e As System.EventArgs)
  392.         ' ---------------------------------------------------------------------
  393.         ' Override the OnParentFontChanged event
  394.         ' ---------------------------------------------------------------------
  395.         MyBase.OnParentFontChanged(e)
  396.         Call SetTreeviewStyle()
  397.     End Sub
  398.  
  399.     Protected Overrides Sub OnBeforeSelect(ByVal e As System.Windows.Forms.TreeViewCancelEventArgs)
  400.         ' ---------------------------------------------------------------------
  401.         ' Override the OnBeforeSelect event
  402.         ' ---------------------------------------------------------------------
  403.         MyBase.OnBeforeSelect(e)
  404.         If _selectableParent = True Then Return
  405.         ' Cannot select the PARENT Node
  406.         If e.Node.Parent Is Nothing Then e.Cancel = True
  407.     End Sub
  408.  
  409.     Protected Overrides Sub OnBeforeExpand(ByVal e As System.Windows.Forms.TreeViewCancelEventArgs)
  410.         ' ---------------------------------------------------------------------
  411.         ' Override the OnBeforeExpand event
  412.         ' ---------------------------------------------------------------------
  413.         MyBase.OnBeforeExpand(e)
  414.         MyBase.Invalidate()
  415.         If _collapsibleParent = True Then Return
  416.         ' If we cannot collapse the parent,
  417.         ' then ensure that a parent node isn't expanded.
  418.         If e.Node.Parent Is Nothing Then e.Cancel = True
  419.     End Sub
  420.  
  421.     Protected Overrides Sub OnBeforeCollapse(ByVal e As System.Windows.Forms.TreeViewCancelEventArgs)
  422.         ' ---------------------------------------------------------------------
  423.         ' Override the OnBeforeCollapse event
  424.         ' ---------------------------------------------------------------------
  425.         MyBase.OnBeforeCollapse(e)
  426.         MyBase.Invalidate()
  427.         If _collapsibleParent = True Then Return
  428.         ' If we cannot collapse the parent,
  429.         ' then ensure that a parent node isn't collapsed.
  430.         If e.Node.Parent Is Nothing Then e.Cancel = True
  431.     End Sub
  432.  
  433.     Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs)
  434.         MyBase.OnPaintBackground(pevent)
  435.         pevent.Graphics.DrawImage(My.Resources.VeggieBag, New Rectangle(0, 0, My.Resources.VeggieBag.Width, My.Resources.VeggieBag.Height))
  436.     End Sub
  437.  
  438.     Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
  439.         ' ---------------------------------------------------------------------
  440.         ' Override the OnPaint event to paint the nodes
  441.         ' ---------------------------------------------------------------------
  442.         MyBase.OnPaint(e)
  443.         ' Recalculate the number of nodes
  444.         ' Note (This Could Get Sluggish)
  445.         Call CalculateNodes()
  446.         '
  447.         If Me._collapsibleParent = False Then
  448.             For Each n As TreeNode In MyBase.Nodes
  449.                 If n.IsExpanded = False Then n.Expand()
  450.             Next
  451.         End If
  452.         Call PaintNodes(MyBase.Nodes, e)
  453.     End Sub
  454.  
  455. #Region "    NODE DRAWING FUNCTIONS    "
  456.  
  457.     Private Sub PaintNodes(ByVal parentNodes As TreeNodeCollection, ByVal args As PaintEventArgs)
  458.         ' ---------------------------------------------------------------------
  459.         ' Paint the nodes
  460.         ' ---------------------------------------------------------------------
  461.         For Each n As TreeNode In parentNodes
  462.             Dim b As New Rectangle(0, n.Bounds.Y, MyBase.ClientSize.Width, n.Bounds.Height)
  463.             If args.ClipRectangle.IntersectsWith(b) Then
  464.                 ' This node intersects with the clip rectangle, so draw it
  465.                 b = New Rectangle(n.Bounds.X, n.Bounds.Y, MyBase.ClientSize.Width - n.Bounds.X - 1, n.Bounds.Height)
  466.                 Call DrawThisNode(args.Graphics, n, b)
  467.             End If
  468.             ' Check for children nodes
  469.             ' (Recursively draw the nodes)
  470.             If n.Nodes.Count > 0 Then Call PaintNodes(n.Nodes, args)
  471.         Next
  472.     End Sub
  473.  
  474.     Private Sub DrawThisNode(ByVal g As Graphics, ByVal node As TreeNode, ByVal rBounds As Rectangle)
  475.         ' ---------------------------------------------------------------------
  476.         ' Draw a specific node
  477.         ' ---------------------------------------------------------------------
  478.         '
  479.         Dim selected As Boolean = node.IsSelected ' convenience
  480.         Dim yOffset As Integer = (MyBase.ItemHeight - ICON_SIZE) \ 2 ' where to draw the images
  481.         '
  482.         Dim xIcon As Integer = rBounds.X - ICON_START ' this is where the icon starts
  483.         Dim hasExpander As Boolean ' has an Expand/collaps been drawn for this node
  484.         Dim hasImage As Boolean ' has an image been drawn for this node
  485.         '
  486.         ' Draw expander and image
  487.         hasExpander = DrawNodeExpander(g, node, xIcon, rBounds.Y + yOffset)
  488.         hasImage = DrawNodeImage(g, node, xIcon, rBounds.Y + yOffset)
  489.         '
  490.         ' is there any space to the LEFT of the text
  491.         Dim leftWidth As Integer = xIcon + ICON_SIZE
  492.         If hasExpander Then leftWidth -= (EXPANDER_LOCATION + 1)
  493.         If hasImage Or hasExpander Then leftWidth -= ICON_START
  494.         Dim noIcons As Boolean = Not (hasExpander Or hasImage)
  495.         '
  496.         Dim rBorder As Rectangle
  497.         ' Reduce the SIZE of the bounds, used for drawing rectangles
  498.         If noIcons Then
  499.             ' The rectangle Bounds stretches across the full width of the control
  500.             rBorder = New Rectangle(0, rBounds.Y, rBounds.Width - 1 + rBounds.X, rBounds.Height - 1)
  501.             leftWidth = 0 ' Clear the left width value
  502.         Else
  503.             If hasImage Then
  504.                 ' Reduce the SIZE of the bounds, used for drawing rectangles
  505.                 rBorder = New Rectangle(rBounds.X, rBounds.Y, rBounds.Width - 1, rBounds.Height - 1)
  506.             Else
  507.                 ' Reduce the SIZE of the bounds, used for drawing rectangles
  508.                 rBorder = New Rectangle(rBounds.X - ICON_SIZE, rBounds.Y, rBounds.Width + ICON_SIZE - 1, rBounds.Height - 1)
  509.             End If
  510.         End If
  511.         '
  512.         ' Highlighting/background
  513.         If node.Level = 0 And _parentShaded Then
  514.             ' Draw a Shaded Parent Node
  515.             Call DrawParentNodeBackground(g, selected, leftWidth, rBorder)
  516.         Else
  517.             ' Draw a child/unshaded node
  518.             Call DrawNodeBackground(g, selected, leftWidth, rBorder)
  519.         End If
  520.         '
  521.         ' Draw the actual Text
  522.         Dim rText As RectangleF = DrawNodeText(g, node, rBounds)
  523.         ' Draw any node customization
  524.         Call DrawNodeCustom(g, node, rText)
  525.         '
  526.     End Sub
  527.  
  528.     Private Function DrawNodeImage(ByVal g As Graphics, ByVal node As TreeNode, ByVal xPosition As Integer, ByVal yPosition As Integer) As Boolean
  529.         ' ---------------------------------------------------------------------
  530.         ' Draw the IMAGE associated with this node
  531.         ' ---------------------------------------------------------------------
  532.         If _haveImages = False Then Return False
  533.         ' Have images: does this NODE have an image?
  534.         '
  535.         Dim img As Drawing.Image = Nothing ' Define an image object
  536.         '
  537.         If node.IsSelected Then
  538.             ' SELECTED
  539.             If node.SelectedImageIndex >= 0 Then
  540.                 img = MyBase.ImageList.Images(node.SelectedImageIndex)
  541.             ElseIf node.SelectedImageKey IsNot Nothing Then
  542.                 img = MyBase.ImageList.Images(node.SelectedImageKey)
  543.             End If
  544.         Else
  545.             ' NOT SELECTED
  546.             If node.ImageIndex >= 0 Then
  547.                 img = MyBase.ImageList.Images(node.ImageIndex)
  548.             ElseIf node.ImageKey IsNot Nothing Then
  549.                 img = MyBase.ImageList.Images(node.ImageKey)
  550.             End If
  551.         End If
  552.         If img Is Nothing Then Return False
  553.         ' If an image exists, then draw this image at the correct position
  554.         g.DrawImage(img, xPosition, yPosition)
  555.         Return True
  556.     End Function
  557.  
  558.     Private Function DrawNodeExpander(ByVal g As Graphics, ByVal node As TreeNode, ByVal iconLocation As Integer, ByVal yPosition As Integer) As Boolean
  559.         ' ---------------------------------------------------------------------
  560.         ' Draw the Expander, returning T/F for expander drawn
  561.         ' ---------------------------------------------------------------------
  562.         ' Draw the PLUS/MINUS Expand/Collapse Icon if necessary
  563.         If (node.Level = 0 And _collapsibleParent = False) Then Return False
  564.         ' Set the position for the Expand/collapse icon
  565.         Dim position As Integer = iconLocation - EXPANDER_LOCATION
  566.         Dim result As Boolean
  567.         If node.IsExpanded Then
  568.             ' Draw the EXPANDED Icon
  569.             g.DrawImage(ExpandedIcon, position, yPosition)
  570.             result = True
  571.         Else
  572.             If node.Nodes.Count > 0 Then
  573.                 ' Draw the Collapsed Icon
  574.                 g.DrawImage(CollapsedIcon, position, yPosition)
  575.                 result = True
  576.             End If
  577.         End If
  578.         Return result ' Has an expander been drawn
  579.     End Function
  580.  
  581.     Private Sub DrawParentNodeBackground(ByVal g As Graphics, ByVal selected As Boolean, ByVal wLeft As Integer, ByVal rBorder As Rectangle)
  582.         ' ---------------------------------------------------------------------
  583.         ' Draws the background for a PARENT Node
  584.         ' ---------------------------------------------------------------------
  585.         Dim r As Rectangle
  586.         ' Set the rectangle for the 'background' Gradient
  587.         r = New Rectangle(rBorder.X, rBorder.Y - 1, rBorder.Width, rBorder.Height + 4)
  588.         If r.Width < 1 Then r.Width = 1
  589.         If r.Height < 1 Then r.Height = 1
  590.         ' Set the FILL Rectangle to be slightly smaller than the actual gradient rectangle
  591.         Dim rFill As New Rectangle(r.Left, rBorder.Y, r.Width, rBorder.Height - 1)
  592.         '
  593.         ' Draw the gradient
  594.         Dim lgBrush As Drawing2D.LinearGradientBrush
  595.         If selected Then
  596.             ' Highlight Node
  597.             lgBrush = New Drawing2D.LinearGradientBrush(r, HighlightColor, Me.BackColor, Drawing2D.LinearGradientMode.Vertical)
  598.         Else
  599.             ' Normal Node
  600.             lgBrush = New Drawing2D.LinearGradientBrush(r, Me.BackColor, RootColor, Drawing2D.LinearGradientMode.Vertical)
  601.         End If
  602.         g.FillRectangle(lgBrush, rFill)
  603.         If selected Then g.DrawRectangle(HighlightBorderPen, rBorder)
  604.         '
  605.         If wLeft > 0 Then
  606.             ' Draw the LEFT Rectangle if necessary
  607.             ' If a rectangle is needed, then draw the rectangle
  608.             Dim leftRect As New Rectangle(0, rBorder.Y, wLeft, rBorder.Height)
  609.             g.FillRectangle(lgBrush, leftRect)
  610.             If selected Then
  611.                 ' Selected rectangle to the LEFT of the icons
  612.                 g.DrawRectangle(HighlightBorderPen, leftRect)
  613.             End If
  614.         End If
  615.     End Sub
  616.  
  617.     Private Sub DrawNodeBackground(ByVal g As Graphics, ByVal selected As Boolean, ByVal wLeft As Integer, ByVal rBorder As Rectangle)
  618.         ' ---------------------------------------------------------------------
  619.         ' Draws the background for a REGULAR Node
  620.         ' ---------------------------------------------------------------------
  621.         If Not selected Then Return ' Nothing to draw
  622.         '
  623.         ' Highlight either the FULL row, or to the right of the icons
  624.         g.FillRectangle(HighlightBrush, rBorder)
  625.         g.DrawRectangle(HighlightBorderPen, rBorder)
  626.         '
  627.         If wLeft > 0 Then
  628.             ' Draw the LEFT Rectangle
  629.             Dim leftRect As New Rectangle(0, rBorder.Y, wLeft, rBorder.Height)
  630.             g.FillRectangle(HighlightBrush, leftRect)
  631.             g.DrawRectangle(HighlightBorderPen, leftRect)
  632.         End If
  633.     End Sub
  634.  
  635.     Private Function DrawNodeText(ByVal g As Graphics, ByVal node As TreeNode, ByVal rBounds As Rectangle) As RectangleF
  636.         ' ---------------------------------------------------------------------
  637.         ' Draw the actual node text
  638.         ' Returns a rectangle where the text was drawn
  639.         ' ---------------------------------------------------------------------
  640.         Dim txt As String = node.Text
  641.         Dim sz As SizeF = g.MeasureString(txt, Me.Font, rBounds.Width, TextFormat)
  642.         Dim textBounds As New RectangleF(rBounds.X, rBounds.Y + Convert.ToInt32((rBounds.Height - sz.Height) / 2), rBounds.Width, Convert.ToInt32(sz.Height))
  643.         g.DrawString(node.Text, Me.Font, NodeBrush, textBounds, TextFormat)
  644.         ' Return the rectanglewhere the text was drawn
  645.         textBounds.Width = sz.Width
  646.         Return textBounds
  647.     End Function
  648.  
  649.     Private Sub DrawNodeCustom(ByVal g As Graphics, ByVal node As TreeNode, ByVal rText As RectangleF)
  650.         ' ---------------------------------------------------------------------
  651.         ' Draw a CUSTOM Node if it's been supplied
  652.         ' ---------------------------------------------------------------------
  653.         Dim n As CustomNode = TryCast(node, CustomNode)
  654.         If n Is Nothing Then Return ' Nothing is customized
  655.         '
  656.         ' Draw the CUSTOM Properties for this node
  657.         Dim br As Brush
  658.         ' Get the Post-text color
  659.         Dim col As Color = n.PostTextColor
  660.         ' If there isn't a color, use the derault color
  661.         If col.IsEmpty Then
  662.             br = NodeBrush
  663.         Else
  664.             br = New SolidBrush(n.PostTextColor)
  665.         End If
  666.         g.DrawString(String.Format(Globalization.CultureInfo.CurrentUICulture, " {0}", n.PostText), Me.Font, br, rText.X + rText.Width, rText.Y)
  667.     End Sub
  668.  
  669. #End Region
  670.  
  671. End Class
  672.  
  673. <Serializable()> Public Class CustomNode : Inherits TreeNode
  674.  
  675.     Private _postText As String
  676.     Private _postTextColor As Color
  677.     Private _postTextShow As Boolean
  678.  
  679.     Protected Sub New(ByVal info As System.Runtime.Serialization.SerializationInfo, _
  680.                     ByVal context As System.Runtime.Serialization.StreamingContext)
  681.         ' ---------------------------------------------------------------------
  682.         ' Constructor for Serialization
  683.         ' ---------------------------------------------------------------------
  684.         Call MyBase.New(info, context)
  685.     End Sub
  686.  
  687.     Public Sub New()
  688.         _postTextColor = Color.Empty
  689.         _postTextShow = True
  690.         _postText = String.Empty
  691.     End Sub
  692.  
  693.     Public Property PostText() As String
  694.         Get
  695.             Return _postText
  696.         End Get
  697.         Set(ByVal value As String)
  698.             _postText = value
  699.             Call ForceUpdate()
  700.         End Set
  701.     End Property
  702.  
  703.     Public Property PostTextColor() As Color
  704.         Get
  705.             Return _postTextColor
  706.         End Get
  707.         Set(ByVal value As Color)
  708.             _postTextColor = value
  709.             Call ForceUpdate()
  710.         End Set
  711.     End Property
  712.  
  713.     Public Property PostTextShow() As Boolean
  714.         Get
  715.             Return _postTextShow
  716.         End Get
  717.         Set(ByVal value As Boolean)
  718.             _postTextShow = value
  719.             Call ForceUpdate()
  720.         End Set
  721.     End Property
  722.  
  723.     Private Sub ForceUpdate()
  724.         ' ---------------------------------------------------------------------
  725.         ' Force a change in the node text to update the
  726.         ' node in the parent treeview
  727.         ' Rather crude, but appears to work
  728.         ' ---------------------------------------------------------------------
  729.         MyBase.Text = MyBase.Text
  730.     End Sub
  731.  
  732. End Class

Last edited by Nico; 04-11-07 at 02:11 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
fit/stretch background image todayscoffee HTML/XHTML/XML 1 03-17-06 05:25 PM
fit/stretch background image todayscoffee CSS 0 03-14-06 06:49 PM
HTML table = Background Image versus Image problem Mickoes HTML/XHTML/XML 2 02-27-06 02:43 PM
Background image for iframe same as main page riots HTML/XHTML/XML 6 02-17-06 12:22 PM
fading background image script... pedro Script Requests 1 07-20-04 11:27 AM


All times are GMT -5. The time now is 10:37 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.