如何:使用 TabControl 顯示對齊側邊的定位點

TabControlAlignment 屬性支援以垂直方式 (沿著控制項的左或右邊緣) 顯示索引標籤,而非水平方式 (橫跨控制項的頂端或底部) 。 根據預設,此垂直顯示會導致不良的使用者經驗,因為當啟用視覺化樣式時,TabPage 物件的 Text 屬性並不會在索引標籤中顯示。 也沒有直接方法可控制索引標籤內文字的方向。您可以使用擁有者繪製 TabControl 來改善此體驗。

下列程序示範如何使用主控描繪功能來呈現靠右對齊的索引標籤,使索引標籤文字從左向右跑。

若要顯示靠右對齊的索引標籤

  1. 加入 TabControl 至您的表單。

  2. Alignment 屬性設為 Right

  3. 設定 SizeMode 屬性為 Fixed,好讓所有的索引標籤有相同寬度。

  4. 設定 ItemSize 屬性為索引標籤的慣用固定大小。 請記住,ItemSize 屬性的行為如同索引標籤已在頂端,儘管它們是靠右對齊的。 因此,為了讓索引標籤更寬,您必須變更 Height 屬性,而且為了讓它們更高,您必須變更 Width 屬性。

    為了使下列程式碼範例獲得最佳結果,請設定索引標籤的 Width 為 25 ,並設定 Height 為 100。

  5. DrawMode 屬性設為 OwnerDrawFixed

  6. 定義 TabControlDrawItem 事件處理常式,這會從左到右呈現文字。

    public Form1()
    {
        // Remove this call if you do not program using Visual Studio.
        InitializeComponent();
    
        tabControl1.DrawItem += new DrawItemEventHandler(tabControl1_DrawItem);
    }
    
    private void tabControl1_DrawItem(Object sender, System.Windows.Forms.DrawItemEventArgs e)
    {
        Graphics g = e.Graphics;
        Brush _textBrush;
    
        // Get the item from the collection.
        TabPage _tabPage = tabControl1.TabPages[e.Index];
    
        // Get the real bounds for the tab rectangle.
        Rectangle _tabBounds = tabControl1.GetTabRect(e.Index);
    
        if (e.State == DrawItemState.Selected)
        {
    
            // Draw a different background color, and don't paint a focus rectangle.
            _textBrush = new SolidBrush(Color.Red);
            g.FillRectangle(Brushes.Gray, e.Bounds);
        }
        else
        {
            _textBrush = new System.Drawing.SolidBrush(e.ForeColor);
            e.DrawBackground();
        }
    
        // Use our own font.
        Font _tabFont = new Font("Arial", 10.0f, FontStyle.Bold, GraphicsUnit.Pixel);
    
        // Draw string. Center the text.
        StringFormat _stringFlags = new StringFormat();
        _stringFlags.Alignment = StringAlignment.Center;
        _stringFlags.LineAlignment = StringAlignment.Center;
        g.DrawString(_tabPage.Text, _tabFont, _textBrush, _tabBounds, new StringFormat(_stringFlags));
    }
    
    Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem
        Dim g As Graphics = e.Graphics
        Dim _TextBrush As Brush
    
        ' Get the item from the collection.
        Dim _TabPage As TabPage = TabControl1.TabPages(e.Index)
    
        ' Get the real bounds for the tab rectangle.
        Dim _TabBounds As Rectangle = TabControl1.GetTabRect(e.Index)
    
        If (e.State = DrawItemState.Selected) Then
            ' Draw a different background color, and don't paint a focus rectangle.
            _TextBrush = New SolidBrush(Color.Red)
            g.FillRectangle(Brushes.Gray, e.Bounds)
        Else
            _TextBrush = New System.Drawing.SolidBrush(e.ForeColor)
            e.DrawBackground()
        End If
    
        ' Use our own font.
        Dim _TabFont As New Font("Arial", 10.0, FontStyle.Bold, GraphicsUnit.Pixel)
    
        ' Draw string. Center the text.
        Dim _StringFlags As New StringFormat()
        _StringFlags.Alignment = StringAlignment.Center
        _StringFlags.LineAlignment = StringAlignment.Center
        g.DrawString(_TabPage.Text, _TabFont, _TextBrush, _TabBounds, New StringFormat(_StringFlags))
    End Sub
    

另請參閱