如何:使用 TabControl 显示 Side-Aligned 选项卡

AlignmentTabControl属性支持垂直显示选项卡(沿控件的左边缘或右边缘),而非水平显示(在控件的顶部或底部)。 默认情况下,此垂直显示会导致用户体验不佳,因为 Text 启用视觉样式时,该对象的属性 TabPage 不会显示在选项卡中。 也没有直接方法控制选项卡内文本的方向。您可以使用自定义绘制TabControl来改进这体验。

以下过程演示如何使用“所有者绘图”功能呈现右对齐的选项卡,以及显示从左到右的选项卡文本。

显示右对齐选项卡

  1. 向表单添加TabControl

  2. Alignment 属性设置为 Right

  3. SizeMode 属性设置为 Fixed,以便所有选项卡的宽度相同。

  4. ItemSize 属性设置为选项卡的首选固定大小。 请记住,属性 ItemSize 的行为就像选项卡位于顶部一样,虽然这些选项卡是右对齐的。 因此,若要使选项卡变宽,必须更改 Height 属性,并且要使其更高,必须更改 Width 属性。

    为了获得最佳结果,请使用下面的代码示例将选项卡设置为 Width 25 和 Height 100。

  5. DrawMode 属性设置为 OwnerDrawFixed

  6. DrawItem 从左到右呈现文本的事件 TabControl 定义处理程序。

    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
    

另请参阅