本文介绍了平滑淡入淡出的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试淡化标签,但它似乎不像表单本身那样具有不透明度属性。
I am trying to fade a label but it doesn't seem to have the opacity property like the form itself.
还有另一种淡化控件的方法吗?
Is there another way to fade controls?
推荐答案您可以使用我的透明控件接受透明颜色并具有不透明度属性(0-100):
You can use my Transparent control that accept transparent color and have opacity property(0 - 100):
public class TranspCtrl : Control { public bool drag = false; public bool enab = false; private int m_opacity = 100; private int alpha; public TranspCtrl() { SetStyle(ControlStyles.SupportsTransparentBackColor, true); SetStyle(ControlStyles.Opaque, true); this.BackColor = Color.Transparent; } public int Opacity { get { if (m_opacity > 100) { m_opacity = 100; } else if (m_opacity < 1) { m_opacity = 1; } return this.m_opacity; } set { this.m_opacity = value; if (this.Parent != null) { Parent.Invalidate(this.Bounds, true); } } } protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle = cp.ExStyle | 0x20; return cp; } } protected override void OnPaint(PaintEventArgs e) { if (!DesignMode) { Graphics g = e.Graphics; Rectangle bounds = new Rectangle(0, 0, this.Width - 1, this.Height - 1); Color frmColor = this.Parent.BackColor; Brush bckColor = default(Brush); alpha = (m_opacity * 255) / 100; if (drag) { Color dragBckColor = default(Color); if (BackColor != Color.Transparent) { int Rb = BackColor.R * alpha / 255 + frmColor.R * (255 - alpha) / 255; int Gb = BackColor.G * alpha / 255 + frmColor.G * (255 - alpha) / 255; int Bb = BackColor.B * alpha / 255 + frmColor.B * (255 - alpha) / 255; dragBckColor = Color.FromArgb(Rb, Gb, Bb); } else { dragBckColor = frmColor; } alpha = 255; bckColor = new SolidBrush(Color.FromArgb(alpha, dragBckColor)); } else { bckColor = new SolidBrush(Color.FromArgb(alpha, this.BackColor)); } if (this.BackColor != Color.Transparent | drag) { g.FillRectangle(bckColor, bounds); } bckColor.Dispose(); g.Dispose(); } base.OnPaint(e); } protected override void OnBackColorChanged(EventArgs e) { if (this.Parent != null) { Parent.Invalidate(this.Bounds, true); } base.OnBackColorChanged(e); } protected override void OnParentBackColorChanged(EventArgs e) { this.Invalidate(); base.OnParentBackColorChanged(e); } }这不是标签,但您可以将绘码更改为绘制文本。
It's not a label but you can change onpaint code to draw a text.
更多推荐
平滑淡入淡出的控件
发布评论