簡體   English   中英

我怎樣才能使 WinForms TabPage header 寬度適合它的標題?

[英]How can i make WinForms TabPage header width fit it's title?

我怎樣才能使 WinForms TabPage header 寬度適合它的標題? 這是問題所在。

在此處輸入圖像描述

本機 Windows 選項卡控件允許覆蓋默認的最小選項卡寬度。 遺憾的是,TabControl 包裝器 class 中沒有公開該功能。 不過這是可以解決的。 將新的 class 添加到您的項目中並粘貼如下所示的代碼。 編譯。 將新控件從工具箱頂部拖放到表單上。

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

謝謝,漢斯。 我使用了您的代碼而沒有創建 class

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

您需要測量 fonts。

嘗試這樣的事情:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM