打赏

如Delphi之TPageControl控件,其TTabSheet有TabVisible属性,在制作类似Wizard页面切换时,甚为有用。

而c#对应之TabControl控件,其页面TabPage无此属性,实现Tab标题栏隐藏,方法诸多,却不够易用,不甚方便。

stackoverflow上找到另一方法,其移自微软社区,即扩展TabControl控件接管绘制消息,相比来说 ,易用多点。

 

贴代码如下:

using System;
using System.Windows.Forms;

public class TablessControl : TabControl
{
    protected override void WndProc(ref Message m)
    {
        // Hide tabs by trapping the TCM_ADJUSTRECT message
        if (m.Msg == 0x1328 && !DesignMode)
            m.Result = (IntPtr)1;
        else
            base.WndProc(ref m);
    }

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == (Keys.Control | Keys.Tab) || keyData == (Keys.Control | Keys.Shift | Keys.Tab) || keyData == (Keys.Left) || keyData == (Keys.Right))
            return true;

        return base.ProcessCmdKey(ref msg, keyData);
    }
}

另一种实现方式,发布TabsVisible属性:

using System;
using System.ComponentModel;
using System.Windows.Forms;

public class WizardPages : TabControl {
    private bool tabsVisible;

    [DefaultValue(false)]
    public bool TabsVisible {
        get { return tabsVisible; }
        set {
            if (tabsVisible == value) return;
            tabsVisible = value;
            RecreateHandle();
        }
    }

    protected override void WndProc(ref Message m) {
        // Hide tabs by trapping the TCM_ADJUSTRECT message
        if (m.Msg == 0x1328) {
            if (!tabsVisible && !DesignMode) {
                m.Result = (IntPtr)1;
                return;
            }
        }
        base.WndProc(ref m);
    }
    
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == (Keys.Control | Keys.Tab) || keyData == (Keys.Control | Keys.Shift | Keys.Tab) || keyData == (Keys.Left) || keyData == (Keys.Right))
            return true;

        return base.ProcessCmdKey(ref msg, keyData);
    }
}

设计期间,与正常TabControl一样:

运行期效果如下:

 

参考资料:

Tabs in Winform

Hide Tab Header on C# TabControl

posted on 2018-08-28 15:45  楚人无衣  阅读(1243)  评论(0编辑  收藏  举报