VB.Net2005中TabControl实现Disabl功能,例一

来源:互联网 发布:linux新建目录命令 编辑:程序博客网 时间:2024/04/27 14:44

Public Class TabControlEx : Inherits TabControl

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg
            Case &H201 ''WM_LBUTTONDOWN
                Dim tp As Object = GetTabPage(New Point(m.LParam.ToInt32))
                If Not tp Is Nothing AndAlso TypeOf (tp) Is TabPage Then
                    If tp.Enabled = False Then
                        Return
                    End If
                End If

        End Select

        MyBase.WndProc(m)
    End Sub

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        Dim obj As Object = Control.FromHandle(msg.HWnd)
        If Not obj Is Nothing AndAlso TypeOf obj Is TabControlEx AndAlso obj.Focus Then
            If keyData = (Keys.Tab Or Keys.Control) OrElse keyData = Keys.Right Then
                Dim tp As Object = GetNextEnablePage()
                If Not tp Is Nothing AndAlso TypeOf (tp) Is TabPage Then
                    SelectedTab = tp

                    Return True
                End If
            End If
            If keyData = (Keys.Tab Or Keys.Shift Or Keys.Control) OrElse keyData = Keys.Left Then
                Dim tp As Object = GetProvEnablePage()
                If Not tp Is Nothing AndAlso TypeOf (tp) Is TabPage Then
                    SelectedTab = tp

                    Return True
                End If
            End If
            If keyData = Keys.Up OrElse keyData = Keys.Down Then
                Dim tp As Object = GetLastEnablePage()
                If Not tp Is Nothing AndAlso TypeOf (tp) Is TabPage Then
                    SelectedTab = tp

                    Return True
                End If
            End If
        End If

        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function

    Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
        MyBase.OnDrawItem(e)

        Dim pg As TabPage = TabPages(e.Index)
        Dim sf As New StringFormat
        sf.Alignment = StringAlignment.Center
        sf.LineAlignment = StringAlignment.Center
        Dim fnt As New Font(pg.Font, FontStyle.Bold)

        Dim sb As New SolidBrush(Color.Gray)
        If SelectedIndex = e.Index Then
            e.Graphics.FillRectangle(SystemBrushes.Control, e.Bounds)
            'ControlPaint.DrawFocusRectangle(e.Graphics, pg.Bounds)
        End If

        If pg.Enabled = True Then
            If SelectedIndex = e.Index Then
                e.Graphics.DrawString(pg.Text, fnt, SystemBrushes.ControlText, RectangleF.op_Implicit(e.Bounds), sf)
            Else
                e.Graphics.DrawString(pg.Text, pg.Font, SystemBrushes.ControlText, RectangleF.op_Implicit(e.Bounds), sf)
            End If
        Else
            e.Graphics.DrawString(pg.Text, pg.Font, sb, RectangleF.op_Implicit(e.Bounds), sf)
        End If

        sf.Dispose()
        sb.Dispose()
        fnt.Dispose()
    End Sub

    Private Function GetTabPage(ByVal pt As Point) As Object
        Dim nIndex As Integer = 0
        For nIndex = 0 To TabCount - 1
            If GetTabRect(nIndex).Contains(pt.X, pt.Y) Then
                Return TabPages(nIndex)
            End If
        Next
        Return Nothing
    End Function

   
End Class 

原创粉丝点击