2010-02-17 169 views

回答

0

也許不是最優雅的解決方案,但..將TabControl的DrawMode更改爲OwnerDrawFixed並自己處理繪圖。以MSDN爲例。

+0

太不雅觀加上需要處理所有其他標籤及其文本。我想要做的只是改變一個標籤頁眉的顏色。 – Alex 2010-02-17 01:55:02

0

除了對DrawItem事件進行編碼之外,還可以通過此事件隨時更改Tabpage文本。

Private Sub ChangeTabPageTextColor(ByVal TabPageIndex As Integer, ByVal ForeColor As Color) 
    ' Get the area of the header of this TabPage 
    Dim HeaderRect As Rectangle = TabControl1.GetTabRect(TabPageIndex) 
    ' Identify which TabPage is currently selected 
    Dim SelectedTab As TabPage = TabControl1.TabPages(TabPageIndex) 
    ' Create a Brush to paint the Text 
    Dim TextBrush As New SolidBrush(ForeColor) 
    ' Declare the text alignment variable 
    Dim sf As New StringFormat() 
    ' Set the Horizontal Alignment of the Text 
    sf.Alignment = StringAlignment.Center 
    ' Set the Verticle Alignment of the Text 
    sf.LineAlignment = StringAlignment.Near 
    ' Declare a Font 
    Dim newfont As New Font(TabControl1.Font.Name, TabControl1.Font.Size, FontStyle.Regular) 
    ' Draw the text 
    TabControl1.CreateGraphics().DrawString(SelectedTab.Text, newfont, TextBrush, HeaderRect, sf) 
    ' Job done - dispose of the Brush 
    TextBrush.Dispose() 
End Sub