C# TabControl左右形式

转自:http://en.csharp-online.net/TabControl#Render_Right-Aligned_or_Left-Aligned_Tabs_horizontally

Render Right-Aligned or Left-Aligned Tabs horizontally

When you set the Alignment property to either Left or Right, the tabs appear vertically. By drawing the tabs yourself, you can have horizontal text tabs. To achieve this follow these simple steps.

  1. Set the following properties of the TabControl:

    PropertyValue
    AlignmentRight
    SizeModeFixed
    DrawModeOwnerDrawFixed

    Set the ItemSize.Width property to 25 and the ItemSize.Height property to 100. Your TabControl should now look something like this.

    Image:526-50.jpg

  2. Add an event handler for the DrawItem event and add the following code.
    private void tabControl1_DrawItem(object sender, 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. Because we CAN.
      Font _TabFont = new Font("Arial", 10, 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));
    }

When the form runs, horizontal text is in the tabs.

Image:526-51.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值