在其他控件上方显示带有半透明BackColor的标签?

编程入门 行业动态 更新时间:2024-10-03 23:32:06
本文介绍了在其他控件上方显示带有半透明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的标签?

本文发布于:2023-11-03 04:38:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554249.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   标签   BackColor

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!