如何创建没有选项卡标题的 TabControl?

发布于 2024-10-15 20:30:34 字数 99 浏览 2 评论 0原文

如何制作不显示选项卡标题的选项卡管理器?

这是一个winforms应用程序,使用选项卡管理器的目的是只能通过代码更改显示内容。它非常适合各种菜单选项更改屏幕内容的菜单。

How do I make a tab manager that doesn't show the tab headers?

This is a winforms application, and the purpose of using a tab manager is so the display content can only be changed through code. It's good for menus where various menu options change the screen contents.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夜光 2024-10-22 20:30:34

一旦您了解了技巧,隐藏标准 TabControl 上的选项卡就非常简单。向选项卡控件发送 TCM_ADJUSTRECT 消息当它需要调整选项卡大小时,我们只需要捕获该消息即可。 (我确信之前已经回答过这个问题,但是发布代码比搜索它更容易。)

将以下代码添加到项目中的新类中,重新编译,然后使用 CustomTabControl 类内置控件的:(

class CustomTabControl : TabControl
{
    private const int TCM_ADJUSTRECT = 0x1328;

    protected override void WndProc(ref Message m)
    {
        // Hide the tab headers at run-time
        if (m.Msg == TCM_ADJUSTRECT && !DesignMode)
        {
            m.Result = (IntPtr)1;
            return;
        }

        // call the base class implementation
        base.WndProc(ref m);
    }
}

代码示例最初取自Dot Net Thoughts。)

请注意对于位于侧面或底部的选项卡标题,这将无法正常工作。但这不仅看起来很奇怪,而且您无论如何也无法在运行时看到选项卡。只需将它们放在它们所属的顶部即可。

Hiding the tabs on a standard TabControl is pretty simple, once you know the trick. The tab control is sent a TCM_ADJUSTRECT message when it needs to adjust the tab size, so we just need to trap that message. (I'm sure this has been answered before, but posting the code is easier than searching for it.)

Add the following code to a new class in your project, recompile, and use the CustomTabControl class instead of the built-in control:

class CustomTabControl : TabControl
{
    private const int TCM_ADJUSTRECT = 0x1328;

    protected override void WndProc(ref Message m)
    {
        // Hide the tab headers at run-time
        if (m.Msg == TCM_ADJUSTRECT && !DesignMode)
        {
            m.Result = (IntPtr)1;
            return;
        }

        // call the base class implementation
        base.WndProc(ref m);
    }
}

(Code sample originally taken from Dot Net Thoughts.)

Note that this will not work properly for tab headers positioned on the sides or the bottom. But not only does that just look weird, you won't be able to see the tabs at run-time anyway. Just put them on the top where they belong.

忆悲凉 2024-10-22 20:30:34

是的,如果它是 Web 应用程序,您可以使用相同的位置构建自己的 DIV,并根据您的需要隐藏/显示。

Right, if it's web application, you can build your own DIV with the same placement and hide/show as per your needs.

与往事干杯 2024-10-22 20:30:34

和其他人一样,我发现你的问题有点令人困惑。我之前使用过此处找到的方法。使用这种方式,您可以更改一个属性来决定是否要显示选项卡标题。

Along with everybody else, I find your question a bit confusing. I've used this method found here before. Using this way you have a single property you can change as to whether you want to show the tab headers or not.

燕归巢 2024-10-22 20:30:34

在编辑和评论使问题更加清晰之后,我认为处理此问题的正常方法是使用多个面板而不是选项卡。

After the edit and comments made the question more clear, I think the normal way to handle this is to use multiple panels rather than tabs.

心如荒岛 2024-10-22 20:30:34

我想,使用面板是最简单的解决方案。此外,我建议使用我的(免费、开源)VisualStateManager 来简化切换并消除大量 .Enabled = true 的问题。

软件包在 Nuget 上可用。

只需编写这段代码:

// Contains and propagates information about current page
private SwitchCondition<int> settingPageCondition;
// Controls state of specific controls basing on given SwitchCondition
private VisualStateSwitchController<int> settingPageController;

// (...)

private void InitializeActions()
{
    // Initialize with possible options
    settingPageCondition = new SwitchCondition<int>(0, 1);

    settingPageController = new VisualStateSwitchController<int>(
        null,                  // Enabled is not controlled
        null,                  // Checked is not controlled
        settingPageCondition,  // Visible is controller by settingPageCondition
        new SwitchControlSet<int>(0, pGeneral),   // State 0 controls pGeneral
        new SwitchControlSet<int>(1, pParsing));  // State 1 controls pParsing
}

// (...)

public void MainForm()
{
    InitializeComponent();
    InitializeActions();
}

// (...)

// Wat to set specific page
settingPageCondition.Current = 0;

I guess, that using panels is the simplest solution. In addition, I suggest using my (free, opensource) VisualStateManager to simplify switching and eliminate lots of .Enabled = true horrors.

Package is available on Nuget.

Just write this code:

// Contains and propagates information about current page
private SwitchCondition<int> settingPageCondition;
// Controls state of specific controls basing on given SwitchCondition
private VisualStateSwitchController<int> settingPageController;

// (...)

private void InitializeActions()
{
    // Initialize with possible options
    settingPageCondition = new SwitchCondition<int>(0, 1);

    settingPageController = new VisualStateSwitchController<int>(
        null,                  // Enabled is not controlled
        null,                  // Checked is not controlled
        settingPageCondition,  // Visible is controller by settingPageCondition
        new SwitchControlSet<int>(0, pGeneral),   // State 0 controls pGeneral
        new SwitchControlSet<int>(1, pParsing));  // State 1 controls pParsing
}

// (...)

public void MainForm()
{
    InitializeComponent();
    InitializeActions();
}

// (...)

// Wat to set specific page
settingPageCondition.Current = 0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文