c#:如何删除tabcontrol边框?

时间:2014-04-23 14:51:07

标签: c# forms

在我的表单中,我正在使用tabcontrol。我想隐藏标签页眉和边框。我可以做任何一个,如果我试图隐藏标题然后边框变得可见。有人可以帮帮我吗?谢谢,这是我的代码:

public Form3()
{
    InitializeComponent();
    this.NativeTabControl1 = new NativeTabControl();

    this.NativeTabControl1.AssignHandle(this.tabControl1.Handle);

}

private NativeTabControl NativeTabControl1;

private class NativeTabControl : NativeWindow
{
    protected override void WndProc(ref Message m)
    {
        if ((m.Msg == TCM_ADJUSTRECT))
        {
            RECT rc = (RECT)m.GetLParam(typeof(RECT));
            //Adjust these values to suit, dependant upon Appearance
            rc.Left -= 3;
            rc.Right += 3;
            rc.Top -= 3;
            rc.Bottom += 3;
            Marshal.StructureToPtr(rc, m.LParam, true);
        }
        base.WndProc(ref m);
    }

    private const Int32 TCM_FIRST = 0x1300;
    private const Int32 TCM_ADJUSTRECT = (TCM_FIRST + 40);
    private struct RECT
    {
        public Int32 Left;
        public Int32 Top;
        public Int32 Right;
        public Int32 Bottom;
    }

    private void Form3_Load(object sender, EventArgs e)
    {
        //hides tabcontrol headers
        tabControl1.Appearance = TabAppearance.Buttons;
        tabControl1.ItemSize = new Size(0, 1);
        tabControl1.SizeMode = TabSizeMode.Fixed;
    }
}

2 个答案:

答案 0 :(得分:3)

another thread on Stackoverflow,它提供了一些隐藏Windows窗体中TabControl标签行的提示。
我最喜欢的是覆盖WndProc并将Multiline属性设置为true

public partial class TabControlWithoutHeader : TabControl
{
    public TabControlWithoutHeader()
    {
        if (!this.DesignMode) this.Multiline = true;
    }

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x1328 && !this.DesignMode)
            m.Result = new IntPtr(1);
        else
            base.WndProc(ref m);
    }
}

我测试了Windows 8.1上的代码,既没有看到标签也没有看到边框线。所以我认为你不需要使用像你这样的代码。

答案 1 :(得分:-1)

我通常会建议在xaml中执行此操作而不是C#(我是WPF开发人员)我相信您也可以通过命名TabControl以及每个Tab本身来在C#中执行此操作。

tabControlName.BorderBrush = null;
///^Gets rid of the TabControl's border.

tabName1.Height = 0;
tabName2.Height = 0;
tabNameETC.Height = 0;
///^Removes the tabs(headers) if you have the TabControl.TabStripPlacement set to left 
/// or right, then use the following instead:

tabName1.Width = 0
tabName2.Width = 0
tabNameETC.Width = 0