【问题标题】:How can i make WinForms TabPage header width fit it's title?我怎样才能使 WinForms TabPage 标题宽度适合它的标题?
【发布时间】:2011-10-08 09:08:12
【问题描述】:

如何使 WinForms TabPage 标题宽度适合其标题? 这是问题所在。

【问题讨论】:

    标签: c# winforms width tabcontrol tabpage


    【解决方案1】:

    谢谢,汉斯。 我没有创建类就使用了你的代码

    //InitializeComponent
    this.tabPresentations.HandleCreated += new System.EventHandler(TabControl_HandleCreated);
    
    void TabControl_HandleCreated(object sender, System.EventArgs e)
    {
         // Send TCM_SETMINTABWIDTH
         SendMessage((sender as TabControl).Handle, 0x1300 + 49, IntPtr.Zero, (IntPtr)4);
    }
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
    

    【讨论】:

      【解决方案2】:

      您需要测量字体。

      试试这样的:

      Dim tabPage As New TabPage
      Dim width As Integer = 0
      Dim valueToMeasure As String = <Your title Here>
      Dim g As Graphics = tabPage.CreateGraphics()
      
      width = CType(g.MeasureString(valueToMeasure, tabPage.Font).Width, Integer)
      

      可能会为填充添加一个额外的机器人(宽度 = 宽度 +10)

      已编辑:

      <tab>.width = GetTabWidth(<Title>)
      
      Private Function GetTabWidth (Byval title as String) as Integer
      
           Dim widthValue as Integer = 10    'Padding (Optional)
      
           Dim tabPage as New tabPage
           Dim g as Graphics = tabPage.CreateGraphics()
      
           widthValue += Ctype(g.measureString(title, tabPage.Font).Width, Integer)
      
           Return widthValue
      
      End Function
      

      【讨论】:

      • 好的,我测过了,接下来呢?
      • TabPage 没有宽度属性。
      • 对不起,我习惯了氪星导航器!试试... .Size.Width = GetTabWidth()
      • .Size 用于选项卡区域,而不用于选项卡标题。
      • 你真的相信所有标签页的标题都和“+”标签一样小宽度是个好主意吗?
      【解决方案3】:

      本机 Windows 选项卡控件允许覆盖默认的最小选项卡宽度。遗憾的是,该功能并未在 TabControl 包装类中公开。不过这是可以解决的。向您的项目添加一个新类并粘贴如下所示的代码。编译。将新控件从工具箱顶部拖放到表单上。

      using System;
      using System.Windows.Forms;
      
      class MyTabControl : TabControl {
          protected override void OnHandleCreated(EventArgs e) {
              base.OnHandleCreated(e);
              // Send TCM_SETMINTABWIDTH
              SendMessage(this.Handle, 0x1300 + 49, IntPtr.Zero, (IntPtr)10);
          }
          [System.Runtime.InteropServices.DllImport("user32.dll")]
          private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
      }
      

      【讨论】:

        猜你喜欢
        • 2022-01-12
        • 2014-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-31
        • 2014-06-26
        相关资源
        最近更新 更多