自定义控件显示问题

时间:2017-02-09 18:58:44

标签: vb.net class

我已经在Visual Basic中创建了这个类,它工作正常,但有一个轻微的design issue。正如你所看到的那样,这个小小的bumb,我该如何解决这个问题呢?此外,如何修复内容框和选择菜单之间的间距。

Public Class VerticallTabControll
    Inherits TabControl

    Sub New()
        SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint, True)
        DoubleBuffered = True
        SizeMode = TabSizeMode.Fixed
        ItemSize = New Size(30, 170)
    End Sub

    Protected Overrides Sub CreateHandle()
        MyBase.CreateHandle()
        Alignment = TabAlignment.Left
    End Sub

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        Dim selected As New SolidBrush(Color.FromArgb(0, 122, 204))
        Dim notSelected As New SolidBrush(Color.FromArgb(63, 63, 70))



        Dim B As New Bitmap(Width, Height)
        Dim G As Graphics = Graphics.FromImage(B)




        G.Clear(Color.FromArgb(63, 63, 70))

        For i = 0 To TabCount - 1
            Dim TabRectangle As Rectangle = GetTabRect(i)



            If i = SelectedIndex Then
                '// if tab is selected
                G.FillRectangle(selected, TabRectangle)
            Else
                '// tab is not selected
                G.FillRectangle(notSelected, TabRectangle)
            End If

            'Line Test
            'Dim start As New Point(10, 31 * (i + 1))
            'Dim ende As New Point(160, 31 * (i + 1))
            'G.DrawLine(Pens.White, start, ende)


            G.DrawString(TabPages(i).Text, Font, Brushes.White, TabRectangle, New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})

        Next

        e.Graphics.DrawImage(B.Clone, 0, 0)
        G.Dispose() : B.Dispose() : selected.Dispose() : notSelected.Dispose()

        MyBase.OnPaint(e)
        e.Dispose()
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

您可以尝试重写DisplayRectangle属性以相应地调整内部空间:

Public Overrides ReadOnly Property DisplayRectangle As Rectangle
  Get
    Return New Rectangle(MyBase.DisplayRectangle.Left,
                         MyBase.DisplayRectangle.Top - 2,
                         MyBase.DisplayRectangle.Width + 2,
                         MyBase.DisplayRectangle.Height + 4)
  End Get
End Property