|
2015/07/14(Tue) 11:00:37 編集(投稿者)
■No76482 (こういち さん) に返信 > オーナードローですと、元の表示とだいぶ変わってしまい一から記載するのは大変そうですね。
描画内容を近づけてみました。 事前に、ItemSize プロパティも設定しておいた方が良いかもしれません。
Imports System.Drawing Imports System.Windows.Forms Imports System.Windows.Forms.VisualStyles
Public Class VisualTabControl Inherits TabControl
Public Sub New() MyBase.New() Me.SetStyle(ControlStyles.UserPaint, True) Me.DoubleBuffered = True Me.ResizeRedraw = True 'ControlStyles.UserPaintをTrueすると、 'SizeModeは強制的にTabSizeMode.Fixedにされる Me.SizeMode = TabSizeMode.Fixed End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) MyBase.OnPaint(e) e.Graphics.FillRectangle(SystemBrushes.Control, Me.ClientRectangle) If Me.TabPages.Count = 0 Then Return End If
Dim page As TabPage = Me.TabPages(Me.SelectedIndex) Dim pageRect As New Rectangle(page.Bounds.X - 2, _ page.Bounds.Y - 2, _ page.Bounds.Width + 5, _ page.Bounds.Height + 5) TabRenderer.DrawTabPage(e.Graphics, pageRect)
For i As Integer = 0 To Me.TabPages.Count - 1 DrawTab(e.Graphics, i) Next End Sub
Private Sub DrawTab(g As Graphics, index As Integer) Dim page As TabPage = Me.TabPages(index) Dim tabRect As Rectangle = Me.GetTabRect(index)
Dim state As TabItemState If Not Me.Enabled Then state = TabItemState.Disabled ElseIf tabRect.Contains(Me.PointToClient(Control.MousePosition)) Then state = TabItemState.Hot ElseIf Me.SelectedIndex = index Then state = TabItemState.Selected Else state = TabItemState.Normal End If
If Me.SelectedIndex = index Then tabRect.Y -= 2 tabRect.Height += 2 End If
'タブの下にできる空白部分を消すため、高さを増やす tabRect.Height += 1
'テキストなしで描画 TabRenderer.DrawTabItem( g, _ tabRect, _ "", _ page.Font, _ False, _ state)
'テキストを左寄せで追加描画 Dim tff As TextFormatFlags = TextFormatFlags.Left Or TextFormatFlags.LeftAndRightPadding _ Or TextFormatFlags.VerticalCenter Or TextFormatFlags.NoClipping Or TextFormatFlags.EndEllipsis TextRenderer.DrawText(g, page.Text, page.Font, tabRect, page.ForeColor, tff) End Sub
Private trackingTab As Integer = -1
Protected Overrides Sub OnMouseLeave(e As EventArgs) MyBase.OnMouseLeave(e) trackingTab = -1 Invalidate() End Sub
Protected Overrides Sub OnMouseMove(e As MouseEventArgs) MyBase.OnMouseMove(e) For i As Integer = 0 To Me.TabPages.Count - 1 If Me.GetTabRect(i).Contains(e.Location) Then If trackingTab <> i Then trackingTab = i Me.Invalidate() Exit For End If End If Next End Sub End Class
|