Jak mogę ustawić szerokość nagłówka WinForms TabPage na jego tytuł? Oto problem.Jak mogę ustawić szerokość nagłówka WinForms TabPage na jego tytuł?
6
A
Odpowiedz
10
Natywna kontrola zakładka Windows umożliwia przesłonięcie domyślnej minimalnej szerokości karty. Niestety ta możliwość nie jest widoczna w klasie pakowania TabControl. Można to naprawić. Dodaj nową klasę do swojego projektu i wklej poniższy kod. Skompilować. Rzuć nową kontrolę z góry przybornika na formularz.
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
Musisz zmierzyć czcionek.
Spróbuj czegoś takiego:
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)
Prawdopodobnie dodać bota na Extra dla obicia (szerokość = szerokość +10)
Zmieniano:
<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
3
Dzięki, Hans. Użyłem twojego kodu bez tworzenia klasy
//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);
Dobra, mam to, co dalej? – clumpter
Proszę zobaczyć moją edycję powyżej –