1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
Public Class frmTreeViewDrawItemIconD
Private tagIcon As Icon = My.Resources.Boss
Private Sub frmTreeViewDrawItem_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.TreeView1.DrawMode = TreeViewDrawMode.OwnerDrawText
End Sub
Private Sub btnPopulate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPopulate.Click
InitializeTreeView()
End Sub
' Populates a TreeView control with example nodes.
Private Sub InitializeTreeView()
TreeView1.BeginUpdate()
TreeView1.Nodes.Add("Parent")
TreeView1.Nodes(0).Tag = tagIcon
TreeView1.Nodes(0).BackColor = Color.Yellow
TreeView1.Nodes(0).Nodes.Add("Child 1")
TreeView1.Nodes(0).Nodes.Add("Child 2")
TreeView1.Nodes(0).Nodes(1).Nodes.Add("Grandchild")
TreeView1.Nodes(0).Nodes(1).Nodes(0).Nodes.Add("Great Grandchild")
TreeView1.Nodes(0).Nodes(1).Nodes(0).Tag = tagIcon
TreeView1.Nodes(0).Nodes(1).Nodes(0).BackColor = Color.Yellow
TreeView1.Nodes(0).Nodes.Add("Child 3")
TreeView1.Nodes(0).Nodes(2).Tag = tagIcon
TreeView1.Nodes(0).Nodes(2).BackColor = Color.Yellow
TreeView1.EndUpdate()
End Sub
'version icone à droite
Private Sub TreeView1_DrawNode(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawTreeNodeEventArgs) Handles TreeView1.DrawNode
' Retrieve the node font. If the node font has not been set,
' use the TreeView font.
If e.Node.NodeFont Is Nothing Then
e.Node.NodeFont = CType(sender, TreeView).Font
End If
' Draw the background and node text for a selected node.
If (e.State And TreeNodeStates.Selected) <> 0 Then
' Draw the background of the selected node. The NodeBounds
' method makes the highlight rectangle large enough to
' include the icon of a node tag, if one is present.
e.Graphics.FillRectangle(Brushes.Blue, NodeBounds(e.Node))
' Draw the node text.
e.Graphics.DrawString(e.Node.Text, e.Node.NodeFont, Brushes.Red, _
e.Bounds.Left, e.Bounds.Top)
' Use the default background and node text.
Else
e.DrawDefault = True
End If
' If a node tag is present, draw its icon
' to the right of the label text.
If (e.Node.Tag IsNot Nothing) Then
e.Graphics.DrawIcon(CType(e.Node.Tag, Icon), _
NodeBounds(e.Node).Right - CType(e.Node.Tag, Icon).Width, NodeBounds(e.Node).Top)
End If
' If the node has focus, draw the focus rectangle large, making
' it large enough to include the icon of the node tag, if present.
If (e.State And TreeNodeStates.Focused) <> 0 Then
Dim focusPen As New Pen(Color.Black)
Try
focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot
Dim focusBounds As Rectangle = NodeBounds(e.Node)
focusBounds.Size = New Size(focusBounds.Width - 1, _
focusBounds.Height - 1)
e.Graphics.DrawRectangle(focusPen, focusBounds)
Finally
focusPen.Dispose()
End Try
End If
End Sub
'*** CALCUL DU RECTANGLE DE SELECTION ***
'Returns the bounds of the specified node, including the region
'occupied by the node label and any node tag displayed.
Private Function NodeBounds(ByVal node As TreeNode) As Rectangle
'Set the return value to the normal node bounds.
Dim bounds As Rectangle = node.Bounds
If (node.Tag IsNot Nothing) Then
'Retrieve a Graphics object from the TreeView handle
'and use it to calculate the display width of the tag
Dim g As Graphics = TreeView1.CreateGraphics()
'Adjust the node bounds using the calculated value.
'1- mesure largeur du node.text suivant
'le NodeFont utilise & ajuste le rectangle de selection
Dim tagWidth As Integer = CInt(g.MeasureString( _
node.Text, node.NodeFont).Width)
bounds.Width = tagWidth
'2-mesure la largeur d'icon
tagWidth = CType(node.Tag, Icon).Width
'decale le sommet -x- du rectangle de selection
bounds.Offset(tagWidth / 2, 0)
' ------ rappel :une dilatation accroit a gauche et à droite simuultanement)....
'dilate suivant -x- de tagWidth / 2 tel que
bounds = Rectangle.Inflate(bounds, tagWidth / 2, 0)
g.Dispose()
End If
Return bounds
End Function 'NodeBounds
End Class |
Partager