如何将选取框滚动和透明度结合到标签控件中

时间:2019-06-20 12:50:13

标签: c# winforms label transparent marquee

我发现以下两个标签控件分别处理标签中文本的选取框滚动,并使标签部分透明。它们分别很好地工作,但是鉴于我有限的C#,我很难将它们组合到一个控件中。

有人可以给我一些线索吗?

透明标签:

public class LabelTransparent : Label
    {
        private int opacity;
        public Color clrTransparentColor;
        public LabelTransparent()
        {
            this.clrTransparentColor = 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);
                }
            }
        }

        public int Opacity
        {
            get { return opacity; }
            set { if (value >= 0 && value <= 255) opacity = value; this.Invalidate(); }
        }
        public Color TransparentBackColor
        {
            get { return clrTransparentColor; }
            set { clrTransparentColor = value; this.Invalidate(); }
        }
        [Browsable(false)]
        public override Color BackColor
        {
            get { return Color.Transparent; }
            set { base.BackColor = Color.Transparent; }
        }
    }

字幕滚动:

public class LabelMarquee : Label
    {
        private int CurrentPosition { get; set; }
        private Timer Timer = new Timer();
        private Graphics grText;
        private float fTextPixels;

        public int ScrollSpeed
        {
            get { return this.Timer.Interval; }
            set { try { this.Timer.Interval = value; } catch (Exception) { this.Timer.Interval = 15; } }
        }                
        public LabelMarquee()
        {
            UseCompatibleTextRendering = true;
            grText = this.CreateGraphics();
            Timer.Tick += new EventHandler(Timer_Tick);
            Timer.Start();
        }
        void Timer_Tick(object sender, EventArgs e)
        {
            fTextPixels      = grText.MeasureString(this.Text, this.Font).Width;
            if (CurrentPosition < -this.fTextPixels) CurrentPosition = Width;
            else CurrentPosition -= 1;

            Invalidate();
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.TranslateTransform((float)CurrentPosition, 0);
            base.OnPaint(e);
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (Timer != null)
                    Timer.Dispose();
            }
            Timer = null;
        }
    }

0 个答案:

没有答案
相关问题