重画系列:重画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 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container
        m_TVColumnStyles = New ArrayList
    End Sub

#End Region
#Region "类变量定义区"
    '保存Style对象
    Private m_TVColumnStyles As ArrayList
    '触发的点击事件
    Public Event TreeViewClick(ByVal ClickNode As TreeNode, ByVal CurrentCol As Integer)
    '对node.text的分隔显示符号
    Private m_Seperator As String = ","
    '列缩进距离
    Private m_Indent As Integer = Me.Indent
    '加号的大小
    Private pWid As Integer = 6
    'mousedown时鼠标所在的节点
    Private SelNode As TreeNode
    'mousedown时鼠标所在的列的位置
    Private CurrentStyle As Integer
    'mousedown时鼠标所在的单元格的bounds
    Private SelNodeBound As Rectangle
    '自己的graphics对象
    Private m_graphics As Graphics = Me.CreateGraphics
    '连线的颜色
    Private m_LineColor As Color = Color.DarkGreen
    '加减号按钮外框的颜色
    Private m_PlusMinusBorderColor As Color = Color.DarkGreen
    '加减号按钮的颜色
    Private m_PlusMinusColor As Color = Color.DarkOrange
#End Region

#Region "类对外属性区"
    '设置列分隔符
    Public Property Seperator() As String
        Get
            Return Me.m_Seperator
        End Get
        Set(ByVal Value As String)
            Me.m_Seperator = Value
        End Set
    End Property
    '连线的颜色
    Public Property LineColor() As Color
        Get
            Return Me.m_LineColor
        End Get
        Set(ByVal Value As Color)
            Me.m_LineColor = Value
        End Set
    End Property
    '加减号按钮外框的颜色
    Public Property PlusMinusBorderColor() As Color
        Get
            Return Me.m_PlusMinusBorderColor
        End Get
        Set(ByVal Value As Color)
            Me.m_PlusMinusBorderColor = Value
        End Set
    End Property
    '加减号按钮的颜色
    Public Property PlusMinusColor() As Color
        Get
            Return Me.m_PlusMinusColor
        End Get
        Set(ByVal Value As Color)
            Me.m_PlusMinusColor = Value
        End Set
    End Property
#End Region

#Region "类对外方法区"
    '添加列style
    Public Sub AddTreeViewColumnStyles(ByVal ColStyle As TreeViewColStyle)
        ColStyle.ParentControl = Me
        m_TVColumnStyles.Add(ColStyle)
    End Sub
#End Region

#Region "类私有方法区"
    '重画的主要函数,实现对每个节点的bounds的分配以及value的分隔
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        If IsNothing(Me.Nodes) Then Return
        If Me.Nodes.Count < 1 Then Return
        Dim node As TreeNode = Me.Nodes(0)
        If Not IsNothing(node) Then
            PaintNodes(e.Graphics, node)
        End If
    End Sub
    '重画的从属函数,第归实现对每个节点的bounds的分配以及value的分隔
    Private Sub PaintNodes(ByRef g As Graphics, ByRef node As TreeNode)
        '画当前节点
        If IsNothing(node) Then Return
        DrawNode(g, node)
        If node.IsExpanded Then
            '画其第一个子节点
            PaintNodes(g, node.FirstNode)
        Else
            '画加号
        End If
        '画下一个节点
        node = node.NextNode
        If IsNothing(node) Then
            Return
        Else
            PaintNodes(g, node)
        End If
    End Sub

    '根据所在节点,取得style的bounds
    Private Function GetStyleBounds(ByRef g As Graphics, ByRef node As TreeNode, ByVal StylePosition As Integer) As Rectangle
        Dim str As String = node.Text
        Dim arrstr As String() = str.Split(Me.m_Seperator)
        str = String.Join(Me.m_Seperator, arrstr, 0, StylePosition)
        Dim preSize As SizeF
        If str = "" Then
            preSize = New SizeF(0, 0)
        Else
            preSize = g.MeasureString(str, Me.Font)
        End If
        Dim nxtSize As SizeF

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值