Treeview Background Image
- VB.NET
Sie verwenden einen veralteten Browser (%browser%) mit Sicherheitsschwachstellen und können nicht alle Funktionen dieser Webseite nutzen.
Hier erfahren Sie, wie einfach Sie Ihren Browser aktualisieren können.
Hier erfahren Sie, wie einfach Sie Ihren Browser aktualisieren können.
Es gibt 6 Antworten in diesem Thema. Der letzte Beitrag () ist von Gelöschter Benutzer.
-
-
Spontane Idee (ungetestet): Eigenes Control von TreeView ableiten und OnPaint überschreiben (MyBase.OnPaint() aufrufen nicht vergessen).Weltherrschaft erlangen: 1%Ist dein Problem erledigt? -> Dann markiere das Thema bitte entsprechend.
Waren Beiträge dieser Diskussion dabei hilfreich? -> Dann klick dort jeweils auf den Hilfreich-Button.
Danke.
-
-
vergisses. BackgroundImage ist in Treeview nicht vorgesehen, und das mti Gewalt da reinzufrickeln wäre ein recht aufwändiger Hack (spontan wüsste ich nicht wie - iwas mit Api, oder von Treeview erben und WndProc überschreiben etc.)
-
-
Hallo Achim,
ich habe hier ein TreeView-Control für Dich gefunden, dass prima mit Hintergrundbild funktioniert!
Von hier habe ich es: KLICK.
Es ist von mir getestet und arbeitet einwandfrei:
Spoiler anzeigen
Quellcode
- Imports System.ComponentModel
- ' -----------------------------------------------------------------------------
- ' Copyright © 2007 Stephen J Whiteley
- ' Copyright © 2007 Timothy C. Alvord
- '
- ' This source code is provided AS IS, with no warranty expressed or implied,
- ' including without limitation, warranties of merchantability or
- ' fitness for a particular purpose or any warranty of title or
- ' non-infringement. This disclaimer must be passed on whenever the Software
- ' is distributed in either source form or as a derivative works.
- ' It may be used for both commercial and non-commercial applications.
- '
- ' -----------------------------------------------------------------------------
- ' Notes:
- ' Properties Added:
- ' ExpanderStyle - PlusMinus, Arrow
- ' ShadedParentStyle - Shades from ShadedParentColor to BackColor
- ' CollapsibleParent - Allow Root Node to be collapsed
- ' SelectableParent - Allow Root Node to be selected
- '
- ' TreeBackgroundImage - Image for Background
- ' TreeBackgroundImageLayout - Center, None, Stretch, Tile, Zoom
- ' TreeNodeLineType - Custom (Defaults to Dot), Dash, DashDot,
- ' DashDotDot, Dot, Solid
- ' DefaultToRootNodeImage - Draws Root Node Image for all Nodes which
- ' don't have an image defined.
- ' ShadedParentColor - Top Color of Gradient
- ' TreeNodeHighlightColor - Solid Highlight Color
- ' TreeNodeHighlightBorderColor - Solid Highlight Border Color
- '
- ' Properties/methods Removed/Readonly:
- ' DrawMode
- ' FullRowSelect
- ' LabelEdit
- ' ShowPlusMinus
- ' Checkboxes
- '
- ' Shadowed:
- ' ImageList
- ' Font
- '
- ' Custom node can be used as a node: contains a 'post text' property
- ' which is text which is drawn after the main text.
- '
- ' Bugs:
- ' The horizontal scroll bar does not work correctly when 'posttext' is
- ' added to a node.
- '
- ' If you are using DrawLines without any ImageList, for some reason the control
- ' doesn't draw correctly. The workaround is to set an ImageList for the control.
- ' Then simply don't assign any images to any of the individual nodes.
- '
- ' --------------------------------------------------------------------------------
- ' Version:
- ' --------------------------------------------------------------------------------
- ' 1.0 2007-02-17 SJW
- ' Initial Release
- ' 1.1 2007-04-17 TCA
- ' Modified to add Background Image & Layout
- ' Modified to add support for ShowRootLines and ShowLines and LineColor
- ' Modified to add LineType
- ' Modified to add support for Root Node Image replication.
- ' DefaultToRootNodeImage = true - The ImageIndex and SelectedImageIndex
- ' of the Tree is replicated to each node.
- ' DefaultToRootNodeImage = false - The ImageIndex and SelectedImageIndex
- ' of the node is used. If none is set,
- ' no Image will be displayed for the node
- ' Modified to expose RootColor and Highlight color.
- '
- ' --------------------------------------------------------------------------------
- Public Class Tree : Inherits TreeView
- Public Enum TreeExpanderStyle
- PlusMinus = 0
- Arrow = 1
- End Enum
- Private CollapsedString As String = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAARpJREFUOE+lk9tKAmEUhfWhfIiew1eyUkkvPCaNdZdHbPCACIqCklZERQZCZhNZeZylS5jRhvlHcAb2zcD61tqLfzsBOGx9BNgZXdwffCKYLCEgFXF2IcN3XoA3nsNJJAtPOK1Ptd5Z+21NdUDwsgxVBRZLFdPZEj9/CyjjOd6VKd6GEzwPfnH/OobryG0OCEilvWK58SIGMLbRmW6ac+fpG1fynRjgX+9sjE0AY1PcfPiCVLAAnMby+s4UGqfWVZDI98QJjqMZ08LoTHG5PUI82xUDPJH0v7YZmyk08U3rA9HMHsBuYbvOFOcaQ4RTt9YJWFix2Ueq8rgpjDszNp0pDl1bAPjCzMoz/hO+xEPvwdYhbS75UGdNtwLNm+LI5h1FwAAAAABJRU5ErkJggg=="
- Private CollapsedArrowString As String = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAANFJREFUOE+l00kKhDAQheHq+x/KlaALxXkeF04o4g1ep0J3Y0OEiILoIvnyK9QLAD26GHhyKzc7joNpmmhZFtq2jfZ9p+M4lGsvgTOyrqtEVKWXQN/3YGQcR1nCiDZg2zbatsUZmef5HlBVFZqmQdd1smQYBn3AsizkeY6yLP8Q7U8wTRNpmv4QLhAl+gUMRFGEJEnA76KE6rrWBwzDQBAE4KdAKMsyKoriHvBBSJTQF9H+B7zZdV3yPI9836cwDCmOY/2CO7PxaJDkJN85TbX2Db5d1YfJcQ3TAAAAAElFTkSuQmCC"
- Private CollapsedIcon As Image
- Private ExpandedString As String = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAO9JREFUOE+lk9sKAVEUhnkoD+E5vJJzuHEshzs1DiGUQ0RRigsKJceRcWZ+tppxmjXKTK2bqe/791p7Lz0AnaaPCbSUDI8mK3iiBbgjebjCOThCGdiDKVh9SZi9nFylWvue9wyVBZ5YEaIIXK4ijqcrtvsLeOGMGX/EeH7AYLJDdyjAYDQpC9yRwk+43d/QAnZstWQGN3prWuC890wdW4IrHZ4W2AJpuWfW52cxuNha0gKLP/E1sNdkBmebC1pg9nFv01aCU/W5ukC6KgrmqjN1AbtnNThentIC9sKUhvf5j3yJ/+6DpkV6bPK/yRJ3A/PE7e2oP8DgAAAAAElFTkSuQmCCAPjCzMoz/hO+xEPvwdYhbS75UGdNtwLNm+LI5h1FwAAAAABJRU5ErkJggg=="
- Private ExpandedArrowString As String = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAM9JREFUOE/N0kkKg0AQBdCf+x/KlaALxXkeF04o4g0qXU1sDPQqBhLhI72o17+gH0SEWx8Dd3JrWLa/c/t3Ac/ziOM4Dtm2LWNZFpmmKWMYhsq1tVphmiYw0Pc9tW1LVVVRnueUpilFUURBEEjAdd23tdVhWRZckaZpqCxLiSRJIocFwpfogW3bwMg4juDqXdfRifCwQCCawPd9PbDvO9Z1VQgPMcJ/0QRZloGRMAz1wHEcOJF5njEMA14I6rpGURQSieNYD3z6Hv7oIf1shSf3G9UMQ+Vu/QAAAABJRU5ErkJggg=="
- Private ExpandedIcon As Image
- '
- Private Const ICON_SIZE As Integer = 16 ' All Icons are this size
- Private Const EXPANDER_LOCATION As Integer = 18 ' Position to draw the expand/collapse
- Private Const ICON_START As Integer = 19 ' Position that the TEXT is drawn for a node
- ' Text Formatting
- Private TextFormat As New StringFormat(StringFormatFlags.NoWrap)
- '
- Private HighlightBorderPen As Pen
- Private HighlightBrush As SolidBrush
- Private HighlightColor As Color = Color.FromArgb(255, 204, 230, 255)
- Private RootColor As Color = Color.FromArgb(255, 204, 204, 204)
- '
- Private _collapsibleParent As Boolean
- Private _selectableParent As Boolean
- Private _parentShaded As Boolean
- Private _haveImages As Boolean
- Private _expanderStyle As TreeExpanderStyle
- Private _nodeCount As Integer
- Private m_BackgroundImage As System.Drawing.Image = Nothing
- Private m_BackgroundImageLayout As ImageLayout
- Private m_TreeNodeLineType As Drawing2D.DashStyle
- Private m_DefaultToRootNodeImage As Boolean
- Private m_ShadedParentColor As Color
- Private m_TreeNodeHighlightColor As Color
- Private m_TreeNodeHighlightBorderColor As Color
- '
- ' Text Brushes so we don't have to create them each and every time
- Private NodeBrush As Brush
- Private InvertedNodeBrush As Brush
- Private NodeBackgroundBrush As Brush
- Private Const WM_HSCROLL As Integer = &H114
- Private Const WM_VSCROLL As Integer = &H115
- Private Const WM_MOUSEWHEEL As Integer = &H20A
- Sub New()
- ' ---------------------------------------------------------------------
- ' New Treeview
- ' ---------------------------------------------------------------------
- '
- HighlightBrush = New SolidBrush(m_TreeNodeHighlightColor)
- MyBase.LineColor = SystemColors.GrayText
- _expanderStyle = TreeExpanderStyle.PlusMinus
- Call LoadExpanderImages()
- '
- ' Set the style to double buffered and do all the drawing in
- ' the paint event (onPaint) of the treeview.
- '
- ' This reduces the flicker, but is a lot more work
- '
- MyBase.SetStyle(ControlStyles.DoubleBuffer _
- Or ControlStyles.UserPaint _
- Or ControlStyles.AllPaintingInWmPaint, _
- True)
- MyBase.UpdateStyles() ' Update the styles
- '
- ' Set the treeview styles
- Call SetTreeviewStyle()
- End Sub
- Protected Overrides Sub WndProc(ByRef m As Message)
- Select Case m.Msg
- Case WM_HSCROLL
- Me.Invalidate()
- Case WM_VSCROLL
- Me.Invalidate()
- Case WM_MOUSEWHEEL
- Me.Invalidate()
- End Select
- MyBase.WndProc(m)
- End Sub
- Private Sub Tree_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
- Select Case e.KeyCode
- Case Keys.Up, Keys.Down, Keys.Left, Keys.Right
- Me.Invalidate()
- End Select
- End Sub
- Private Sub LoadExpanderImages()
- ' ---------------------------------------------------------------------
- ' Loads the included and defined expander images
- ' ---------------------------------------------------------------------
- '
- Dim b() As Byte
- '
- ' Expanded Image
- Select Case _expanderStyle
- Case TreeExpanderStyle.Arrow
- b = Convert.FromBase64String(ExpandedArrowString)
- Case Else
- b = Convert.FromBase64String(ExpandedString)
- End Select
- Dim ms As New System.IO.MemoryStream()
- ms.Write(b, 0, b.Length)
- ExpandedIcon = Image.FromStream(ms)
- '
- ' Collapsed Image
- Select Case _expanderStyle
- Case TreeExpanderStyle.Arrow
- b = Convert.FromBase64String(CollapsedArrowString)
- Case Else
- b = Convert.FromBase64String(CollapsedString)
- End Select
- ms.Position = 0
- ms.Write(b, 0, b.Length)
- CollapsedIcon = Image.FromStream(ms)
- ms.Close()
- '
- End Sub
- Public ReadOnly Property NodesVisible() As Integer
- Get
- ' returns the number of visible nodes
- Return _nodeCount
- End Get
- End Property
- <DescriptionAttribute("PlusMinus, Arrow.")> _
- Public Property ExpanderStyle() As TreeExpanderStyle
- Get
- Return _expanderStyle
- End Get
- Set(ByVal value As TreeExpanderStyle)
- If _expanderStyle <> value Then
- _expanderStyle = value
- ' Load the expander styles and redraw
- ' the whole treeview
- Call LoadExpanderImages()
- MyBase.Invalidate()
- End If
- End Set
- End Property
- <DescriptionAttribute("Shades from ShadedParentColor to BackColor.")> _
- Public Property ShadedParentStyle() As Boolean
- Get
- Return _parentShaded
- End Get
- Set(ByVal value As Boolean)
- _parentShaded = value
- Me.Invalidate()
- End Set
- End Property
- <DescriptionAttribute("Top Color of Gradient. Parent gradient is drawn from this color to BackColor. If SelectableParent is true and Root is selected gradient is drawn from BackColor to TreeNodeHighlightColor.")> _
- Public Property ShadedParentColor() As Color
- Get
- Return m_ShadedParentColor
- End Get
- Set(ByVal value As Color)
- m_ShadedParentColor = value
- Me.Invalidate()
- End Set
- End Property
- <DescriptionAttribute("Solid Highlight Color. This is the highlight rectangle.")> _
- Public Property TreeNodeHighlightColor() As Color
- Get
- Return m_TreeNodeHighlightColor
- End Get
- Set(ByVal value As Color)
- m_TreeNodeHighlightColor = value
- Me.Invalidate()
- End Set
- End Property
- <DescriptionAttribute("Solid Highlight Border Color. This is the border that is drawn around the highlight rectangle.")> _
- Public Property TreeNodeHighlightBorderColor() As Color
- Get
- Return m_TreeNodeHighlightBorderColor
- End Get
- Set(ByVal value As Color)
- m_TreeNodeHighlightBorderColor = value
- Me.Invalidate()
- End Set
- End Property
- <DescriptionAttribute("Image for Background of Tree.")> _
- Public Property TreeBackgroundImage() As System.Drawing.Image
- Get
- TreeBackgroundImage = m_BackgroundImage
- End Get
- Set(ByVal value As System.Drawing.Image)
- m_BackgroundImage = value
- Me.Invalidate()
- End Set
- End Property
- <DescriptionAttribute("Center, None, Stretch, Tile, Zoom.")> _
- Public Property TreeBackgroundImageLayout() As ImageLayout
- Get
- TreeBackgroundImageLayout = m_BackgroundImageLayout
- End Get
- Set(ByVal value As ImageLayout)
- m_BackgroundImageLayout = value
- Me.Invalidate()
- End Set
- End Property
- <DescriptionAttribute("Custom (Defaults to Dot), Dash, DashDot, DashDotDot, Dot, Solid.")> _
- Public Property TreeNodeLineType() As Drawing2D.DashStyle
- Get
- TreeNodeLineType = m_TreeNodeLineType
- End Get
- Set(ByVal value As Drawing2D.DashStyle)
- If value = Drawing2D.DashStyle.Custom Then
- m_TreeNodeLineType = Drawing2D.DashStyle.Dot
- Else
- m_TreeNodeLineType = value
- End If
- Me.Invalidate()
- End Set
- End Property
- <DescriptionAttribute("Draws Root Node Image for all Nodes which don't have an image defined.")> _
- Public Property DefaultToRootNodeImage() As Boolean
- Get
- DefaultToRootNodeImage = m_DefaultToRootNodeImage
- End Get
- Set(ByVal value As Boolean)
- m_DefaultToRootNodeImage = value
- Me.Invalidate()
- End Set
- End Property
- <DescriptionAttribute("Allow Root Node to be collapsed.")> _
- Public Property CollapsibleParent() As Boolean
- Get
- Return _collapsibleParent
- End Get
- Set(ByVal value As Boolean)
- _collapsibleParent = value
- If _collapsibleParent = True Then
- ' If the PARENT it collapsable then
- ' the parent must also be selectable
- _selectableParent = True
- End If
- Call SetTreeviewStyle()
- End Set
- End Property
- <DescriptionAttribute("Allow Root Node to be selected.")> _
- Public Property SelectableParent() As Boolean
- Get
- Return _selectableParent
- End Get
- Set(ByVal value As Boolean)
- _selectableParent = value
- If _selectableParent = False Then
- 'if the PARENT is NOT Selectable
- ' then it ca'nt be collapsable
- _collapsibleParent = False
- End If
- Call SetTreeviewStyle()
- End Set
- End Property
- Private Sub SetTreeviewStyle()
- ' ---------------------------------------------------------------------
- ' Sets the various Treeview settings
- ' ---------------------------------------------------------------------
- '
- MyBase.ShowRootLines = _collapsibleParent
- If _collapsibleParent = False Then
- ' Ensure that the root nodes are expanded
- For Each n As TreeNode In MyBase.Nodes
- If n.IsExpanded = False Then n.Expand()
- Next
- End If
- '
- ' Unselect the node if it's a parent and are not allowed
- ' to select parent nodes
- If _selectableParent = False Then
- If MyBase.SelectedNode IsNot Nothing AndAlso MyBase.SelectedNode.Level = 0 Then
- MyBase.SelectedNode = Nothing
- End If
- End If
- '
- ' ensure a node is selected
- If MyBase.SelectedNode Is Nothing Then
- For Each n As TreeNode In MyBase.Nodes
- If _selectableParent Then
- MyBase.SelectedNode = n
- Exit For
- End If
- If n.IsExpanded = True Then
- ' Select the FIRST Node in this list
- If n.Nodes.Count > 0 Then
- MyBase.SelectedNode = n.Nodes(0)
- Exit For
- End If
- End If
- Next
- End If
- '
- Dim g As Graphics = Me.CreateGraphics
- Dim sz As SizeF = g.MeasureString(CollapsedString.Substring(0, 2), Me.Font, 125)
- Dim newHeight As Integer = Convert.ToInt32(Math.Ceiling(sz.Height))
- If newHeight > MyBase.ItemHeight Then MyBase.ItemHeight = newHeight
- g.Dispose()
- '
- NodeBrush = New SolidBrush(Me.ForeColor)
- End Sub
- Private Sub CalculateNodes()
- ' ---------------------------------------------------------------------
- ' Calculate the current number of nodes that can be visible
- ' in the treeview
- ' ---------------------------------------------------------------------
- 'Dim currentCount As Integer = _nodeCount
- _nodeCount = NodeRecursiveCount(MyBase.Nodes)
- 'If _nodeCount <> currentCount Then
- ' RaiseEvent NewNodeCount
- 'End If
- End Sub
- Private Function NodeRecursiveCount(ByVal nodeItems As TreeNodeCollection) As Integer
- ' ---------------------------------------------------------------------
- ' Recursive function to determine how many nodes are visible
- ' ---------------------------------------------------------------------
- Dim count As Integer
- For Each n As TreeNode In nodeItems
- count += 1
- If n.IsExpanded Then count += NodeRecursiveCount(n.Nodes)
- Next
- Return count
- End Function
- #Region " SHADOWED PROPERTIES "
- Public Shadows Property ImageList() As ImageList
- Get
- Return MyBase.ImageList
- End Get
- Set(ByVal value As ImageList)
- MyBase.ImageList = value
- _haveImages = (MyBase.ImageList IsNot Nothing)
- End Set
- End Property
- <Browsable(True), [ReadOnly](False)> _
- Public Shadows Property Font() As Font
- Get
- Return MyBase.Font
- End Get
- Set(ByVal value As Font)
- MyBase.Font = value
- Call SetTreeviewStyle()
- End Set
- End Property
- <Browsable(False), [ReadOnly](True)> _
- Public Shadows ReadOnly Property DrawMode() As TreeViewDrawMode
- Get
- Return MyBase.DrawMode
- End Get
- End Property
- <Browsable(False), [ReadOnly](True)> _
- Public Shadows ReadOnly Property FullRowSelect() As Boolean
- Get
- Return MyBase.FullRowSelect
- End Get
- End Property
- <Browsable(False), [ReadOnly](True)> _
- Public Shadows ReadOnly Property LabelEdit() As Boolean
- Get
- Return MyBase.LabelEdit
- End Get
- End Property
- <Browsable(False), [ReadOnly](True)> _
- Public Shadows ReadOnly Property ShowPlusMinus() As Boolean
- Get
- Return MyBase.ShowPlusMinus
- End Get
- End Property
- <Browsable(False), [ReadOnly](True)> _
- Public Shadows ReadOnly Property CheckBoxes() As Boolean
- Get
- Return MyBase.CheckBoxes
- End Get
- End Property
- #End Region
- Protected Overrides Sub InitLayout()
- ' ---------------------------------------------------------------------
- ' New Treeview: Initialize the Layout
- ' ---------------------------------------------------------------------
- MyBase.InitLayout()
- '
- ' Overridden properties
- MyBase.DrawMode = TreeViewDrawMode.OwnerDrawAll
- MyBase.FullRowSelect = True
- MyBase.CheckBoxes = False
- End Sub
- Protected Overrides Sub OnAfterCollapse(ByVal e As System.Windows.Forms.TreeViewEventArgs)
- ' ---------------------------------------------------------------------
- ' After collapsing - redo the node count
- ' ---------------------------------------------------------------------
- MyBase.OnAfterCollapse(e)
- Call CalculateNodes()
- End Sub
- Protected Overrides Sub OnAfterExpand(ByVal e As System.Windows.Forms.TreeViewEventArgs)
- ' ---------------------------------------------------------------------
- ' After Expanding - redo the node count
- ' ---------------------------------------------------------------------
- MyBase.OnAfterExpand(e)
- Call CalculateNodes()
- End Sub
- Protected Overrides Sub OnParentFontChanged(ByVal e As System.EventArgs)
- ' ---------------------------------------------------------------------
- ' Override the OnParentFontChanged event
- ' ---------------------------------------------------------------------
- MyBase.OnParentFontChanged(e)
- Call SetTreeviewStyle()
- End Sub
- Protected Overrides Sub OnBeforeSelect(ByVal e As System.Windows.Forms.TreeViewCancelEventArgs)
- ' ---------------------------------------------------------------------
- ' Override the OnBeforeSelect event
- ' ---------------------------------------------------------------------
- MyBase.OnBeforeSelect(e)
- If _selectableParent = True Then Return
- ' Cannot select the PARENT Node
- If e.Node.Parent Is Nothing Then e.Cancel = True
- End Sub
- Protected Overrides Sub OnBeforeExpand(ByVal e As System.Windows.Forms.TreeViewCancelEventArgs)
- ' ---------------------------------------------------------------------
- ' Override the OnBeforeExpand event
- ' ---------------------------------------------------------------------
- MyBase.OnBeforeExpand(e)
- MyBase.Invalidate()
- If _collapsibleParent = True Then Return
- ' If we cannot collapse the parent,
- ' then ensure that a parent node isn't expanded.
- If e.Node.Parent Is Nothing Then e.Cancel = True
- End Sub
- Protected Overrides Sub OnBeforeCollapse(ByVal e As System.Windows.Forms.TreeViewCancelEventArgs)
- ' ---------------------------------------------------------------------
- ' Override the OnBeforeCollapse event
- ' ---------------------------------------------------------------------
- MyBase.OnBeforeCollapse(e)
- MyBase.Invalidate()
- If _collapsibleParent = True Then Return
- ' If we cannot collapse the parent,
- ' then ensure that a parent node isn't collapsed.
- If e.Node.Parent Is Nothing Then e.Cancel = True
- End Sub
- Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs)
- ' ---------------------------------------------------------------------
- ' Override the OnPaintBackground event
- ' ---------------------------------------------------------------------
- MyBase.OnPaintBackground(pevent)
- If m_BackgroundImage Is Nothing Then
- Else
- Select Case m_BackgroundImageLayout
- Case ImageLayout.Center
- pevent.Graphics.DrawImage(m_BackgroundImage, New Rectangle((Me.Width - m_BackgroundImage.Width) / 2, (Me.Height - m_BackgroundImage.Height) / 2, m_BackgroundImage.Width, m_BackgroundImage.Height))
- Case ImageLayout.None
- pevent.Graphics.DrawImage(m_BackgroundImage, New Rectangle(0, 0, m_BackgroundImage.Width, m_BackgroundImage.Height))
- Case ImageLayout.Stretch
- pevent.Graphics.DrawImage(m_BackgroundImage, New Rectangle(0, 0, Me.Width, Me.Height))
- Case ImageLayout.Tile
- Dim iLeft, iTop As Int16
- iTop = 0
- While iTop < Me.Height
- iLeft = 0
- While iLeft < Me.Width
- pevent.Graphics.DrawImage(m_BackgroundImage, New Rectangle(iLeft, iTop, m_BackgroundImage.Width, m_BackgroundImage.Height))
- iLeft = iLeft + m_BackgroundImage.Width
- End While
- iTop = iTop + m_BackgroundImage.Height
- End While
- Case ImageLayout.Zoom
- Dim dImageWidthRatio, dImageHeightRatio As Double
- dImageWidthRatio = Me.Width / m_BackgroundImage.Width
- dImageHeightRatio = Me.Height / m_BackgroundImage.Height
- If dImageWidthRatio < dImageHeightRatio Then
- pevent.Graphics.DrawImage(m_BackgroundImage, New Rectangle((Me.Width - m_BackgroundImage.Width * dImageWidthRatio) / 2, (Me.Height - m_BackgroundImage.Height * dImageWidthRatio) / 2, m_BackgroundImage.Width * dImageWidthRatio, m_BackgroundImage.Height * dImageWidthRatio))
- Else
- pevent.Graphics.DrawImage(m_BackgroundImage, New Rectangle((Me.Width - m_BackgroundImage.Width * dImageWidthRatio) / 2, (Me.Height - m_BackgroundImage.Height * dImageWidthRatio) / 2, m_BackgroundImage.Width * dImageHeightRatio, m_BackgroundImage.Height * dImageHeightRatio))
- End If
- End Select
- End If
- End Sub
- Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
- ' ---------------------------------------------------------------------
- ' Override the OnPaint event to paint the nodes
- ' ---------------------------------------------------------------------
- MyBase.OnPaint(e)
- ' Recalculate the number of nodes
- ' Note (This Could Get Sluggish)
- Call CalculateNodes()
- '
- If Me._collapsibleParent = False Then
- For Each n As TreeNode In MyBase.Nodes
- If n.IsExpanded = False Then n.Expand()
- Next
- End If
- Call PaintNodes(MyBase.Nodes, e)
- End Sub
- #Region " NODE DRAWING FUNCTIONS "
- Private Sub PaintNodes(ByVal parentNodes As TreeNodeCollection, ByVal args As PaintEventArgs)
- ' ---------------------------------------------------------------------
- ' Paint the nodes
- ' ---------------------------------------------------------------------
- InvertedNodeBrush = New SolidBrush(Color.FromArgb(255 Xor m_TreeNodeHighlightColor.R, 255 Xor m_TreeNodeHighlightColor.G, 255 Xor m_TreeNodeHighlightColor.B))
- For Each n As TreeNode In parentNodes
- Dim b As New Rectangle(0, n.Bounds.Y, MyBase.ClientSize.Width, n.Bounds.Height)
- If args.ClipRectangle.IntersectsWith(b) Then
- ' This node intersects with the clip rectangle, so draw it
- b = New Rectangle(n.Bounds.X, n.Bounds.Y, MyBase.ClientSize.Width - n.Bounds.X - 1, n.Bounds.Height)
- Call DrawThisNode(args.Graphics, n, b)
- End If
- ' Check for children nodes
- ' (Recursively draw the nodes)
- If n.Nodes.Count > 0 Then Call PaintNodes(n.Nodes, args)
- Next
- End Sub
- Private Sub DrawThisNode(ByVal g As Graphics, ByVal node As TreeNode, ByVal rBounds As Rectangle)
- ' ---------------------------------------------------------------------
- ' Draw a specific node
- ' ---------------------------------------------------------------------
- '
- Dim selected As Boolean = node.IsSelected ' convenience
- Dim yOffset As Integer = (MyBase.ItemHeight - ICON_SIZE) \ 2 ' where to draw the images
- Dim xIcon As Integer = rBounds.X - ICON_START ' this is where the icon starts
- Dim hasExpander As Boolean ' has an Expand/collaps been drawn for this node
- Dim hasLines As Boolean ' have lines been drawn for this node
- Dim hasImage As Boolean ' has an image been drawn for this node
- '
- ' Draw expander and image
- hasExpander = DrawNodeExpander(g, node, xIcon, rBounds.Y + yOffset)
- hasImage = DrawNodeImage(g, node, xIcon, rBounds.Y + yOffset)
- hasLines = DrawNodeLines(g, node, xIcon - 12, (rBounds.Y + yOffset) + ICON_SIZE / 2, hasImage, hasExpander)
- '
- Dim rBorder As Rectangle
- ' Reduce the SIZE of the bounds, used for drawing rectangles
- rBorder = New Rectangle(rBounds.X, rBounds.Y, rBounds.Width - 1, rBounds.Height - 1)
- If node.Level = 0 And _parentShaded Then
- ' Draw a Shaded Parent Node
- Call DrawParentNodeBackground(g, selected, rBorder)
- Else
- ' Draw a child/unshaded node
- Call DrawNodeBackground(g, selected, rBorder)
- End If
- ' Draw the actual Text
- Dim rText As RectangleF = DrawNodeText(g, node, rBounds)
- ' Draw any node customization
- Call DrawNodeCustom(g, node, rText)
- End Sub
- Private Function DrawNodeLines(ByVal g As Graphics, ByVal node As TreeNode, ByVal xCenterPosition As Integer, ByVal yCenterPosition As Integer, ByVal bImage As Boolean, ByVal bExpander As Boolean) As Boolean
- ' ---------------------------------------------------------------------
- ' Draw the LINES associated with this node
- ' ---------------------------------------------------------------------
- If Me.ShowLines = False Then Return False
- ' Show Lines: Does this NODE have lines?
- '
- Dim pStartPoint, pEndPoint As Point
- Dim ColorPen As New Pen(Me.LineColor, 1)
- Dim iIndex As Int16
- ColorPen.DashStyle = m_TreeNodeLineType
- ' Draw vertical line in the correct position
- If bExpander Then
- If node.Parent IsNot Nothing Then
- If node.Parent.NextNode IsNot Nothing Then
- For iIndex = node.Level - 1 To 1 Step -1
- pStartPoint.X = xCenterPosition - iIndex * (ICON_SIZE + 3)
- pStartPoint.Y = yCenterPosition - ICON_SIZE \ 2
- pEndPoint.X = pStartPoint.X
- pEndPoint.Y = yCenterPosition + ICON_SIZE \ 2
- g.DrawLine(ColorPen, pStartPoint, pEndPoint)
- Next
- End If
- End If
- If node.IsExpanded Then
- If bImage Then
- pStartPoint.Y = yCenterPosition + ICON_SIZE
- Else
- pStartPoint.Y = yCenterPosition
- End If
- pStartPoint.X = xCenterPosition + ICON_SIZE + 3
- pEndPoint.X = pStartPoint.X
- pEndPoint.Y = yCenterPosition + ICON_SIZE \ 2
- Else
- pStartPoint.X = xCenterPosition
- pStartPoint.Y = yCenterPosition
- pEndPoint.X = pStartPoint.X
- pEndPoint.Y = pStartPoint.Y
- End If
- Else
- If node.Parent IsNot Nothing Then
- If node.Parent.NextNode IsNot Nothing Then
- 'For iIndex = 2 To node.Level
- For iIndex = node.Level - 1 To 1 Step -1
- pStartPoint.X = xCenterPosition - iIndex * (ICON_SIZE + 3)
- pStartPoint.Y = yCenterPosition - ICON_SIZE \ 2
- pEndPoint.X = pStartPoint.X
- pEndPoint.Y = yCenterPosition + ICON_SIZE \ 2
- g.DrawLine(ColorPen, pStartPoint, pEndPoint)
- Next
- End If
- End If
- pStartPoint.X = xCenterPosition
- pStartPoint.Y = yCenterPosition - ICON_SIZE \ 2
- pEndPoint.X = pStartPoint.X
- If node.NextNode Is Nothing Then
- pEndPoint.Y = yCenterPosition
- Else
- pEndPoint.Y = yCenterPosition + ICON_SIZE \ 2
- End If
- End If
- g.DrawLine(ColorPen, pStartPoint, pEndPoint)
- ' Draw horizontal line in the correct position
- If bExpander Then
- pStartPoint.X = xCenterPosition + ICON_SIZE \ 2
- Else
- pStartPoint.X = xCenterPosition
- End If
- pStartPoint.Y = yCenterPosition
- If bImage Then
- pEndPoint.X = xCenterPosition + ICON_SIZE \ 2 + 2
- Else
- pEndPoint.X = xCenterPosition + 2 * ICON_SIZE - 2
- End If
- pEndPoint.Y = yCenterPosition
- g.DrawLine(ColorPen, pStartPoint, pEndPoint)
- Return True
- End Function
- Private Function DrawNodeImage(ByVal g As Graphics, ByVal node As TreeNode, ByVal xPosition As Integer, ByVal yPosition As Integer) As Boolean
- ' ---------------------------------------------------------------------
- ' Draw the IMAGE associated with this node
- ' ---------------------------------------------------------------------
- If _haveImages = False Then Return False
- ' Have images: does this NODE have an image?
- '
- Dim img As Drawing.Image = Nothing ' Define an image object
- '
- If node.IsSelected Then
- ' SELECTED
- If node.SelectedImageIndex >= 0 Then
- img = MyBase.ImageList.Images(node.SelectedImageIndex)
- ElseIf node.SelectedImageKey <> "" Then
- img = MyBase.ImageList.Images(node.SelectedImageKey)
- ElseIf DefaultToRootNodeImage = True Then
- If node.TreeView.SelectedImageIndex >= 0 Then
- img = MyBase.ImageList.Images(node.TreeView.SelectedImageIndex)
- ElseIf node.TreeView.SelectedImageKey <> "" Then
- img = MyBase.ImageList.Images(node.TreeView.SelectedImageKey)
- End If
- End If
- Else
- ' NOT SELECTED
- If node.ImageIndex >= 0 Then
- img = MyBase.ImageList.Images(node.ImageIndex)
- ElseIf node.ImageKey <> "" Then
- img = MyBase.ImageList.Images(node.ImageKey)
- ElseIf DefaultToRootNodeImage = True Then
- If node.TreeView.ImageIndex >= 0 Then
- img = MyBase.ImageList.Images(node.TreeView.ImageIndex)
- ElseIf node.TreeView.ImageKey <> "" Then
- img = MyBase.ImageList.Images(node.TreeView.ImageKey)
- End If
- End If
- End If
- If img Is Nothing Then Return False
- ' If an image exists, then draw this image at the correct position
- g.DrawImage(img, xPosition, yPosition)
- Return True
- End Function
- Private Function DrawNodeExpander(ByVal g As Graphics, ByVal node As TreeNode, ByVal iconLocation As Integer, ByVal yPosition As Integer) As Boolean
- ' ---------------------------------------------------------------------
- ' Draw the Expander, returning T/F for expander drawn
- ' ---------------------------------------------------------------------
- ' Draw the PLUS/MINUS Expand/Collapse Icon if necessary
- If (node.Level = 0 And _collapsibleParent = False) Then Return False
- ' Set the position for the Expand/collapse icon
- Dim position As Integer = iconLocation - EXPANDER_LOCATION
- Dim result As Boolean
- If node.IsExpanded Then
- ' Draw the EXPANDED Icon
- g.DrawImage(ExpandedIcon, position, yPosition)
- result = True
- Else
- If node.Nodes.Count > 0 Then
- ' Draw the Collapsed Icon
- g.DrawImage(CollapsedIcon, position, yPosition)
- result = True
- End If
- End If
- Return result ' Has an expander been drawn
- End Function
- Private Sub DrawParentNodeBackground(ByVal g As Graphics, ByVal selected As Boolean, ByVal rBorder As Rectangle)
- ' ---------------------------------------------------------------------
- ' Draws the background for a PARENT Node
- ' ---------------------------------------------------------------------
- Dim r As Rectangle
- ' Set the rectangle for the 'background' Gradient
- r = New Rectangle(rBorder.X, rBorder.Y - 1, rBorder.Width, rBorder.Height + 4)
- If r.Width < 1 Then r.Width = 1
- If r.Height < 1 Then r.Height = 1
- ' Set the FILL Rectangle to be slightly smaller than the actual gradient rectangle
- Dim rFill As New Rectangle(r.Left, rBorder.Y, r.Width, rBorder.Height - 1)
- '
- ' Draw the gradient
- Dim lgBrush As Drawing2D.LinearGradientBrush
- If selected Then
- ' Highlight Node
- lgBrush = New Drawing2D.LinearGradientBrush(r, Me.BackColor, m_TreeNodeHighlightColor, Drawing2D.LinearGradientMode.Vertical)
- Else
- ' Normal Node
- lgBrush = New Drawing2D.LinearGradientBrush(r, m_ShadedParentColor, Me.BackColor, Drawing2D.LinearGradientMode.Vertical)
- End If
- g.FillRectangle(lgBrush, rFill)
- If selected Then
- HighlightBorderPen = New Pen(m_TreeNodeHighlightBorderColor)
- g.DrawRectangle(HighlightBorderPen, rBorder)
- HighlightBorderPen = Nothing
- End If
- End Sub
- Private Sub DrawNodeBackground(ByVal g As Graphics, ByVal selected As Boolean, ByVal rBorder As Rectangle)
- ' ---------------------------------------------------------------------
- ' Draws the background for a REGULAR Node
- ' ---------------------------------------------------------------------
- If Not selected Then Return ' Nothing to draw
- '
- ' Highlight to the right of the icons
- NodeBackgroundBrush = New SolidBrush(m_TreeNodeHighlightColor)
- HighlightBorderPen = New Pen(m_TreeNodeHighlightBorderColor)
- g.FillRectangle(NodeBackgroundBrush, rBorder)
- g.DrawRectangle(HighlightBorderPen, rBorder)
- NodeBackgroundBrush = Nothing
- HighlightBorderPen = Nothing
- End Sub
- Private Function DrawNodeText(ByVal g As Graphics, ByVal node As TreeNode, ByVal rBounds As Rectangle) As RectangleF
- ' ---------------------------------------------------------------------
- ' Draw the actual node text
- ' Returns a rectangle where the text was drawn
- ' ---------------------------------------------------------------------
- Dim txt As String = node.Text
- Dim sz As SizeF = g.MeasureString(txt, Me.Font, rBounds.Width, TextFormat)
- Dim textBounds As New RectangleF(rBounds.X, rBounds.Y + Convert.ToInt32((rBounds.Height - sz.Height) / 2), rBounds.Width, Convert.ToInt32(sz.Height))
- If node.IsSelected Then
- g.DrawString(node.Text, Me.Font, InvertedNodeBrush, textBounds, TextFormat)
- Else
- g.DrawString(node.Text, Me.Font, NodeBrush, textBounds, TextFormat)
- End If
- ' Return the rectangle where the text was drawn
- textBounds.Width = sz.Width
- Return textBounds
- End Function
- Private Sub DrawNodeCustom(ByVal g As Graphics, ByVal node As TreeNode, ByVal rText As RectangleF)
- ' ---------------------------------------------------------------------
- ' Draw a CUSTOM Node if it's been supplied
- ' ---------------------------------------------------------------------
- Dim n As CustomNode = TryCast(node, CustomNode)
- If n Is Nothing Then Return ' Nothing is customized
- '
- ' Draw the CUSTOM Properties for this node
- Dim br As Brush
- ' Get the Post-text color
- Dim col As Color = n.PostTextColor
- ' If there isn't a color, use the derault color
- If col.IsEmpty Then
- br = NodeBrush
- Else
- br = New SolidBrush(n.PostTextColor)
- End If
- g.DrawString(String.Format(Globalization.CultureInfo.CurrentUICulture, " {0}", n.PostText), Me.Font, br, rText.X + rText.Width, rText.Y)
- End Sub
- #End Region
- End Class
- <Serializable()> Public Class CustomNode : Inherits TreeNode
- Private _postText As String
- Private _postTextColor As Color
- Private _postTextShow As Boolean
- Protected Sub New(ByVal info As System.Runtime.Serialization.SerializationInfo, _
- ByVal context As System.Runtime.Serialization.StreamingContext)
- ' ---------------------------------------------------------------------
- ' Constructor for Serialization
- ' ---------------------------------------------------------------------
- Call MyBase.New(info, context)
- End Sub
- Public Sub New()
- _postTextColor = Color.Empty
- _postTextShow = True
- _postText = String.Empty
- End Sub
- Public Property PostText() As String
- Get
- Return _postText
- End Get
- Set(ByVal value As String)
- _postText = value
- Call ForceUpdate()
- End Set
- End Property
- Public Property PostTextColor() As Color
- Get
- Return _postTextColor
- End Get
- Set(ByVal value As Color)
- _postTextColor = value
- Call ForceUpdate()
- End Set
- End Property
- Public Property PostTextShow() As Boolean
- Get
- Return _postTextShow
- End Get
- Set(ByVal value As Boolean)
- _postTextShow = value
- Call ForceUpdate()
- End Set
- End Property
- Private Sub ForceUpdate()
- ' ---------------------------------------------------------------------
- ' Force a change in the node text to update the
- ' node in the parent treeview
- ' Rather crude, but appears to work
- ' ---------------------------------------------------------------------
- MyBase.Text = MyBase.Text
- End Sub
- End Class
So sieht es dann aus (einfacher Test als FolderExplorer mit eigenem Hintergrundbild):
VG,
BrunoDieser Beitrag wurde bereits 3 mal editiert, zuletzt von „diylab“ ()
-
-
Tags
-
Ähnliche Themen
-
Halfbax - - Off-Topic
-
3 Benutzer haben hier geschrieben
- Gast (5)
- ErfinderDesRades (1)
- Arby (1)