如何制作透明的tabPage? [英] How can I make a transparent tabPage?

查看:249
本文介绍了如何制作透明的tabPage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何制作透明的tabPage?我发现了一些解决方案,例如将Form的BackColorTransparencyKey都设置为类似Color.LimeGreen的颜色,或者使用空方法覆盖OnPaintBackground,但是TabPage既没有TransparencyKey property nor 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?

推荐答案

TabControl是Windows的本机组件,它始终将标签页绘制为不透明的,并且不内置对透明度的支持.解决此问题需要开箱即用的思路,带有透明选项卡页面的选项卡控件仅演变为可见的选项卡即可.您所要做的就是使用面板来托管现在位于选项卡页面上的控件,并通过SelectedIndexChanged事件使正确的控件可见.

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);
    }
}

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

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

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

这篇关于如何制作透明的tabPage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆