Search code examples
.netwinformstabcontrolownerdrawn

Owner drawn tabcontrol has wider tabs


I am trying to custom draw a tabcontrol. When I draw the tabs using the dimensions returned by GetTabRect, the tabs are drawn noticeably wider when compared to how they are normally drawn. I thought it was trying to make room for an image, but I have no images defined for the tabs. Why would GetTabRect return a wider size?

Don't know if this has anything to do with it, but here is how I set it to owner draw in the custom TabControl constructor.

this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | 
              ControlStyles.DoubleBuffer | ControlStyles.ResizeRedraw | 
              ControlStyles.SupportsTransparentBackColor, true); 

Solution

  • I found a solution here: http://www.codeproject.com/Messages/2707590/Re-Tab-Size.aspx

    Quote:

    When ControlStyle.UserPaint is set to true, the control no longer sends WM_SETFONT messages.
    The code needed to send the FontChange messages:

    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
    
    private const int WM_SETFONT = 0x30;
    private const int WM_FONTCHANGE = 0x1d;
    
    protected override void OnCreateControl()
    {
       base.OnCreateControl();
       this.OnFontChanged(EventArgs.Empty);
    }
    
    protected override void OnFontChanged(EventArgs e)
    {
       base.OnFontChanged(e);
       IntPtr hFont = this.Font.ToHfont();
       SendMessage(this.Handle, WM_SETFONT, hFont, (IntPtr)(-1));
       SendMessage(this.Handle, WM_FONTCHANGE, IntPtr.Zero, IntPtr.Zero);
       this.UpdateStyles();
    }