在其他控件上方显示带有半透明BackColor的标签? [英] Show a Label with semi-transparent BackColor above other controls?

查看:97
本文介绍了在其他控件上方显示带有半透明BackColor的标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义控件,其中有两个 PictureBox控件,它们经过了动画标签控件



设置了子索引,以便标签始终位于顶部,但图片框却在互换,因此在动画时它们分别显示不同的图像



据我所知,标签需要有一个父控件,在它上面可以支持半透明颜色(Argb)。由于标签具有活动图片框作为其父标签,因此也将对其进行动画处理,这根本不是我想要的。



有没有办法固定相对于孩子的位置

解决方案

要具有透明标签控件,您可以覆盖



示例实施

 公共类TransparentLabel:标签
{
public TransparentLabel()
{
this.transparentBackColor = Color.Blue;
this.opacity = 50;
this.BackColor = Color.Transparent;
}
受保护的覆盖无效OnPaint(PaintEventArgs e)
{
if(Parent!= null)
{
using(var bmp = new Bitmap( Parent.Width,Parent.Height))
{
Parent.Controls.Cast< Control>()
.Where(c => Parent.Controls.GetChildIndex(c)> Parent .Controls.GetChildIndex(this)
.Where(c => c.Bounds.IntersectsWith(this.Bounds))
.OrderByDescending(c => Parent.Controls.GetChildIndex(c))
.ToList()
.ForEach(c => c.DrawToBitmap(bmp,c.Bounds));


e.Graphics.DrawImage(bmp,-Left,-Top);
使用(var b = new SolidBrush(Color.FromArgb(this.Opacity,this.TransparentBackColor)))
{
e.Graphics.FillRectangle(b,this.ClientRectangle);
}
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
TextRenderer.DrawText(e.Graphics,this.Text,this.Font,this.ClientRectangle,this.ForeColor,Color.Transparent);
}
}
}

私人不透明性;
public int不透明度
{
get {返回不透明度; }
set
{
if(value> = 0&& value< = 255)
opacity = value;
this.Invalidate();
}
}

public Color transparentBackColor;
public Color TransparentBackColor
{
get {return transparentBackColor; }
设置
{
transparentBackColor = value;
this.Invalidate();
}
}

[Browsable(false)]
公共重写颜色BackColor
{
获得
{
返回Color.Transparent;
}
set
{
base.BackColor = Color.Transparent;
}
}
}


I've got a custom control with two PictureBox controls that are animated and a label control over them.

The child indexes are set so that label is always on top but the picture boxes are interchanging so when animated they display different images each time.

As I understand, label needs to have a parent control on top of which it can support a semi transparent color (Argb). Since the label has active picture box as its parent it will also be animated with which is not what I want at all.

Is there a way to fix a child position relative to parents parent?

解决方案

To have a transparent label control, you can override the OnPaint method and draw all controls that intersects with label, at last draw the background and text of the label.

Also when moving your picture boxes, don't forget to call the Invalidate() method of the transparent label.

Screenshot

Sample Implementation

public class TransparentLabel : Label
{
    public TransparentLabel()
    {
        this.transparentBackColor = Color.Blue;
        this.opacity = 50;
        this.BackColor = Color.Transparent;
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        if (Parent != null)
        {
            using (var bmp = new Bitmap(Parent.Width, Parent.Height))
            {
                Parent.Controls.Cast<Control>()
                      .Where(c => Parent.Controls.GetChildIndex(c) > Parent.Controls.GetChildIndex(this))
                      .Where(c => c.Bounds.IntersectsWith(this.Bounds))
                      .OrderByDescending(c => Parent.Controls.GetChildIndex(c))
                      .ToList()
                      .ForEach(c => c.DrawToBitmap(bmp, c.Bounds));


                e.Graphics.DrawImage(bmp, -Left, -Top);
                using (var b = new SolidBrush(Color.FromArgb(this.Opacity, this.TransparentBackColor)))
                {
                    e.Graphics.FillRectangle(b, this.ClientRectangle);
                }
                e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
                TextRenderer.DrawText(e.Graphics, this.Text, this.Font, this.ClientRectangle, this.ForeColor, Color.Transparent);
            }
        }
    }

    private int opacity;
    public int Opacity
    {
        get { return opacity; }
        set
        {
            if (value >= 0 && value <= 255)
                opacity = value;
            this.Invalidate();
        }
    }

    public Color transparentBackColor;
    public Color TransparentBackColor
    {
        get { return transparentBackColor; }
        set
        {
            transparentBackColor = value;
            this.Invalidate();
        }
    }

    [Browsable(false)]
    public override Color BackColor
    {
        get
        {
            return Color.Transparent;
        }
        set
        {
            base.BackColor = Color.Transparent;
        }
    }
}

这篇关于在其他控件上方显示带有半透明BackColor的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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