重画系列:重画TreeView控件(可扩展)

最近因公司产品的需要,重写了System.Windows.Form.TreeView,使其具有更好的外观显示,可以在树结点同时显示文字,button,链接文本,。。。还可以自定义自己的显示比如所文本,如下了图所示

好了,让我们来看看怎么实现的吧

首先创建类OwnTreeView继承自System.Windows.Form.TreeView,需要在其中完成主要的重画功能

然后创建一虚类TreeViewColStyle,用于定制树结点的列

最后通过继承重写上面的虚类,生成显示Button的列TreeViewButtonStyle,显示链接的列TreeViewLinkTextStyle,显示一般文本的列TreeViewTextStyle

代码如下

1。OwnTreeView类源代码:

#Region "实现整体TreeView重画控制"
Public Class OwnTreeView
Inherits System.Windows.Forms.TreeView

#Region " Windows 窗体设计器生成的代码 "

Public Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化
Me.SetStyle(ControlStyles.UserPaint Or ControlStyles.Selectable, True)
'Me.SetStyle(ControlStyles.UserMouse, True)
Me.SetStyle(ControlStyles.StandardClick Or ControlStyles.StandardDoubleClick, False)
Me.SetStyle(ControlStyles.ResizeRedraw, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.DoubleBuffer, True)

End Sub

'UserControl1 重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。

  1<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()   
  2components = New System.ComponentModel.Container   
  3m_TVColumnStyles = New ArrayList   
  4End Sub 
  5
  6#End Region   
  7#Region "类变量定义区"   
  8'保存Style对象   
  9Private m_TVColumnStyles As ArrayList   
 10'触发的点击事件   
 11Public Event TreeViewClick(ByVal ClickNode As TreeNode, ByVal CurrentCol As Integer)   
 12'对node.text的分隔显示符号   
 13Private m_Seperator As String = ","   
 14'列缩进距离   
 15Private m_Indent As Integer = Me.Indent   
 16'加号的大小   
 17Private pWid As Integer = 6   
 18'mousedown时鼠标所在的节点   
 19Private SelNode As TreeNode   
 20'mousedown时鼠标所在的列的位置   
 21Private CurrentStyle As Integer   
 22'mousedown时鼠标所在的单元格的bounds   
 23Private SelNodeBound As Rectangle   
 24'自己的graphics对象   
 25Private m_graphics As Graphics = Me.CreateGraphics   
 26'连线的颜色   
 27Private m_LineColor As Color = Color.DarkGreen   
 28'加减号按钮外框的颜色   
 29Private m_PlusMinusBorderColor As Color = Color.DarkGreen   
 30'加减号按钮的颜色   
 31Private m_PlusMinusColor As Color = Color.DarkOrange   
 32#End Region 
 33
 34#Region "类对外属性区"   
 35'设置列分隔符   
 36Public Property Seperator() As String   
 37Get   
 38Return Me.m_Seperator   
 39End Get   
 40Set(ByVal Value As String)   
 41Me.m_Seperator = Value   
 42End Set   
 43End Property   
 44'连线的颜色   
 45Public Property LineColor() As Color   
 46Get   
 47Return Me.m_LineColor   
 48End Get   
 49Set(ByVal Value As Color)   
 50Me.m_LineColor = Value   
 51End Set   
 52End Property   
 53'加减号按钮外框的颜色   
 54Public Property PlusMinusBorderColor() As Color   
 55Get   
 56Return Me.m_PlusMinusBorderColor   
 57End Get   
 58Set(ByVal Value As Color)   
 59Me.m_PlusMinusBorderColor = Value   
 60End Set   
 61End Property   
 62'加减号按钮的颜色   
 63Public Property PlusMinusColor() As Color   
 64Get   
 65Return Me.m_PlusMinusColor   
 66End Get   
 67Set(ByVal Value As Color)   
 68Me.m_PlusMinusColor = Value   
 69End Set   
 70End Property   
 71#End Region 
 72
 73#Region "类对外方法区"   
 74'添加列style   
 75Public Sub AddTreeViewColumnStyles(ByVal ColStyle As TreeViewColStyle)   
 76ColStyle.ParentControl = Me   
 77m_TVColumnStyles.Add(ColStyle)   
 78End Sub   
 79#End Region 
 80
 81#Region "类私有方法区"   
 82'重画的主要函数,实现对每个节点的bounds的分配以及value的分隔   
 83Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)   
 84If IsNothing(Me.Nodes) Then Return   
 85If Me.Nodes.Count &lt; 1 Then Return   
 86Dim node As TreeNode = Me.Nodes(0)   
 87If Not IsNothing(node) Then   
 88PaintNodes(e.Graphics, node)   
 89End If   
 90End Sub   
 91'重画的从属函数,第归实现对每个节点的bounds的分配以及value的分隔   
 92Private Sub PaintNodes(ByRef g As Graphics, ByRef node As TreeNode)   
 93'画当前节点   
 94If IsNothing(node) Then Return   
 95DrawNode(g, node)   
 96If node.IsExpanded Then   
 97'画其第一个子节点   
 98PaintNodes(g, node.FirstNode)   
 99Else   
100'画加号   
101End If   
102'画下一个节点   
103node = node.NextNode   
104If IsNothing(node) Then   
105Return   
106Else   
107PaintNodes(g, node)   
108End If   
109End Sub 
110
111'根据所在节点,取得style的bounds   
112Private Function GetStyleBounds(ByRef g As Graphics, ByRef node As TreeNode, ByVal StylePosition As Integer) As Rectangle   
113Dim str As String = node.Text   
114Dim arrstr As String() = str.Split(Me.m_Seperator)   
115str = String.Join(Me.m_Seperator, arrstr, 0, StylePosition)   
116Dim preSize As SizeF   
117If str = "" Then   
118preSize = New SizeF(0, 0)   
119Else   
120preSize = g.MeasureString(str, Me.Font)   
121End If   
122Dim nxtSize As SizeF = g.MeasureString(Me.m_Seperator &amp; arrstr(StylePosition), Font)   
123Dim bounds As Rectangle = node.Bounds   
124Dim rect As Rectangle = New Rectangle(bounds.X + preSize.Width, bounds.Y, nxtSize.Width, bounds.Height)   
125Return rect   
126End Function   
127'画给定的节点,根据不同列的style执行各自的paint方法   
128'画连线和加减号   
129Private Sub DrawNode(ByRef g As Graphics, ByRef node As TreeNode)   
130Dim Bounds As Rectangle = node.Bounds 
131
132'画各个列   
133Dim strArr() As String = node.Text.Split(Me.m_Seperator)   
134Dim i As Integer   
135For i = 0 To Me.m_TVColumnStyles.Count - 1   
136If i &lt; strArr.Length Then   
137Dim bound As Rectangle = Me.GetStyleBounds(g, node, i)   
138CType(Me.m_TVColumnStyles(i), CustomTreeview.TreeViewColStyle).Paint(g, bound, strArr(i), Font, node.IsSelected)   
139End If   
140Next 
141
142'画连线   
143If Me.ShowLines Then   
144Dim points() As Point   
145Dim NextNode As TreeNode = node.NextNode   
146If IsNothing(NextNode) Then   
147points = New Point() {New Point(Bounds.X - m_Indent / 2, Bounds.Y), _   
148New Point(Bounds.X - m_Indent / 2, Bounds.Y + Me.pWid), _   
149New Point(Bounds.X - pWid / 2, Bounds.Y + Me.pWid)}   
150Else   
151points = New Point() {New Point(Bounds.X - m_Indent / 2, Bounds.Y), _   
152New Point(Bounds.X - m_Indent / 2, Bounds.Y + Bounds.Height), _   
153New Point(Bounds.X - m_Indent / 2, Bounds.Y + Me.pWid), _   
154New Point(Bounds.X - pWid / 2, Bounds.Y + Me.pWid)}   
155'有下一兄弟节点,就画直接之间的连线   
156Dim _points() As Point = New Point() {New Point(Bounds.X - m_Indent / 2, Bounds.Y), _   
157New Point(Bounds.X - m_Indent / 2, NextNode.Bounds.Y)}   
158DrawLines(g, _points)   
159End If   
160DrawLines(g, points)   
161End If 
162
163'画加减号   
164If Me.ShowPlusMinus Then   
165If node.GetNodeCount(True) &gt; 0 Then   
166Dim rect As Rectangle = New Rectangle(Bounds.X - (m_Indent + Me.pWid) / 2, Bounds.Y + Me.pWid / 2, pWid, pWid) 
167
168If node.IsExpanded Then   
169'画减号   
170DrawSymble(g, rect, False)   
171Else '画加号   
172DrawSymble(g, rect, True)   
173End If   
174End If   
175End If   
176End Sub   
177'根据point集合画连线   
178Private Sub DrawLines(ByRef g As Graphics, ByRef Points() As Point)   
179g.DrawLines(New Pen(Me.m_LineColor), Points)   
180End Sub   
181'画加减号   
182Private Sub DrawSymble(ByRef g As Graphics, ByVal rect As Rectangle, ByVal IsPlus As Boolean)   
183g.DrawRectangle(New Pen(Me.m_PlusMinusBorderColor), rect)   
184g.DrawLine(New Pen(Me.m_PlusMinusColor), rect.X, rect.Y + CInt(rect.Width / 2), rect.X + rect.Width, rect.Y + CInt(rect.Width / 2))   
185If IsPlus Then   
186g.DrawLine(New Pen(Me.m_PlusMinusColor), rect.X + CInt(rect.Width / 2), rect.Y, rect.X + CInt(rect.Width / 2), rect.Y + rect.Height)   
187End If   
188End Sub 
189
190'实现在mousedown是的更新,同时对一次完整的Click事件进行记录和控制   
191Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)   
192MyBase.OnMouseDown(e)   
193Me.Refresh()   
194Dim g As Graphics = m_graphics   
195Dim pointmouse As Point = (New Point(e.X, e.Y))   
196Dim node As TreeNode = Me.GetNodeAt(pointmouse)   
197If IsNothing(node) Then Return   
198SelNode = node 
199
200Dim strArr() As String = node.Text.Split(Me.m_Seperator) 
201
202Dim i As Integer   
203For i = 0 To Me.m_TVColumnStyles.Count - 1   
204If i &lt; strArr.Length Then   
205Dim bound As Rectangle = Me.GetStyleBounds(g, node, i)   
206Dim rect As Rectangle = CType(Me.m_TVColumnStyles(i), TreeViewColStyle).GetCellBounds(bound)   
207If rect.Contains(pointmouse) Then   
208'RaiseEvent TreeViewClick(node, i)   
209CurrentStyle = i   
210SelNodeBound = rect   
211Exit For   
212End If   
213End If   
214Next   
215End Sub 
216
217''画背景颜色   
218Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs)   
219Dim g As Graphics = pevent.Graphics   
220If IsNothing(Me.BackgroundImage) Then   
221g.FillRectangle(New SolidBrush(BackColor), pevent.ClipRectangle)   
222Else   
223g.DrawImage(Me.BackgroundImage, pevent.ClipRectangle)   
224End If   
225End Sub   
226''实现在mousedown是的更新,同时判断并实现一次完整的Click事件   
227Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)   
228MyBase.OnMouseUp(e)   
229Me.Refresh()   
230'Dim g As Graphics = Me.CreateGraphics   
231Dim pointmouse As Point = (New Point(e.X, e.Y))   
232Dim node As TreeNode = Me.GetNodeAt(pointmouse)   
233If IsNothing(node) Then Return   
234If SelNodeBound.Contains(pointmouse) Then   
235RaiseEvent TreeViewClick(SelNode, CurrentStyle)   
236End If   
237End Sub   
238''实现鼠标图像变化   
239Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)   
240Dim g As Graphics = m_graphics   
241Dim pointmouse As Point = (New Point(e.X, e.Y))   
242Dim node As TreeNode = Me.GetNodeAt(pointmouse)   
243If IsNothing(node) Then Return   
244Dim IsChangeCursor As Boolean = False   
245Dim strArr() As String = node.Text.Split(Me.m_Seperator)   
246Dim i As Integer   
247Dim ChangedCursor As Cursor   
248For i = 0 To Me.m_TVColumnStyles.Count - 1   
249If i &lt; strArr.Length Then   
250Dim bound As Rectangle = Me.GetStyleBounds(g, node, i)   
251Dim rect As Rectangle = CType(Me.m_TVColumnStyles(i), TreeViewColStyle).GetCellBounds(bound)   
252If rect.Contains(pointmouse) Then   
253ChangedCursor = CType(Me.m_TVColumnStyles(i), TreeViewColStyle).Cursor   
254If ChangedCursor Is Cursors.Default Then   
255Else   
256IsChangeCursor = True   
257Exit For   
258End If   
259End If   
260End If   
261Next   
262If IsChangeCursor Then   
263Me.Cursor = ChangedCursor   
264Else   
265Me.Cursor = Cursors.Default   
266End If   
267MyBase.OnMouseMove(e)   
268End Sub   
269#End Region   
270End Class   
271
272
2732。TreeViewColStyle类源代码: 
274
275#Region "自定义TreeView列Style基类,所有的列style的生成必须继承它"   
276Public MustInherit Class TreeViewColStyle   
277#Region "必须重写的方法"   
278''根据给定的信息自由的画格中的显示   
279Public MustOverride Overloads Sub Paint(ByRef g As Graphics, _   
280ByVal bounds As Rectangle, _   
281ByVal Source As String, _   
282ByVal Font As Font, _   
283ByVal IsSelected As Boolean)   
284''根据给定的Bounds获取到自己裁出的bounds,用于在上一级类中判断鼠标是否处于此bounds   
285Public MustOverride Function GetCellBounds(ByVal Bounds As Rectangle) As Rectangle   
286#End Region   
287'所在的treeview   
288Public WithEvents ParentControl As TreeView   
289'''鼠标图像的显示   
290Private m_Cursor As Cursor = Cursors.Default   
291Public Property Cursor() As Cursor   
292Get   
293Return m_Cursor   
294End Get   
295Set(ByVal Value As Cursor)   
296m_Cursor = Value   
297End Set   
298End Property   
299End Class   
300#End Region 
301
3023。TreeViewButtonStyle类源代码,实现双色button: 
303
304#Region "自定义列style,实现双色button类型的显示"   
305Public Class TreeViewButtonStyle : Inherits TreeViewColStyle   
306#Region "类变量区"   
307'判断是否鼠标按下,以显示鼠标按下的图像   
308Private IsMouseDown As Boolean = False 
309
310Private xMagin As Integer = 2   
311Private yMagin As Integer = 2   
312'按钮的背景色   
313Private m_BackColor As Color = Color.BurlyWood   
314'按钮的前景色   
315Private m_ForeColor As Color = Color.White   
316'按钮的样式   
317Private m_Style As Style = Style.RectStyle   
318'大小   
319Private m_size As Size = New Size(20, 10)   
320Public Enum Style   
321RectStyle = 0   
322EllipseStyle = 1   
323CircleStyle = 2   
324End Enum   
325#End Region 
326
327#Region "对外属性区"   
328'--------------------对外属性--------------   
329'亮色   
330Public Property LightColor() As Color   
331Get   
332Return m_ForeColor   
333End Get   
334Set(ByVal Value As Color)   
335m_ForeColor = Value   
336End Set   
337End Property   
338'暗色   
339Public Property GrayColor() As Color   
340Get   
341Return Me.m_BackColor   
342End Get   
343Set(ByVal Value As Color)   
344Me.m_BackColor = Value   
345End Set   
346End Property   
347Public Property ButtonStyle() As Style   
348Get   
349Return Me.m_Style   
350End Get   
351Set(ByVal Value As Style)   
352Me.m_Style = Value   
353End Set   
354End Property   
355'设置button的大小   
356Public Property Size() As Size   
357Get   
358Return Me.m_size   
359End Get   
360Set(ByVal Value As Size)   
361Me.m_size = Value   
362End Set   
363End Property   
364#End Region 
365
366#Region "类对外方法区"   
367'提供上层调用,得知上层提供的bounds下的实际paint的bounds   
368Public Overrides Function GetCellBounds(ByVal Bounds As Rectangle) As Rectangle   
369Dim rect As Rectangle   
370If IsNothing(m_size) Then   
371rect = Bounds   
372Else   
373If m_size.Width &gt; Bounds.Width Or m_size.Height &gt; Bounds.Height Then   
374rect = Bounds   
375Else   
376rect = New Rectangle(Bounds.X + (Bounds.Width - m_size.Width) / 2, Bounds.Y + (Bounds.Height - m_size.Height) / 2, m_size.Width, m_size.Height)   
377End If   
378End If   
379Return rect   
380End Function   
381'提供给上层调用的paint方法   
382Public Overrides Sub Paint(ByRef g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal Source As String, ByVal Font As Font, ByVal IsSelected As Boolean)   
383Dim rect As Rectangle = GetCellBounds(bounds) 
384
385Dim brush As Drawing.Drawing2D.LinearGradientBrush   
386Dim brush1 As Drawing.Drawing2D.LinearGradientBrush   
387brush = New Drawing.Drawing2D.LinearGradientBrush(New PointF(rect.X, rect.Y), New PointF(rect.X, rect.Y + rect.Height), Me.m_ForeColor, Me.m_BackColor)   
388'brush = New Drawing.Drawing2D.LinearGradientBrush(New PointF(0, 0), New PointF(0, Me.Height), m_BackColor, Me.m_ForeColor) 
389
390If Me.IsMouseDown AndAlso rect.Contains(Me.ParentControl.PointToClient(Me.ParentControl.MousePosition)) Then   
391brush1 = New Drawing.Drawing2D.LinearGradientBrush(New PointF(rect.X + xMagin, rect.Y + yMagin), New PointF(rect.X + xMagin, rect.Y + rect.Height), Color.FromArgb(0, 255, 255, 255), Color.FromArgb(150, 255, 255, 255))   
392Else   
393brush1 = New Drawing.Drawing2D.LinearGradientBrush(New PointF(rect.X + xMagin, rect.Y + yMagin), New PointF(rect.X + xMagin, rect.Y + rect.Height / 2), Color.FromArgb(150, 255, 255, 255), Color.FromArgb(0, 255, 255, 255))   
394End If   
395brush.WrapMode = Drawing.Drawing2D.WrapMode.TileFlipX   
396brush1.WrapMode = Drawing.Drawing2D.WrapMode.TileFlipX   
397Dim rect1 As Rectangle = New Rectangle(rect.X + xMagin, rect.Y + yMagin, rect.Width - 2 * xMagin, rect.Height / 2)   
398Select Case Me.m_Style   
399Case Style.RectStyle   
400Me.DrawRectStyle(g, rect, rect1, brush, brush1)   
401Case Style.EllipseStyle   
402Me.DrawEllipseStyle(g, rect, rect1, brush, brush1)   
403Case Style.CircleStyle   
404Me.DrawEllipseStyle(g, rect, rect1, brush, brush1)   
405Case Else   
406Me.DrawRectStyle(g, rect, rect1, brush, brush1)   
407End Select   
408End Sub   
409#End Region 
410
411#Region "类私有方法区"   
412'画椭圆形按钮   
413Private Sub DrawEllipseStyle(ByRef g As Graphics, ByVal rect As Rectangle, ByVal rect1 As Rectangle, ByVal bbrush As Brush, ByVal fbrush As Brush)   
414g.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias   
415g.FillEllipse(bbrush, rect)   
416g.FillEllipse(fbrush, rect)   
417g.DrawEllipse(New Pen(m_BackColor), rect)   
418End Sub   
419'画方形按钮   
420Private Sub DrawRectStyle(ByRef g As Graphics, ByVal rect As Rectangle, ByVal rect1 As Rectangle, ByVal bbrush As Brush, ByVal fbrush As Brush)   
421g.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias   
422g.FillRectangle(bbrush, rect)   
423g.FillRectangle(fbrush, rect)   
424g.DrawRectangle(New Pen(m_BackColor), rect)   
425End Sub 
426
427Private Sub XpStyleButton_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ParentControl.MouseDown   
428IsMouseDown = True   
429End Sub 
430
431Private Sub XpStyleButton_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ParentControl.MouseUp   
432IsMouseDown = False   
433End Sub   
434#End Region   
435End Class   
4364。TreeViewTextStyle类源代码,自定义列style,实现一般文本类型的显示: 
437
438#Region "自定义列style,实现一般文本类型的显示"   
439Public Class TreeViewTextStyle : Inherits TreeViewColStyle   
440#Region "类变量区"   
441Private xMagin As Integer = 2   
442Private yMagin As Integer = 2   
443'字体颜色   
444Private m_TextColor As Color = SystemColors.ControlDarkDark   
445#End Region 
446
447#Region "对外属性区"   
448Public Property TextColor() As Color   
449Get   
450Return Me.m_TextColor   
451End Get   
452Set(ByVal Value As Color)   
453Me.m_TextColor = Value   
454End Set   
455End Property   
456#End Region 
457
458#Region "类对外方法区"   
459'提供上层调用,得知上层提供的bounds下的实际paint的bounds   
460Public Overrides Function GetCellBounds(ByVal Bounds As Rectangle) As Rectangle   
461Return Bounds   
462End Function   
463'提供给上层调用的paint方法   
464Public Overrides Sub Paint(ByRef g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal Source As String, ByVal Font As Font, ByVal IsSelected As Boolean)   
465'根据bounds画出文本   
466g.DrawString(Source, Font, New SolidBrush(Me.m_TextColor), bounds.X + xMagin, bounds.Y + yMagin)   
467End Sub   
468#End Region   
469End Class   
470#End Region 
471
4725。TreeViewLinkTextStyle类源代码,自定义列style,实现一般链接文本类型的显示: 
473
474#Region "自定义列style,实现一般链接文本类型的显示"   
475Public Class TreeViewLinkTextStyle : Inherits TreeViewColStyle   
476#Region "类变量区"   
477'判断是否鼠标按下,以显示鼠标按下的图像   
478'Private IsMouseDown As Boolean = False 
479
480Private xMagin As Integer = 2   
481Private yMagin As Integer = 2   
482'字体颜色   
483Private m_TextColor As Color = SystemColors.ControlDarkDark   
484#End Region 
485
486#Region "对外属性区"   
487Public Property TextColor() As Color   
488Get   
489Return Me.m_TextColor   
490End Get   
491Set(ByVal Value As Color)   
492Me.m_TextColor = Value   
493End Set   
494End Property   
495#End Region 
496
497#Region "类对外方法区"   
498'提供上层调用,得知上层提供的bounds下的实际paint的bounds   
499Public Overrides Function GetCellBounds(ByVal Bounds As Rectangle) As Rectangle   
500Return Bounds   
501End Function   
502'提供给上层调用的paint方法   
503Public Overrides Sub Paint(ByRef g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal Source As String, ByVal Font As Font, ByVal IsSelected As Boolean)   
504'根据bounds画出文本   
505Dim f As New Font(Font, FontStyle.Underline)   
506g.DrawString(Source, f, New SolidBrush(Me.m_TextColor), bounds.X + xMagin, bounds.Y + yMagin)   
507End Sub   
508#End Region   
509'让鼠标mousemove时形状为小手   
510Public Sub New()   
511Cursor = Cursors.Hand   
512End Sub   
513End Class   
514#End Region 
515
516如果你不满足上面的button列,链接列的显示,还可以自己重写TreeViewColStyle,生成自己想要的列 
517
518事件留了一个接口 
519
520Public Event TreeViewClick(ByVal ClickNode As TreeNode, ByVal CurrentCol As Integer) 
521
522上面的button的点击事件可以通过TreeViewClick获得,其实每一列都有此事件,就看使用时判断CurrentCol 得知是哪一列发出的就行了,谢谢阅读,希望有什么意见能和我联系, [email protected] qq: 44460100 
523
524以下是测试程序源代码: 
525
526Public Class Form1   
527Inherits System.Windows.Forms.Form 
528
529#Region " Windows 窗体设计器生成的代码 " 
530
531Public Sub New()   
532MyBase.New() 
533
534'该调用是 Windows 窗体设计器所必需的。   
535InitializeComponent() 
536
537'在 InitializeComponent() 调用之后添加任何初始化   
538buttonstyle = New CustomTreeview.TreeViewButtonStyle   
539TextStyle = New CustomTreeview.TreeViewTextStyle   
540LinkTextStyle = New CustomTreeview.TreeViewLinkTextStyle   
541LinkTextStyle.TextColor = Color.Blue   
542'TreeView1.BackgroundImage = Image.FromFile(Application.StartupPath &amp; "\begin.gif")   
543TreeView1.AddTreeViewColumnStyles(TextStyle)   
544TreeView1.AddTreeViewColumnStyles(buttonstyle)   
545TreeView1.AddTreeViewColumnStyles(LinkTextStyle)   
546End Sub 
547
548'窗体重写 dispose 以清理组件列表。   
549Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)   
550If disposing Then   
551If Not (components Is Nothing) Then   
552components.Dispose()   
553End If   
554End If   
555MyBase.Dispose(disposing)   
556End Sub 
557
558'Windows 窗体设计器所必需的   
559Private components As System.ComponentModel.IContainer 
560
561'注意: 以下过程是 Windows 窗体设计器所必需的   
562'可以使用 Windows 窗体设计器修改此过程。   
563'不要使用代码编辑器修改它。   
564Friend WithEvents OwnTreeView1 As CustomTreeview.OwnTreeView   
565Friend WithEvents TreeView1 As CustomTreeview.OwnTreeView   
566Friend WithEvents Button1 As System.Windows.Forms.Button   
567Private buttonstyle As CustomTreeview.TreeViewButtonStyle   
568Private TextStyle As CustomTreeview.TreeViewTextStyle   
569Private LinkTextStyle As CustomTreeview.TreeViewLinkTextStyle   
570Friend WithEvents TreeView2 As System.Windows.Forms.TreeView   
571<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()   
572Me.TreeView1 = New CustomTreeview.OwnTreeView   
573Me.Button1 = New System.Windows.Forms.Button   
574Me.TreeView2 = New System.Windows.Forms.TreeView   
575Me.SuspendLayout()   
576'   
577'TreeView1   
578'   
579Me.TreeView1.Cursor = System.Windows.Forms.Cursors.Default   
580Me.TreeView1.ImageIndex = -1   
581Me.TreeView1.Indent = 15   
582Me.TreeView1.LineColor = System.Drawing.Color.DarkGreen   
583Me.TreeView1.Location = New System.Drawing.Point(176, 24)   
584Me.TreeView1.Name = "TreeView1"   
585Me.TreeView1.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("节点0", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("节点0,fdsa", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("节点6,ffff", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("节点7,ffff", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("节点0,fdsf", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("节点1,fdsa", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("节点2,fgas", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("节点3,hhhh", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("节点4,dfgg")})})})})}), New System.Windows.Forms.TreeNode("节点9,ffff")}), New System.Windows.Forms.TreeNode("节点10,ffff")})}), New System.Windows.Forms.TreeNode("节点1,wddd,www.sohu.com"), New System.Windows.Forms.TreeNode("节点2,hgds"), New System.Windows.Forms.TreeNode("节点3,hgfs"), New System.Windows.Forms.TreeNode("节点4,jhgf,www.sina.com", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("节点5,hgfd")}), New System.Windows.Forms.TreeNode("节点8,ffff,中国企业网"), New System.Windows.Forms.TreeNode("节点0"), New System.Windows.Forms.TreeNode("节点1"), New System.Windows.Forms.TreeNode("节点2"), New System.Windows.Forms.TreeNode("节点3"), New System.Windows.Forms.TreeNode("节点4"), New System.Windows.Forms.TreeNode("节点5"), New System.Windows.Forms.TreeNode("节点6")})   
586Me.TreeView1.PlusMinusBorderColor = System.Drawing.Color.DarkGreen   
587Me.TreeView1.PlusMinusColor = System.Drawing.Color.DarkOrange   
588Me.TreeView1.SelectedImageIndex = -1   
589Me.TreeView1.Seperator = ","   
590Me.TreeView1.Size = New System.Drawing.Size(272, 208)   
591Me.TreeView1.TabIndex = 1   
592'   
593'Button1   
594'   
595Me.Button1.Location = New System.Drawing.Point(40, 232)   
596Me.Button1.Name = "Button1"   
597Me.Button1.Size = New System.Drawing.Size(56, 24)   
598Me.Button1.TabIndex = 2   
599Me.Button1.Text = "Button1"   
600'   
601'TreeView2   
602'   
603Me.TreeView2.ImageIndex = -1   
604Me.TreeView2.Location = New System.Drawing.Point(56, 56)   
605Me.TreeView2.Name = "TreeView2"   
606Me.TreeView2.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("节点0"), New System.Windows.Forms.TreeNode("节点1"), New System.Windows.Forms.TreeNode("节点2", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("节点10", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("节点11")})}), New System.Windows.Forms.TreeNode("节点3", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("节点8", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("节点9")})}), New System.Windows.Forms.TreeNode("节点4"), New System.Windows.Forms.TreeNode("节点5"), New System.Windows.Forms.TreeNode("节点6", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("节点12", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("节点13")})}), New System.Windows.Forms.TreeNode("节点7")})   
607Me.TreeView2.SelectedImageIndex = -1   
608Me.TreeView2.Size = New System.Drawing.Size(96, 128)   
609Me.TreeView2.TabIndex = 3   
610'   
611'Form1   
612'   
613Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)   
614Me.ClientSize = New System.Drawing.Size(632, 273)   
615Me.Controls.Add(Me.TreeView2)   
616Me.Controls.Add(Me.Button1)   
617Me.Controls.Add(Me.TreeView1)   
618Me.Name = "Form1"   
619Me.Text = "Form1"   
620Me.ResumeLayout(False) 
621
622End Sub 
623
624#End Region 
625
626  
627End Class</system.diagnostics.debuggerstepthrough()></system.diagnostics.debuggerstepthrough()>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus