【问题标题】:tab control, vertically aligned tabs with vertically aligned text选项卡控件,具有垂直对齐文本的垂直对齐选项卡
【发布时间】:2010-06-29 21:21:04
【问题描述】:

我想使用选项卡控件并将选项卡显示在左侧,而不是顶部。我已将对齐设置为左侧,并且选项卡显示在那里。但是,如何让文本垂直显示在选项卡上?我查看了 msdn,它提供了一个左对齐选项卡控件的示例,但选项卡标签仍然水平显示!

另外,有人知道如何使用带有左对齐标签的标签控件和默认布局以使其看起来更好吗?

请不要使用第三方应用,除非它们是免费的,是的,我已经看过代码项目了。

谢谢,R.

【问题讨论】:

    标签: c# winforms visual-studio-2008


    【解决方案1】:

    这是原生 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】:

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

    1) 设置 TabControl 的以下属性:

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

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

    3) 为 DrawItem 事件添加事件处理函数,并添加如下代码:

    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

    【讨论】:

      猜你喜欢
      • 2013-12-13
      • 1970-01-01
      • 1970-01-01
      • 2019-08-23
      • 1970-01-01
      • 2021-08-31
      • 1970-01-01
      • 2020-09-25
      • 1970-01-01
      相关资源
      最近更新 更多