可显示/隐藏左边控件的Splitter

类别:.NET开发 点击:0 评论:0 推荐:

有些软件中的splitter中间有一个按钮,点击后可显示/隐藏左边的控件,如WPS就是这样。在.NET中也自定义了一个。

类代码:
Public Class mySplitter

    Public Shared Sub AddShowHideEvent(ByVal sl As Splitter)
        Dim lbl As New Label
        lbl.Text = "3"
        lbl.Font = New System.Drawing.Font("Marlett", 7.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(2, Byte))
        lbl.AutoSize = True
        lbl.Location = New Point(-3.5, sl.Height / 2 - lbl.Height)
        lbl.Cursor = Cursors.Hand
        lbl.ForeColor = Color.Red
        sl.Controls.Add(lbl)
        sl.Width = 3.5

        AddHandler lbl.Click, AddressOf lbl_Click
        AddHandler sl.Resize, AddressOf splitter_Resize

        Dim tt As New ToolTip
        tt.SetToolTip(lbl, "隐藏左边的的控件")
        lbl.Tag = tt
    End Sub


    Private Shared Sub lbl_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lbl As Label = CType(sender, Label)
        Dim targetctrl As Control
        Dim sl As Splitter = CType(lbl.Parent, Splitter)
        Dim pctrl As Control = sl.Parent

        For Each temp As Control In pctrl.Controls
            If Not temp.Equals(sl) Then
                If sl.Dock = DockStyle.Left Then
                    If temp.Dock = DockStyle.Left AndAlso _
                            (Not temp.Visible OrElse temp.Left < sl.Left) Then
                        targetctrl = temp
                        Exit For
                    End If
                ElseIf sl.Dock = DockStyle.Top Then
                    If temp.Dock = DockStyle.Top AndAlso _
                            (Not temp.Visible OrElse temp.Top < sl.Top) Then
                        targetctrl = temp
                        Exit For
                    End If
                End If
            End If
        Next

        If Not targetctrl Is Nothing Then
            Dim tt As ToolTip = CType(lbl.Tag, ToolTip)
            If lbl.Text = "3" Then
                lbl.Text = 4
                tt.SetToolTip(lbl, "显示左边的的控件")
                targetctrl.Visible = False
            Else
                lbl.Text = 3
                tt.SetToolTip(lbl, "隐藏左边的的控件")
                targetctrl.Visible = True
            End If
        End If
    End Sub

    Private Shared Sub splitter_Resize(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim sl As Splitter = CType(sender, Splitter)
        Dim lbl As Label = sl.Controls(0)
        lbl.Location = New Point(-3.5, sl.Height / 2 - lbl.Height)
    End Sub
End Class

使用时,在窗体的初始化代码中加入如下所示的红色代码即可:
    Public Sub New()
        MyBase.New()

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

        '在 InitializeComponent() 调用之后添加任何初始化
      mySplitter.AddShowHideEvent(Me.Splitter1)
    End Sub


本文地址:http://com.8s8s.com/it/it41712.htm