代码之家  ›  专栏  ›  技术社区  ›  flavour404

选项卡控件,具有垂直对齐文本的垂直对齐选项卡

  •  2
  • flavour404  · 技术社区  · 16 年前

    另一件事,有人知道如何使用标签控件与左对齐的默认布局标签,使它看起来更好吗?

    谢谢,R。

    2 回复  |  直到 16 年前
        1
  •  4
  •   Hans Passant    16 年前

    这是本机Windows选项卡控件的视觉样式呈现器中的一个古老错误。它只支持顶部的标签,我猜是微软的程序员在完成任务之前被一辆公共汽车碾过了。

    using System;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    public class FixedTabControl : TabControl {
        [DllImportAttribute("uxtheme.dll")]
        private static extern int SetWindowTheme(IntPtr hWnd, string appname, string idlist);
    
        protected override void OnHandleCreated(EventArgs e) {
            SetWindowTheme(this.Handle, "", "");
            base.OnHandleCreated(e);
        }
    }
    
        2
  •  2
  •   Ed Schwehm    15 年前

    如果创建自己的DrawItem事件,则可以手动编写选项卡标题。您可以使用以下过程:

     Property  |  Value
     ----------|----------------
     Alignment |  Right (or left, depending on what you want)
     SizeMode  |  Fixed
     DrawMode  |  OwnerDrawFixed
    

    2) 将ItemSize.Width属性设置为25,将ItemSize.Height属性设置为100。根据需要调整这些值,但请记住,基本上,宽度就是高度,反之亦然。

    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));
    }
    

    4) 利润!

    http://en.csharp-online.net/TabControl )

    推荐文章