在www.codeproject.com 看到一个关于重绘tabControl的例了,觉得挺有意思的。照着修改一下,有一些东西自己并没有去改,使得代码很短,同时也有一些功能并没实现的。具休可到http://www.codeproject.com/KB/tabs/flattabcontrol.aspx。
我实现的效果如图:
代码实现
主要是对TabControl实现的重绘,可以通过继承TabControl来重绘。
基本上没什么难度,有兴趣可以试着写一下。
vb.net IC交易网代码如下:
view plaincopy to clipboardprint?
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() pdf调用之后添加任何初始化。
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