如何制作透明的标签?

发布于 2025-01-19 19:46:23 字数 243 浏览 2 评论 0原文

如何制作透明的标签?我找到了诸如SET SET form backcolorthrencykey之类的解决方案,例如color.limegreen或Override onpaintbackground with a paintbackground 空方法但TABPAGE既没有透明度属性也不具有 OnPaintBackground“方法。我该怎么做?

How can I make a transparent tabPage? I found solutions like set both Form's BackColor and TransparencyKey to a color like Color.LimeGreen or override OnPaintBackground with a empty method but TabPage doesn't have neither TransparencyKeyproperty norOnPaintBackground` method. How can I do that?

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

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

发布评论

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

评论(1

凉宸 2025-01-26 19:46:23

TabControl 是本机 Windows 组件,它始终将选项卡页面绘制为不透明,没有内置的透明度支持。解决这个问题需要一些开箱即用的思维帮助,具有透明选项卡页的选项卡控件只需将选项卡条变为可见即可。您所要做的就是使用面板来承载现在位于选项卡页上的控件,并通过 SelectedIndexChanged 事件使正确的控件可见。

最好将其保留在派生类中,以便您仍然可以在设计时正常使用选项卡控件。将新类添加到您的项目中并粘贴下面所示的代码。编译。将新控件从工具箱顶部拖放到窗体上,替换现有控件。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

class TransparentTabControl : TabControl {
    private List<Panel> pages = new List<Panel>();

    public void MakeTransparent() {
        if (TabCount == 0) throw new InvalidOperationException();
        var height = GetTabRect(0).Bottom;
        // Move controls to panels
        for (int tab = 0; tab < TabCount; ++tab) {
            var page = new Panel {
                Left = this.Left, Top = this.Top + height,
                Width = this.Width, Height = this.Height - height,
                BackColor = Color.Transparent,
                Visible = tab == this.SelectedIndex
            };
            for (int ix = TabPages[tab].Controls.Count - 1; ix >= 0; --ix) {
                TabPages[tab].Controls[ix].Parent = page;
            }
            pages.Add(page);
            this.Parent.Controls.Add(page);
        }
        this.Height = height /* + 1 */;
    }

    protected override void OnSelectedIndexChanged(EventArgs e) {
        base.OnSelectedIndexChanged(e);
        for (int tab = 0; tab < pages.Count; ++tab) {
            pages[tab].Visible = tab == SelectedIndex;
        }
    }

    protected override void Dispose(bool disposing) {
        if (disposing) foreach (var page in pages) page.Dispose();
        base.Dispose(disposing);
    }
}

在表单的 Load 事件处理程序中调用 MakeTransparent() 方法:

private void Form1_Load(object sender, EventArgs e) {
    transparentTabControl1.MakeTransparent();
}

TabControl is a native Windows component, it always draws the tab pages opaque with no built-in support for transparency. Solving this requires a little helping of out-of-the-box thinking, a tab control with transparent tab pages simply devolves to just the tabstrip being visible. All you have to do is use panels to host the controls that are now on the tab pages and make the correct one visible with the SelectedIndexChanged event.

Best to stick this in a derived class so you can still use the tab control normally at design time. 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 the form, replacing the existing one.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

class TransparentTabControl : TabControl {
    private List<Panel> pages = new List<Panel>();

    public void MakeTransparent() {
        if (TabCount == 0) throw new InvalidOperationException();
        var height = GetTabRect(0).Bottom;
        // Move controls to panels
        for (int tab = 0; tab < TabCount; ++tab) {
            var page = new Panel {
                Left = this.Left, Top = this.Top + height,
                Width = this.Width, Height = this.Height - height,
                BackColor = Color.Transparent,
                Visible = tab == this.SelectedIndex
            };
            for (int ix = TabPages[tab].Controls.Count - 1; ix >= 0; --ix) {
                TabPages[tab].Controls[ix].Parent = page;
            }
            pages.Add(page);
            this.Parent.Controls.Add(page);
        }
        this.Height = height /* + 1 */;
    }

    protected override void OnSelectedIndexChanged(EventArgs e) {
        base.OnSelectedIndexChanged(e);
        for (int tab = 0; tab < pages.Count; ++tab) {
            pages[tab].Visible = tab == SelectedIndex;
        }
    }

    protected override void Dispose(bool disposing) {
        if (disposing) foreach (var page in pages) page.Dispose();
        base.Dispose(disposing);
    }
}

Call the MakeTransparent() method in the form's Load event handler:

private void Form1_Load(object sender, EventArgs e) {
    transparentTabControl1.MakeTransparent();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文