【问题标题】:How to position tabs on a tabcontrol vb.net如何在 tabcontrol vb.net 上放置选项卡
【发布时间】:2013-12-10 09:42:41
【问题描述】:

VB.Net,TabStrip 控件,

标签是左对齐或右对齐和上或下对齐

我需要从我自己的位置开始标签页控件的顶部

与 Internet Explorer 一样,Tabs 是在 HTTP 地址框之后开始的,但它会覆盖整个页面并且左侧对齐=0。

【问题讨论】:

  • 嗯,Internet Explorer 使用 TabControl,它的选项卡是完全自定义绘制的。您可以通过不使用标签页来模拟类似的东西,只需使 TabControl 高到足以显示标签即可。

标签: vb.net winforms tabcontrol


【解决方案1】:

显示右对齐标签

  1. 在表单中添加 TabControl

  2. Alignment 属性设置为 Right

  3. SizeMode属性设置为Fixed,这样所有标签的宽度都一样。

  4. ItemSize 属性设置为选项卡的首选固定大小。请记住,ItemSize 属性的行为就像选项卡在顶部一样,尽管它们是右对齐的。因此,为了使选项卡更宽,您必须更改 Height 属性,而为了使它们更高,您必须更改 Width 属性。

    在下面的代码示例中,Width 设置为 25,Height 设置为 150。

  5. DrawMode 属性设置为 OwnerDrawFixed

  6. TabControl 的 DrawItem 事件定义一个 handler,从左到右呈现文本。

    C#

    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", (float)10.0, 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));
    

【讨论】: