简体   繁体   English

如何制作透明的tabPage?

[英]How can I make a transparent tabPage?

How can I make a transparent tabPage? 如何制作透明的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 TransparencyKey property nor OnPaintBackground` method. 我发现了一些解决方案,例如将Form的BackColorTransparencyKey设置为Color.LimeGreen类的颜色,或者使用空方法覆盖OnPaintBackground ,但是TabPage既没有TransparencyKey property nor OnPaintBackground`方法。 How can I do that? 我怎样才能做到这一点?

TabControl is a native Windows component, it always draws the tab pages opaque with no built-in support for transparency. TabControl是Windows的本机组件,它始终将标签页绘制为不透明,并且不内置对透明度的支持。 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. 您所要做的就是使用面板来托管现在位于选项卡页面上的控件,并通过SelectedIndexChanged事件使正确的控件可见。

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: 在窗体的Load事件处理程序中调用MakeTransparent()方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM