Increase the tab size in a user-drawn TabControl

By modifying the padding, it is possible to create additional space for multiple icons or other content.

https://docs.microsoft.com/en-us/windows/win32/controls/tab-controls#tab-size-and-position

https://docs.microsoft.com/en-us/windows/win32/controls/tcm-setpadding

https://docs.microsoft.com/en-us/windows/win32/api/_controls/

This is particularly useful in a TabControl with SizeMode = FillToRight because setting ControlStyles.UserPaint to true in the constructor will cause the system to report the size of tabs to match only the text (i.e., the space required for icons is unaccounted for).

My take:

private const int TCM_FIRST = 0x1300;
private const int TCM_SETPADDING = TCM_FIRST + 43;
private const int PADDING_X = 24;
private const int PADDING_Y = 2;
protected override void WndProc( ref Message m )
{
if( m.Msg == TCM_SETPADDING )
{
// The vertical padding is the high-order word and the horizontal the low-order word of the lParam
m.LParam = new IntPtr( ( PADDING_Y << 16 ) | ( PADDING_X & 0xFFFF ) );
}
base.WndProc( ref m );
}
view raw a.cs hosted with ❤ by GitHub

Comments