Treeview Background Image

  • VB.NET

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.
    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

    1. Imports System.ComponentModel
    2. ' -----------------------------------------------------------------------------
    3. ' Copyright © 2007 Stephen J Whiteley
    4. ' Copyright © 2007 Timothy C. Alvord
    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 - PlusMinus, Arrow
    17. ' ShadedParentStyle - Shades from ShadedParentColor to BackColor
    18. ' CollapsibleParent - Allow Root Node to be collapsed
    19. ' SelectableParent - Allow Root Node to be selected
    20. '
    21. ' TreeBackgroundImage - Image for Background
    22. ' TreeBackgroundImageLayout - Center, None, Stretch, Tile, Zoom
    23. ' TreeNodeLineType - Custom (Defaults to Dot), Dash, DashDot,
    24. ' DashDotDot, Dot, Solid
    25. ' DefaultToRootNodeImage - Draws Root Node Image for all Nodes which
    26. ' don't have an image defined.
    27. ' ShadedParentColor - Top Color of Gradient
    28. ' TreeNodeHighlightColor - Solid Highlight Color
    29. ' TreeNodeHighlightBorderColor - Solid Highlight Border Color
    30. '
    31. ' Properties/methods Removed/Readonly:
    32. ' DrawMode
    33. ' FullRowSelect
    34. ' LabelEdit
    35. ' ShowPlusMinus
    36. ' Checkboxes
    37. '
    38. ' Shadowed:
    39. ' ImageList
    40. ' Font
    41. '
    42. ' Custom node can be used as a node: contains a 'post text' property
    43. ' which is text which is drawn after the main text.
    44. '
    45. ' Bugs:
    46. ' The horizontal scroll bar does not work correctly when 'posttext' is
    47. ' added to a node.
    48. '
    49. ' If you are using DrawLines without any ImageList, for some reason the control
    50. ' doesn't draw correctly. The workaround is to set an ImageList for the control.
    51. ' Then simply don't assign any images to any of the individual nodes.
    52. '
    53. ' --------------------------------------------------------------------------------
    54. ' Version:
    55. ' --------------------------------------------------------------------------------
    56. ' 1.0 2007-02-17 SJW
    57. ' Initial Release
    58. ' 1.1 2007-04-17 TCA
    59. ' Modified to add Background Image & Layout
    60. ' Modified to add support for ShowRootLines and ShowLines and LineColor
    61. ' Modified to add LineType
    62. ' Modified to add support for Root Node Image replication.
    63. ' DefaultToRootNodeImage = true - The ImageIndex and SelectedImageIndex
    64. ' of the Tree is replicated to each node.
    65. ' DefaultToRootNodeImage = false - The ImageIndex and SelectedImageIndex
    66. ' of the node is used. If none is set,
    67. ' no Image will be displayed for the node
    68. ' Modified to expose RootColor and Highlight color.
    69. '
    70. ' --------------------------------------------------------------------------------
    71. Public Class Tree : Inherits TreeView
    72. Public Enum TreeExpanderStyle
    73. PlusMinus = 0
    74. Arrow = 1
    75. End Enum
    76. Private CollapsedString As String = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAARpJREFUOE+lk9tKAmEUhfWhfIiew1eyUkkvPCaNdZdHbPCACIqCklZERQZCZhNZeZylS5jRhvlHcAb2zcD61tqLfzsBOGx9BNgZXdwffCKYLCEgFXF2IcN3XoA3nsNJJAtPOK1Ptd5Z+21NdUDwsgxVBRZLFdPZEj9/CyjjOd6VKd6GEzwPfnH/OobryG0OCEilvWK58SIGMLbRmW6ac+fpG1fynRjgX+9sjE0AY1PcfPiCVLAAnMby+s4UGqfWVZDI98QJjqMZ08LoTHG5PUI82xUDPJH0v7YZmyk08U3rA9HMHsBuYbvOFOcaQ4RTt9YJWFix2Ueq8rgpjDszNp0pDl1bAPjCzMoz/hO+xEPvwdYhbS75UGdNtwLNm+LI5h1FwAAAAABJRU5ErkJggg=="
    77. Private CollapsedArrowString As String = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAANFJREFUOE+l00kKhDAQheHq+x/KlaALxXkeF04o4g1ep0J3Y0OEiILoIvnyK9QLAD26GHhyKzc7joNpmmhZFtq2jfZ9p+M4lGsvgTOyrqtEVKWXQN/3YGQcR1nCiDZg2zbatsUZmef5HlBVFZqmQdd1smQYBn3AsizkeY6yLP8Q7U8wTRNpmv4QLhAl+gUMRFGEJEnA76KE6rrWBwzDQBAE4KdAKMsyKoriHvBBSJTQF9H+B7zZdV3yPI9836cwDCmOY/2CO7PxaJDkJN85TbX2Db5d1YfJcQ3TAAAAAElFTkSuQmCC"
    78. Private CollapsedIcon As Image
    79. Private ExpandedString As String = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAO9JREFUOE+lk9sKAVEUhnkoD+E5vJJzuHEshzs1DiGUQ0RRigsKJceRcWZ+tppxmjXKTK2bqe/791p7Lz0AnaaPCbSUDI8mK3iiBbgjebjCOThCGdiDKVh9SZi9nFylWvue9wyVBZ5YEaIIXK4ijqcrtvsLeOGMGX/EeH7AYLJDdyjAYDQpC9yRwk+43d/QAnZstWQGN3prWuC890wdW4IrHZ4W2AJpuWfW52cxuNha0gKLP/E1sNdkBmebC1pg9nFv01aCU/W5ukC6KgrmqjN1AbtnNThentIC9sKUhvf5j3yJ/+6DpkV6bPK/yRJ3A/PE7e2oP8DgAAAAAElFTkSuQmCCAPjCzMoz/hO+xEPvwdYhbS75UGdNtwLNm+LI5h1FwAAAAABJRU5ErkJggg=="
    80. Private ExpandedArrowString As String = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAM9JREFUOE/N0kkKg0AQBdCf+x/KlaALxXkeF04o4g0qXU1sDPQqBhLhI72o17+gH0SEWx8Dd3JrWLa/c/t3Ac/ziOM4Dtm2LWNZFpmmKWMYhsq1tVphmiYw0Pc9tW1LVVVRnueUpilFUURBEEjAdd23tdVhWRZckaZpqCxLiSRJIocFwpfogW3bwMg4juDqXdfRifCwQCCawPd9PbDvO9Z1VQgPMcJ/0QRZloGRMAz1wHEcOJF5njEMA14I6rpGURQSieNYD3z6Hv7oIf1shSf3G9UMQ+Vu/QAAAABJRU5ErkJggg=="
    81. Private ExpandedIcon As Image
    82. '
    83. Private Const ICON_SIZE As Integer = 16 ' All Icons are this size
    84. Private Const EXPANDER_LOCATION As Integer = 18 ' Position to draw the expand/collapse
    85. Private Const ICON_START As Integer = 19 ' Position that the TEXT is drawn for a node
    86. ' Text Formatting
    87. Private TextFormat As New StringFormat(StringFormatFlags.NoWrap)
    88. '
    89. Private HighlightBorderPen As Pen
    90. Private HighlightBrush As SolidBrush
    91. Private HighlightColor As Color = Color.FromArgb(255, 204, 230, 255)
    92. Private RootColor As Color = Color.FromArgb(255, 204, 204, 204)
    93. '
    94. Private _collapsibleParent As Boolean
    95. Private _selectableParent As Boolean
    96. Private _parentShaded As Boolean
    97. Private _haveImages As Boolean
    98. Private _expanderStyle As TreeExpanderStyle
    99. Private _nodeCount As Integer
    100. Private m_BackgroundImage As System.Drawing.Image = Nothing
    101. Private m_BackgroundImageLayout As ImageLayout
    102. Private m_TreeNodeLineType As Drawing2D.DashStyle
    103. Private m_DefaultToRootNodeImage As Boolean
    104. Private m_ShadedParentColor As Color
    105. Private m_TreeNodeHighlightColor As Color
    106. Private m_TreeNodeHighlightBorderColor As Color
    107. '
    108. ' Text Brushes so we don't have to create them each and every time
    109. Private NodeBrush As Brush
    110. Private InvertedNodeBrush As Brush
    111. Private NodeBackgroundBrush As Brush
    112. Private Const WM_HSCROLL As Integer = &H114
    113. Private Const WM_VSCROLL As Integer = &H115
    114. Private Const WM_MOUSEWHEEL As Integer = &H20A
    115. Sub New()
    116. ' ---------------------------------------------------------------------
    117. ' New Treeview
    118. ' ---------------------------------------------------------------------
    119. '
    120. HighlightBrush = New SolidBrush(m_TreeNodeHighlightColor)
    121. MyBase.LineColor = SystemColors.GrayText
    122. _expanderStyle = TreeExpanderStyle.PlusMinus
    123. Call LoadExpanderImages()
    124. '
    125. ' Set the style to double buffered and do all the drawing in
    126. ' the paint event (onPaint) of the treeview.
    127. '
    128. ' This reduces the flicker, but is a lot more work
    129. '
    130. MyBase.SetStyle(ControlStyles.DoubleBuffer _
    131. Or ControlStyles.UserPaint _
    132. Or ControlStyles.AllPaintingInWmPaint, _
    133. True)
    134. MyBase.UpdateStyles() ' Update the styles
    135. '
    136. ' Set the treeview styles
    137. Call SetTreeviewStyle()
    138. End Sub
    139. Protected Overrides Sub WndProc(ByRef m As Message)
    140. Select Case m.Msg
    141. Case WM_HSCROLL
    142. Me.Invalidate()
    143. Case WM_VSCROLL
    144. Me.Invalidate()
    145. Case WM_MOUSEWHEEL
    146. Me.Invalidate()
    147. End Select
    148. MyBase.WndProc(m)
    149. End Sub
    150. Private Sub Tree_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    151. Select Case e.KeyCode
    152. Case Keys.Up, Keys.Down, Keys.Left, Keys.Right
    153. Me.Invalidate()
    154. End Select
    155. End Sub
    156. Private Sub LoadExpanderImages()
    157. ' ---------------------------------------------------------------------
    158. ' Loads the included and defined expander images
    159. ' ---------------------------------------------------------------------
    160. '
    161. Dim b() As Byte
    162. '
    163. ' Expanded Image
    164. Select Case _expanderStyle
    165. Case TreeExpanderStyle.Arrow
    166. b = Convert.FromBase64String(ExpandedArrowString)
    167. Case Else
    168. b = Convert.FromBase64String(ExpandedString)
    169. End Select
    170. Dim ms As New System.IO.MemoryStream()
    171. ms.Write(b, 0, b.Length)
    172. ExpandedIcon = Image.FromStream(ms)
    173. '
    174. ' Collapsed Image
    175. Select Case _expanderStyle
    176. Case TreeExpanderStyle.Arrow
    177. b = Convert.FromBase64String(CollapsedArrowString)
    178. Case Else
    179. b = Convert.FromBase64String(CollapsedString)
    180. End Select
    181. ms.Position = 0
    182. ms.Write(b, 0, b.Length)
    183. CollapsedIcon = Image.FromStream(ms)
    184. ms.Close()
    185. '
    186. End Sub
    187. Public ReadOnly Property NodesVisible() As Integer
    188. Get
    189. ' returns the number of visible nodes
    190. Return _nodeCount
    191. End Get
    192. End Property
    193. <DescriptionAttribute("PlusMinus, Arrow.")> _
    194. Public Property ExpanderStyle() As TreeExpanderStyle
    195. Get
    196. Return _expanderStyle
    197. End Get
    198. Set(ByVal value As TreeExpanderStyle)
    199. If _expanderStyle <> value Then
    200. _expanderStyle = value
    201. ' Load the expander styles and redraw
    202. ' the whole treeview
    203. Call LoadExpanderImages()
    204. MyBase.Invalidate()
    205. End If
    206. End Set
    207. End Property
    208. <DescriptionAttribute("Shades from ShadedParentColor to BackColor.")> _
    209. Public Property ShadedParentStyle() As Boolean
    210. Get
    211. Return _parentShaded
    212. End Get
    213. Set(ByVal value As Boolean)
    214. _parentShaded = value
    215. Me.Invalidate()
    216. End Set
    217. End Property
    218. <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.")> _
    219. Public Property ShadedParentColor() As Color
    220. Get
    221. Return m_ShadedParentColor
    222. End Get
    223. Set(ByVal value As Color)
    224. m_ShadedParentColor = value
    225. Me.Invalidate()
    226. End Set
    227. End Property
    228. <DescriptionAttribute("Solid Highlight Color. This is the highlight rectangle.")> _
    229. Public Property TreeNodeHighlightColor() As Color
    230. Get
    231. Return m_TreeNodeHighlightColor
    232. End Get
    233. Set(ByVal value As Color)
    234. m_TreeNodeHighlightColor = value
    235. Me.Invalidate()
    236. End Set
    237. End Property
    238. <DescriptionAttribute("Solid Highlight Border Color. This is the border that is drawn around the highlight rectangle.")> _
    239. Public Property TreeNodeHighlightBorderColor() As Color
    240. Get
    241. Return m_TreeNodeHighlightBorderColor
    242. End Get
    243. Set(ByVal value As Color)
    244. m_TreeNodeHighlightBorderColor = value
    245. Me.Invalidate()
    246. End Set
    247. End Property
    248. <DescriptionAttribute("Image for Background of Tree.")> _
    249. Public Property TreeBackgroundImage() As System.Drawing.Image
    250. Get
    251. TreeBackgroundImage = m_BackgroundImage
    252. End Get
    253. Set(ByVal value As System.Drawing.Image)
    254. m_BackgroundImage = value
    255. Me.Invalidate()
    256. End Set
    257. End Property
    258. <DescriptionAttribute("Center, None, Stretch, Tile, Zoom.")> _
    259. Public Property TreeBackgroundImageLayout() As ImageLayout
    260. Get
    261. TreeBackgroundImageLayout = m_BackgroundImageLayout
    262. End Get
    263. Set(ByVal value As ImageLayout)
    264. m_BackgroundImageLayout = value
    265. Me.Invalidate()
    266. End Set
    267. End Property
    268. <DescriptionAttribute("Custom (Defaults to Dot), Dash, DashDot, DashDotDot, Dot, Solid.")> _
    269. Public Property TreeNodeLineType() As Drawing2D.DashStyle
    270. Get
    271. TreeNodeLineType = m_TreeNodeLineType
    272. End Get
    273. Set(ByVal value As Drawing2D.DashStyle)
    274. If value = Drawing2D.DashStyle.Custom Then
    275. m_TreeNodeLineType = Drawing2D.DashStyle.Dot
    276. Else
    277. m_TreeNodeLineType = value
    278. End If
    279. Me.Invalidate()
    280. End Set
    281. End Property
    282. <DescriptionAttribute("Draws Root Node Image for all Nodes which don't have an image defined.")> _
    283. Public Property DefaultToRootNodeImage() As Boolean
    284. Get
    285. DefaultToRootNodeImage = m_DefaultToRootNodeImage
    286. End Get
    287. Set(ByVal value As Boolean)
    288. m_DefaultToRootNodeImage = value
    289. Me.Invalidate()
    290. End Set
    291. End Property
    292. <DescriptionAttribute("Allow Root Node to be collapsed.")> _
    293. Public Property CollapsibleParent() As Boolean
    294. Get
    295. Return _collapsibleParent
    296. End Get
    297. Set(ByVal value As Boolean)
    298. _collapsibleParent = value
    299. If _collapsibleParent = True Then
    300. ' If the PARENT it collapsable then
    301. ' the parent must also be selectable
    302. _selectableParent = True
    303. End If
    304. Call SetTreeviewStyle()
    305. End Set
    306. End Property
    307. <DescriptionAttribute("Allow Root Node to be selected.")> _
    308. Public Property SelectableParent() As Boolean
    309. Get
    310. Return _selectableParent
    311. End Get
    312. Set(ByVal value As Boolean)
    313. _selectableParent = value
    314. If _selectableParent = False Then
    315. 'if the PARENT is NOT Selectable
    316. ' then it ca'nt be collapsable
    317. _collapsibleParent = False
    318. End If
    319. Call SetTreeviewStyle()
    320. End Set
    321. End Property
    322. Private Sub SetTreeviewStyle()
    323. ' ---------------------------------------------------------------------
    324. ' Sets the various Treeview settings
    325. ' ---------------------------------------------------------------------
    326. '
    327. MyBase.ShowRootLines = _collapsibleParent
    328. If _collapsibleParent = False Then
    329. ' Ensure that the root nodes are expanded
    330. For Each n As TreeNode In MyBase.Nodes
    331. If n.IsExpanded = False Then n.Expand()
    332. Next
    333. End If
    334. '
    335. ' Unselect the node if it's a parent and are not allowed
    336. ' to select parent nodes
    337. If _selectableParent = False Then
    338. If MyBase.SelectedNode IsNot Nothing AndAlso MyBase.SelectedNode.Level = 0 Then
    339. MyBase.SelectedNode = Nothing
    340. End If
    341. End If
    342. '
    343. ' ensure a node is selected
    344. If MyBase.SelectedNode Is Nothing Then
    345. For Each n As TreeNode In MyBase.Nodes
    346. If _selectableParent Then
    347. MyBase.SelectedNode = n
    348. Exit For
    349. End If
    350. If n.IsExpanded = True Then
    351. ' Select the FIRST Node in this list
    352. If n.Nodes.Count > 0 Then
    353. MyBase.SelectedNode = n.Nodes(0)
    354. Exit For
    355. End If
    356. End If
    357. Next
    358. End If
    359. '
    360. Dim g As Graphics = Me.CreateGraphics
    361. Dim sz As SizeF = g.MeasureString(CollapsedString.Substring(0, 2), Me.Font, 125)
    362. Dim newHeight As Integer = Convert.ToInt32(Math.Ceiling(sz.Height))
    363. If newHeight > MyBase.ItemHeight Then MyBase.ItemHeight = newHeight
    364. g.Dispose()
    365. '
    366. NodeBrush = New SolidBrush(Me.ForeColor)
    367. End Sub
    368. Private Sub CalculateNodes()
    369. ' ---------------------------------------------------------------------
    370. ' Calculate the current number of nodes that can be visible
    371. ' in the treeview
    372. ' ---------------------------------------------------------------------
    373. 'Dim currentCount As Integer = _nodeCount
    374. _nodeCount = NodeRecursiveCount(MyBase.Nodes)
    375. 'If _nodeCount <> currentCount Then
    376. ' RaiseEvent NewNodeCount
    377. 'End If
    378. End Sub
    379. Private Function NodeRecursiveCount(ByVal nodeItems As TreeNodeCollection) As Integer
    380. ' ---------------------------------------------------------------------
    381. ' Recursive function to determine how many nodes are visible
    382. ' ---------------------------------------------------------------------
    383. Dim count As Integer
    384. For Each n As TreeNode In nodeItems
    385. count += 1
    386. If n.IsExpanded Then count += NodeRecursiveCount(n.Nodes)
    387. Next
    388. Return count
    389. End Function
    390. #Region " SHADOWED PROPERTIES "
    391. Public Shadows Property ImageList() As ImageList
    392. Get
    393. Return MyBase.ImageList
    394. End Get
    395. Set(ByVal value As ImageList)
    396. MyBase.ImageList = value
    397. _haveImages = (MyBase.ImageList IsNot Nothing)
    398. End Set
    399. End Property
    400. <Browsable(True), [ReadOnly](False)> _
    401. Public Shadows Property Font() As Font
    402. Get
    403. Return MyBase.Font
    404. End Get
    405. Set(ByVal value As Font)
    406. MyBase.Font = value
    407. Call SetTreeviewStyle()
    408. End Set
    409. End Property
    410. <Browsable(False), [ReadOnly](True)> _
    411. Public Shadows ReadOnly Property DrawMode() As TreeViewDrawMode
    412. Get
    413. Return MyBase.DrawMode
    414. End Get
    415. End Property
    416. <Browsable(False), [ReadOnly](True)> _
    417. Public Shadows ReadOnly Property FullRowSelect() As Boolean
    418. Get
    419. Return MyBase.FullRowSelect
    420. End Get
    421. End Property
    422. <Browsable(False), [ReadOnly](True)> _
    423. Public Shadows ReadOnly Property LabelEdit() As Boolean
    424. Get
    425. Return MyBase.LabelEdit
    426. End Get
    427. End Property
    428. <Browsable(False), [ReadOnly](True)> _
    429. Public Shadows ReadOnly Property ShowPlusMinus() As Boolean
    430. Get
    431. Return MyBase.ShowPlusMinus
    432. End Get
    433. End Property
    434. <Browsable(False), [ReadOnly](True)> _
    435. Public Shadows ReadOnly Property CheckBoxes() As Boolean
    436. Get
    437. Return MyBase.CheckBoxes
    438. End Get
    439. End Property
    440. #End Region
    441. Protected Overrides Sub InitLayout()
    442. ' ---------------------------------------------------------------------
    443. ' New Treeview: Initialize the Layout
    444. ' ---------------------------------------------------------------------
    445. MyBase.InitLayout()
    446. '
    447. ' Overridden properties
    448. MyBase.DrawMode = TreeViewDrawMode.OwnerDrawAll
    449. MyBase.FullRowSelect = True
    450. MyBase.CheckBoxes = False
    451. End Sub
    452. Protected Overrides Sub OnAfterCollapse(ByVal e As System.Windows.Forms.TreeViewEventArgs)
    453. ' ---------------------------------------------------------------------
    454. ' After collapsing - redo the node count
    455. ' ---------------------------------------------------------------------
    456. MyBase.OnAfterCollapse(e)
    457. Call CalculateNodes()
    458. End Sub
    459. Protected Overrides Sub OnAfterExpand(ByVal e As System.Windows.Forms.TreeViewEventArgs)
    460. ' ---------------------------------------------------------------------
    461. ' After Expanding - redo the node count
    462. ' ---------------------------------------------------------------------
    463. MyBase.OnAfterExpand(e)
    464. Call CalculateNodes()
    465. End Sub
    466. Protected Overrides Sub OnParentFontChanged(ByVal e As System.EventArgs)
    467. ' ---------------------------------------------------------------------
    468. ' Override the OnParentFontChanged event
    469. ' ---------------------------------------------------------------------
    470. MyBase.OnParentFontChanged(e)
    471. Call SetTreeviewStyle()
    472. End Sub
    473. Protected Overrides Sub OnBeforeSelect(ByVal e As System.Windows.Forms.TreeViewCancelEventArgs)
    474. ' ---------------------------------------------------------------------
    475. ' Override the OnBeforeSelect event
    476. ' ---------------------------------------------------------------------
    477. MyBase.OnBeforeSelect(e)
    478. If _selectableParent = True Then Return
    479. ' Cannot select the PARENT Node
    480. If e.Node.Parent Is Nothing Then e.Cancel = True
    481. End Sub
    482. Protected Overrides Sub OnBeforeExpand(ByVal e As System.Windows.Forms.TreeViewCancelEventArgs)
    483. ' ---------------------------------------------------------------------
    484. ' Override the OnBeforeExpand event
    485. ' ---------------------------------------------------------------------
    486. MyBase.OnBeforeExpand(e)
    487. MyBase.Invalidate()
    488. If _collapsibleParent = True Then Return
    489. ' If we cannot collapse the parent,
    490. ' then ensure that a parent node isn't expanded.
    491. If e.Node.Parent Is Nothing Then e.Cancel = True
    492. End Sub
    493. Protected Overrides Sub OnBeforeCollapse(ByVal e As System.Windows.Forms.TreeViewCancelEventArgs)
    494. ' ---------------------------------------------------------------------
    495. ' Override the OnBeforeCollapse event
    496. ' ---------------------------------------------------------------------
    497. MyBase.OnBeforeCollapse(e)
    498. MyBase.Invalidate()
    499. If _collapsibleParent = True Then Return
    500. ' If we cannot collapse the parent,
    501. ' then ensure that a parent node isn't collapsed.
    502. If e.Node.Parent Is Nothing Then e.Cancel = True
    503. End Sub
    504. Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs)
    505. ' ---------------------------------------------------------------------
    506. ' Override the OnPaintBackground event
    507. ' ---------------------------------------------------------------------
    508. MyBase.OnPaintBackground(pevent)
    509. If m_BackgroundImage Is Nothing Then
    510. Else
    511. Select Case m_BackgroundImageLayout
    512. Case ImageLayout.Center
    513. 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))
    514. Case ImageLayout.None
    515. pevent.Graphics.DrawImage(m_BackgroundImage, New Rectangle(0, 0, m_BackgroundImage.Width, m_BackgroundImage.Height))
    516. Case ImageLayout.Stretch
    517. pevent.Graphics.DrawImage(m_BackgroundImage, New Rectangle(0, 0, Me.Width, Me.Height))
    518. Case ImageLayout.Tile
    519. Dim iLeft, iTop As Int16
    520. iTop = 0
    521. While iTop < Me.Height
    522. iLeft = 0
    523. While iLeft < Me.Width
    524. pevent.Graphics.DrawImage(m_BackgroundImage, New Rectangle(iLeft, iTop, m_BackgroundImage.Width, m_BackgroundImage.Height))
    525. iLeft = iLeft + m_BackgroundImage.Width
    526. End While
    527. iTop = iTop + m_BackgroundImage.Height
    528. End While
    529. Case ImageLayout.Zoom
    530. Dim dImageWidthRatio, dImageHeightRatio As Double
    531. dImageWidthRatio = Me.Width / m_BackgroundImage.Width
    532. dImageHeightRatio = Me.Height / m_BackgroundImage.Height
    533. If dImageWidthRatio < dImageHeightRatio Then
    534. 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))
    535. Else
    536. 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))
    537. End If
    538. End Select
    539. End If
    540. End Sub
    541. Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    542. ' ---------------------------------------------------------------------
    543. ' Override the OnPaint event to paint the nodes
    544. ' ---------------------------------------------------------------------
    545. MyBase.OnPaint(e)
    546. ' Recalculate the number of nodes
    547. ' Note (This Could Get Sluggish)
    548. Call CalculateNodes()
    549. '
    550. If Me._collapsibleParent = False Then
    551. For Each n As TreeNode In MyBase.Nodes
    552. If n.IsExpanded = False Then n.Expand()
    553. Next
    554. End If
    555. Call PaintNodes(MyBase.Nodes, e)
    556. End Sub
    557. #Region " NODE DRAWING FUNCTIONS "
    558. Private Sub PaintNodes(ByVal parentNodes As TreeNodeCollection, ByVal args As PaintEventArgs)
    559. ' ---------------------------------------------------------------------
    560. ' Paint the nodes
    561. ' ---------------------------------------------------------------------
    562. InvertedNodeBrush = New SolidBrush(Color.FromArgb(255 Xor m_TreeNodeHighlightColor.R, 255 Xor m_TreeNodeHighlightColor.G, 255 Xor m_TreeNodeHighlightColor.B))
    563. For Each n As TreeNode In parentNodes
    564. Dim b As New Rectangle(0, n.Bounds.Y, MyBase.ClientSize.Width, n.Bounds.Height)
    565. If args.ClipRectangle.IntersectsWith(b) Then
    566. ' This node intersects with the clip rectangle, so draw it
    567. b = New Rectangle(n.Bounds.X, n.Bounds.Y, MyBase.ClientSize.Width - n.Bounds.X - 1, n.Bounds.Height)
    568. Call DrawThisNode(args.Graphics, n, b)
    569. End If
    570. ' Check for children nodes
    571. ' (Recursively draw the nodes)
    572. If n.Nodes.Count > 0 Then Call PaintNodes(n.Nodes, args)
    573. Next
    574. End Sub
    575. Private Sub DrawThisNode(ByVal g As Graphics, ByVal node As TreeNode, ByVal rBounds As Rectangle)
    576. ' ---------------------------------------------------------------------
    577. ' Draw a specific node
    578. ' ---------------------------------------------------------------------
    579. '
    580. Dim selected As Boolean = node.IsSelected ' convenience
    581. Dim yOffset As Integer = (MyBase.ItemHeight - ICON_SIZE) \ 2 ' where to draw the images
    582. Dim xIcon As Integer = rBounds.X - ICON_START ' this is where the icon starts
    583. Dim hasExpander As Boolean ' has an Expand/collaps been drawn for this node
    584. Dim hasLines As Boolean ' have lines been drawn for this node
    585. Dim hasImage As Boolean ' has an image been drawn for this node
    586. '
    587. ' Draw expander and image
    588. hasExpander = DrawNodeExpander(g, node, xIcon, rBounds.Y + yOffset)
    589. hasImage = DrawNodeImage(g, node, xIcon, rBounds.Y + yOffset)
    590. hasLines = DrawNodeLines(g, node, xIcon - 12, (rBounds.Y + yOffset) + ICON_SIZE / 2, hasImage, hasExpander)
    591. '
    592. Dim rBorder As Rectangle
    593. ' Reduce the SIZE of the bounds, used for drawing rectangles
    594. rBorder = New Rectangle(rBounds.X, rBounds.Y, rBounds.Width - 1, rBounds.Height - 1)
    595. If node.Level = 0 And _parentShaded Then
    596. ' Draw a Shaded Parent Node
    597. Call DrawParentNodeBackground(g, selected, rBorder)
    598. Else
    599. ' Draw a child/unshaded node
    600. Call DrawNodeBackground(g, selected, rBorder)
    601. End If
    602. ' Draw the actual Text
    603. Dim rText As RectangleF = DrawNodeText(g, node, rBounds)
    604. ' Draw any node customization
    605. Call DrawNodeCustom(g, node, rText)
    606. End Sub
    607. 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
    608. ' ---------------------------------------------------------------------
    609. ' Draw the LINES associated with this node
    610. ' ---------------------------------------------------------------------
    611. If Me.ShowLines = False Then Return False
    612. ' Show Lines: Does this NODE have lines?
    613. '
    614. Dim pStartPoint, pEndPoint As Point
    615. Dim ColorPen As New Pen(Me.LineColor, 1)
    616. Dim iIndex As Int16
    617. ColorPen.DashStyle = m_TreeNodeLineType
    618. ' Draw vertical line in the correct position
    619. If bExpander Then
    620. If node.Parent IsNot Nothing Then
    621. If node.Parent.NextNode IsNot Nothing Then
    622. For iIndex = node.Level - 1 To 1 Step -1
    623. pStartPoint.X = xCenterPosition - iIndex * (ICON_SIZE + 3)
    624. pStartPoint.Y = yCenterPosition - ICON_SIZE \ 2
    625. pEndPoint.X = pStartPoint.X
    626. pEndPoint.Y = yCenterPosition + ICON_SIZE \ 2
    627. g.DrawLine(ColorPen, pStartPoint, pEndPoint)
    628. Next
    629. End If
    630. End If
    631. If node.IsExpanded Then
    632. If bImage Then
    633. pStartPoint.Y = yCenterPosition + ICON_SIZE
    634. Else
    635. pStartPoint.Y = yCenterPosition
    636. End If
    637. pStartPoint.X = xCenterPosition + ICON_SIZE + 3
    638. pEndPoint.X = pStartPoint.X
    639. pEndPoint.Y = yCenterPosition + ICON_SIZE \ 2
    640. Else
    641. pStartPoint.X = xCenterPosition
    642. pStartPoint.Y = yCenterPosition
    643. pEndPoint.X = pStartPoint.X
    644. pEndPoint.Y = pStartPoint.Y
    645. End If
    646. Else
    647. If node.Parent IsNot Nothing Then
    648. If node.Parent.NextNode IsNot Nothing Then
    649. 'For iIndex = 2 To node.Level
    650. For iIndex = node.Level - 1 To 1 Step -1
    651. pStartPoint.X = xCenterPosition - iIndex * (ICON_SIZE + 3)
    652. pStartPoint.Y = yCenterPosition - ICON_SIZE \ 2
    653. pEndPoint.X = pStartPoint.X
    654. pEndPoint.Y = yCenterPosition + ICON_SIZE \ 2
    655. g.DrawLine(ColorPen, pStartPoint, pEndPoint)
    656. Next
    657. End If
    658. End If
    659. pStartPoint.X = xCenterPosition
    660. pStartPoint.Y = yCenterPosition - ICON_SIZE \ 2
    661. pEndPoint.X = pStartPoint.X
    662. If node.NextNode Is Nothing Then
    663. pEndPoint.Y = yCenterPosition
    664. Else
    665. pEndPoint.Y = yCenterPosition + ICON_SIZE \ 2
    666. End If
    667. End If
    668. g.DrawLine(ColorPen, pStartPoint, pEndPoint)
    669. ' Draw horizontal line in the correct position
    670. If bExpander Then
    671. pStartPoint.X = xCenterPosition + ICON_SIZE \ 2
    672. Else
    673. pStartPoint.X = xCenterPosition
    674. End If
    675. pStartPoint.Y = yCenterPosition
    676. If bImage Then
    677. pEndPoint.X = xCenterPosition + ICON_SIZE \ 2 + 2
    678. Else
    679. pEndPoint.X = xCenterPosition + 2 * ICON_SIZE - 2
    680. End If
    681. pEndPoint.Y = yCenterPosition
    682. g.DrawLine(ColorPen, pStartPoint, pEndPoint)
    683. Return True
    684. End Function
    685. Private Function DrawNodeImage(ByVal g As Graphics, ByVal node As TreeNode, ByVal xPosition As Integer, ByVal yPosition As Integer) As Boolean
    686. ' ---------------------------------------------------------------------
    687. ' Draw the IMAGE associated with this node
    688. ' ---------------------------------------------------------------------
    689. If _haveImages = False Then Return False
    690. ' Have images: does this NODE have an image?
    691. '
    692. Dim img As Drawing.Image = Nothing ' Define an image object
    693. '
    694. If node.IsSelected Then
    695. ' SELECTED
    696. If node.SelectedImageIndex >= 0 Then
    697. img = MyBase.ImageList.Images(node.SelectedImageIndex)
    698. ElseIf node.SelectedImageKey <> "" Then
    699. img = MyBase.ImageList.Images(node.SelectedImageKey)
    700. ElseIf DefaultToRootNodeImage = True Then
    701. If node.TreeView.SelectedImageIndex >= 0 Then
    702. img = MyBase.ImageList.Images(node.TreeView.SelectedImageIndex)
    703. ElseIf node.TreeView.SelectedImageKey <> "" Then
    704. img = MyBase.ImageList.Images(node.TreeView.SelectedImageKey)
    705. End If
    706. End If
    707. Else
    708. ' NOT SELECTED
    709. If node.ImageIndex >= 0 Then
    710. img = MyBase.ImageList.Images(node.ImageIndex)
    711. ElseIf node.ImageKey <> "" Then
    712. img = MyBase.ImageList.Images(node.ImageKey)
    713. ElseIf DefaultToRootNodeImage = True Then
    714. If node.TreeView.ImageIndex >= 0 Then
    715. img = MyBase.ImageList.Images(node.TreeView.ImageIndex)
    716. ElseIf node.TreeView.ImageKey <> "" Then
    717. img = MyBase.ImageList.Images(node.TreeView.ImageKey)
    718. End If
    719. End If
    720. End If
    721. If img Is Nothing Then Return False
    722. ' If an image exists, then draw this image at the correct position
    723. g.DrawImage(img, xPosition, yPosition)
    724. Return True
    725. End Function
    726. Private Function DrawNodeExpander(ByVal g As Graphics, ByVal node As TreeNode, ByVal iconLocation As Integer, ByVal yPosition As Integer) As Boolean
    727. ' ---------------------------------------------------------------------
    728. ' Draw the Expander, returning T/F for expander drawn
    729. ' ---------------------------------------------------------------------
    730. ' Draw the PLUS/MINUS Expand/Collapse Icon if necessary
    731. If (node.Level = 0 And _collapsibleParent = False) Then Return False
    732. ' Set the position for the Expand/collapse icon
    733. Dim position As Integer = iconLocation - EXPANDER_LOCATION
    734. Dim result As Boolean
    735. If node.IsExpanded Then
    736. ' Draw the EXPANDED Icon
    737. g.DrawImage(ExpandedIcon, position, yPosition)
    738. result = True
    739. Else
    740. If node.Nodes.Count > 0 Then
    741. ' Draw the Collapsed Icon
    742. g.DrawImage(CollapsedIcon, position, yPosition)
    743. result = True
    744. End If
    745. End If
    746. Return result ' Has an expander been drawn
    747. End Function
    748. Private Sub DrawParentNodeBackground(ByVal g As Graphics, ByVal selected As Boolean, ByVal rBorder As Rectangle)
    749. ' ---------------------------------------------------------------------
    750. ' Draws the background for a PARENT Node
    751. ' ---------------------------------------------------------------------
    752. Dim r As Rectangle
    753. ' Set the rectangle for the 'background' Gradient
    754. r = New Rectangle(rBorder.X, rBorder.Y - 1, rBorder.Width, rBorder.Height + 4)
    755. If r.Width < 1 Then r.Width = 1
    756. If r.Height < 1 Then r.Height = 1
    757. ' Set the FILL Rectangle to be slightly smaller than the actual gradient rectangle
    758. Dim rFill As New Rectangle(r.Left, rBorder.Y, r.Width, rBorder.Height - 1)
    759. '
    760. ' Draw the gradient
    761. Dim lgBrush As Drawing2D.LinearGradientBrush
    762. If selected Then
    763. ' Highlight Node
    764. lgBrush = New Drawing2D.LinearGradientBrush(r, Me.BackColor, m_TreeNodeHighlightColor, Drawing2D.LinearGradientMode.Vertical)
    765. Else
    766. ' Normal Node
    767. lgBrush = New Drawing2D.LinearGradientBrush(r, m_ShadedParentColor, Me.BackColor, Drawing2D.LinearGradientMode.Vertical)
    768. End If
    769. g.FillRectangle(lgBrush, rFill)
    770. If selected Then
    771. HighlightBorderPen = New Pen(m_TreeNodeHighlightBorderColor)
    772. g.DrawRectangle(HighlightBorderPen, rBorder)
    773. HighlightBorderPen = Nothing
    774. End If
    775. End Sub
    776. Private Sub DrawNodeBackground(ByVal g As Graphics, ByVal selected As Boolean, ByVal rBorder As Rectangle)
    777. ' ---------------------------------------------------------------------
    778. ' Draws the background for a REGULAR Node
    779. ' ---------------------------------------------------------------------
    780. If Not selected Then Return ' Nothing to draw
    781. '
    782. ' Highlight to the right of the icons
    783. NodeBackgroundBrush = New SolidBrush(m_TreeNodeHighlightColor)
    784. HighlightBorderPen = New Pen(m_TreeNodeHighlightBorderColor)
    785. g.FillRectangle(NodeBackgroundBrush, rBorder)
    786. g.DrawRectangle(HighlightBorderPen, rBorder)
    787. NodeBackgroundBrush = Nothing
    788. HighlightBorderPen = Nothing
    789. End Sub
    790. Private Function DrawNodeText(ByVal g As Graphics, ByVal node As TreeNode, ByVal rBounds As Rectangle) As RectangleF
    791. ' ---------------------------------------------------------------------
    792. ' Draw the actual node text
    793. ' Returns a rectangle where the text was drawn
    794. ' ---------------------------------------------------------------------
    795. Dim txt As String = node.Text
    796. Dim sz As SizeF = g.MeasureString(txt, Me.Font, rBounds.Width, TextFormat)
    797. Dim textBounds As New RectangleF(rBounds.X, rBounds.Y + Convert.ToInt32((rBounds.Height - sz.Height) / 2), rBounds.Width, Convert.ToInt32(sz.Height))
    798. If node.IsSelected Then
    799. g.DrawString(node.Text, Me.Font, InvertedNodeBrush, textBounds, TextFormat)
    800. Else
    801. g.DrawString(node.Text, Me.Font, NodeBrush, textBounds, TextFormat)
    802. End If
    803. ' Return the rectangle where the text was drawn
    804. textBounds.Width = sz.Width
    805. Return textBounds
    806. End Function
    807. Private Sub DrawNodeCustom(ByVal g As Graphics, ByVal node As TreeNode, ByVal rText As RectangleF)
    808. ' ---------------------------------------------------------------------
    809. ' Draw a CUSTOM Node if it's been supplied
    810. ' ---------------------------------------------------------------------
    811. Dim n As CustomNode = TryCast(node, CustomNode)
    812. If n Is Nothing Then Return ' Nothing is customized
    813. '
    814. ' Draw the CUSTOM Properties for this node
    815. Dim br As Brush
    816. ' Get the Post-text color
    817. Dim col As Color = n.PostTextColor
    818. ' If there isn't a color, use the derault color
    819. If col.IsEmpty Then
    820. br = NodeBrush
    821. Else
    822. br = New SolidBrush(n.PostTextColor)
    823. End If
    824. g.DrawString(String.Format(Globalization.CultureInfo.CurrentUICulture, " {0}", n.PostText), Me.Font, br, rText.X + rText.Width, rText.Y)
    825. End Sub
    826. #End Region
    827. End Class
    828. <Serializable()> Public Class CustomNode : Inherits TreeNode
    829. Private _postText As String
    830. Private _postTextColor As Color
    831. Private _postTextShow As Boolean
    832. Protected Sub New(ByVal info As System.Runtime.Serialization.SerializationInfo, _
    833. ByVal context As System.Runtime.Serialization.StreamingContext)
    834. ' ---------------------------------------------------------------------
    835. ' Constructor for Serialization
    836. ' ---------------------------------------------------------------------
    837. Call MyBase.New(info, context)
    838. End Sub
    839. Public Sub New()
    840. _postTextColor = Color.Empty
    841. _postTextShow = True
    842. _postText = String.Empty
    843. End Sub
    844. Public Property PostText() As String
    845. Get
    846. Return _postText
    847. End Get
    848. Set(ByVal value As String)
    849. _postText = value
    850. Call ForceUpdate()
    851. End Set
    852. End Property
    853. Public Property PostTextColor() As Color
    854. Get
    855. Return _postTextColor
    856. End Get
    857. Set(ByVal value As Color)
    858. _postTextColor = value
    859. Call ForceUpdate()
    860. End Set
    861. End Property
    862. Public Property PostTextShow() As Boolean
    863. Get
    864. Return _postTextShow
    865. End Get
    866. Set(ByVal value As Boolean)
    867. _postTextShow = value
    868. Call ForceUpdate()
    869. End Set
    870. End Property
    871. Private Sub ForceUpdate()
    872. ' ---------------------------------------------------------------------
    873. ' Force a change in the node text to update the
    874. ' node in the parent treeview
    875. ' Rather crude, but appears to work
    876. ' ---------------------------------------------------------------------
    877. MyBase.Text = MyBase.Text
    878. End Sub
    879. End Class


    So sieht es dann aus (einfacher Test als FolderExplorer mit eigenem Hintergrundbild):


    VG,
    Bruno

    Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von „diylab“ ()