Winforms TabControl alignment problems

2020-06-21 04:54发布

问题:

When I set TabControl alignment to Left or Right it leaves this huge space between tab buttons and tab page area. How to get rid of this useless space?

TabControl.Appearance is set to Buttons because if it is set to Normal the text on buttons disappear.

UPDATE:
When i set TabControl.Alignment to Bottom and TabControl.Appearance to Normal buttons look inverted (orange line should be below)

When i set TabControl.Alignment to Bottom and TabControl.Appearance to Buttons, There is no area on TabPage to place controls

回答1:

This is a well-known problem with the XP visual style implementation for the native tab control, only tabs aligned to the top render properly. This bug hasn't been addressed until Windows 7. The workaround is to selectively turn off the style. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form and change the Alignment property to your liking. It isn't going to look prettier than that.

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class FixedTabControl : TabControl {

    protected override void OnHandleCreated(EventArgs e) {
        SetWindowTheme(this.Handle, "", "");
        base.OnHandleCreated(e);
    }

    [DllImportAttribute("uxtheme.dll")]
    private static extern int SetWindowTheme(IntPtr hWnd, string appname, string idlist);
}


回答2:

Microsoft documentation Remarks related to this issue

When the Alignment property is set to Left or Right, the Multiline property is automatically set to true.

When you set the Appearance property to FlatButtons, it only appears as such when the Alignment property is set to Top. Otherwise, the Appearance property displays as if set to the Buttons value.

When you set the Appearance property to Buttons, you must also set the Alignment property to Top so that the buttons display correctly.

Note

When you set the Appearance property to Buttons, you must also set the Alignment property to Top so that the tab page contents display correctly. Additionally, when visual styles are enabled, and the Alignment property is set to a value other than Top, the tab contents may not render correctly.