在www.codeproject.com 看到一個關於重繪tabControl的例了,覺得挺有意思的。照着修改一下,有一些東西自己並沒有去改,使得代碼很短,同時也有一些功能並沒實現的。具休可到http://www.codeproject.com/KB/tabs/flattabcontrol.aspx。
我實現的效果如圖:
代碼實現
主要是對TabControl實現的重繪,可以通過繼承TabControl來重繪。
基本上沒什麼難度,有興趣可以試着寫一下。
vb.net 代碼如下:
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Collections
Imports System.ComponentModel
Imports System.Collections.Generic
Public Class FlatControl
Sub New()
' 此調用是 Windows 窗體設計器所必需的。
InitializeComponent()
' 在 InitializeComponent() 調用之後添加任何初始化。
SetStyle(ControlStyles.AllPaintingInWmPaint, True)
SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
SetStyle(ControlStyles.StandardDoubleClick, True)
SetStyle(ControlStyles.ResizeRedraw, True)
SetStyle(ControlStyles.UserPaint, True)
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
DrawTagControl(e.Graphics)
End Sub
Dim _backColor As Color = SystemColors.Control
Public Property backColors() As Color
Get
Return _backColor
End Get
Set(ByVal value As Color)
_backColor = value
End Set
End Property
Dim _borderWidth As Integer = SystemInformation.Border3DSize.Width
Public Property borderWidth() As Integer
Get
Return _borderWidth
End Get
Set(ByVal value As Integer)
_borderWidth = value
End Set
End Property
Dim _borderColor As Color = SystemColors.ControlDark
Public Property borderColor() As Color
Get
Return _borderColor
End Get
Set(ByVal value As Color)
_borderColor = value
End Set
End Property
Sub DrawTagControl(ByVal g As Graphics)
If Me.Visible = False Then
Exit Sub
End If
Dim BackTabArea As Rectangle = Me.ClientRectangle
Dim UserArea As Rectangle = Me.DisplayRectangle
'draw AllArea
Dim BackBrush As New SolidBrush(backColors)
g.FillRectangle(BackBrush, BackTabArea)
BackBrush.Dispose()
'draw border
Dim BrPen As New Pen(borderColor, borderWidth)
UserArea.Inflate(borderWidth, borderWidth)
g.DrawRectangle(BrPen, UserArea)
BrPen.Dispose()
If Me.TabCount > 0 Then
For i As Integer = 0 To Me.TabCount - 1
DrawTabs(g, TabPages(i), i)
Next
End If
If Me.SelectedTab IsNot Nothing Then
Dim tabPage As TabPage = Me.SelectedTab
Dim color As Color = tabPage.BackColor
Dim bpen As New Pen(color)
UserArea.Offset(1, 1)
UserArea.Width -= 2
UserArea.Height -= 2
g.DrawRectangle(bpen, UserArea)
UserArea.Width -= 1
UserArea.Height -= 1
g.DrawRectangle(bpen, UserArea)
bpen.Dispose()
End If
End Sub
Sub DrawTabs(ByVal g As Graphics, ByVal tabpage As TabPage, ByVal Tindex As Integer)
Dim TabArea As Rectangle = Me.GetTabRect(Tindex)
Dim TabTextArea As RectangleF = Me.GetTabRect(Tindex)
Dim point0 As Point
Dim point1 As Point
Dim point2 As Point
Dim point3 As Point
Dim point4 As Point
Dim point5 As Point
Dim point6 As Point
'只寫top與下兩個方向的
If Me.Alignment = TabAlignment.Top Then
point0 = New Point(TabArea.Left + borderWidth, TabArea.Bottom + 2)
point1 = New Point(TabArea.Left + borderWidth, TabArea.Top + 3)
point2 = New Point(TabArea.Left + borderWidth + 3, TabArea.Top)
point3 = New Point(TabArea.Right - 3, TabArea.Top)
point4 = New Point(TabArea.Right, TabArea.Top + 3)
point5 = New Point(TabArea.Right, TabArea.Bottom + 2)
point6 = point0
ElseIf Me.Alignment = TabAlignment.Bottom Then
point0 = New Point(TabArea.Left, TabArea.Top + 2)
point1 = New Point(TabArea.Left, TabArea.Bottom - 3)
point2 = New Point(TabArea.Left + 3, TabArea.Bottom)
point3 = New Point(TabArea.Right - 3, TabArea.Bottom)
point4 = New Point(TabArea.Right, TabArea.Bottom - 3)
point5 = New Point(TabArea.Right, TabArea.Top + 2)
point6 = point0
End If
Dim pt() As Point = New Point() {point0, point1, point2, point3, point4, point5, point6}
'添充
Dim bBrush As New SolidBrush(tabpage.BackColor)
g.FillPolygon(bBrush, pt)
bBrush.Dispose()
Dim pt1() As Point = New Point() {point0, point1, point2, point3, point4, point5}
'畫邊
Dim bpen As New Pen(borderColor, borderWidth)
g.DrawPolygon(bpen, pt1)
bpen.Dispose()
'draw Image
If tabpage.ImageIndex >= 0 Then
Dim LeftSpace As Integer = 8
Dim Topspace As Single
Dim img As Image = ImageList.Images(tabpage.ImageIndex)
Topspace = (TabTextArea.Height - img.Height) / 2 + 1
g.DrawImage(img, New PointF(LeftSpace + TabTextArea.X, Topspace))
TabTextArea.Width = TabTextArea.Width - LeftSpace + img.Width
End If
'drawText
Dim stringFormat As New StringFormat
stringFormat.Alignment = StringAlignment.Center
stringFormat.LineAlignment = StringAlignment.Center
g.DrawString(TabPages(Tindex).Text, Me.Font, Brushes.Black, TabTextArea, stringFormat)
End Sub
Private Sub FlatControl_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.SelectedIndexChanged
Me.Invalidate()
End Sub
End Class