2011-07-19 24 views

回答

10

本地的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); 
} 
0

您需要测量字体。

尝试是这样的:

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 
+1

好吧,我明白了,接下来是什么? – clumpter

+0

.width =测量宽度 –

+0

请参阅我上面的编辑 –

3

谢谢,汉斯。 我用你的代码没有创建一个类

//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); 
相关问题