Reasons for why a WinForms label does not want to be transparent?

后端 未结 11 1833
抹茶落季
抹茶落季 2020-11-28 10:42

Why can\'t I set the BackColor of a Label to Transparent? I have done it before, but now it just don\'t want to...

I created a new UserControl, added a progressbar a

11条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 11:06

    Here is a transparent control I wrote a while ago which displays rotated text. Most of the code comes from here, though IIRC I had to make a few tweaks to get it to work.

    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Text;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace MyNamespace
    {
        public partial class RotatedText : UserControl
        {
            private readonly Timer _invalidationTimer;
            private const int WS_EX_TRANSPARENT = 0x00000020;
    
            public RotatedText()
            {
                this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
                InitializeComponent();
    
                _invalidationTimer = new Timer {Interval = 500, Enabled = true};
                _invalidationTimer.Tick += TickHandler;
            }
    
            [Browsable(true)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
            [Category("Appearance")]
            [Description("Text which appears in control")]
            public string Text { get; set; }
    
            #region Transparent background
            protected override CreateParams CreateParams
            {
                get
                {
                    CreateParams cp = base.CreateParams;
                    cp.ExStyle |= WS_EX_TRANSPARENT;
                    return cp;
                }
            }
    
            private void TickHandler(object sender, EventArgs e)
            {
                InvalidateEx();
            }
    
            private void InvalidateEx()
            {
                if (Parent != null)
                    Parent.Invalidate(Bounds, false);
                else
                    Invalidate();
            }
    
            protected override void OnPaintBackground(PaintEventArgs e)
            {
                //Intentionally do nothing - stops background from drawing
                //base.OnPaintBackground(e);
            } 
            #endregion
    
            //Rotate text and draw
            protected override void OnPaint(PaintEventArgs e)
            {
                double angleRadians = Math.Atan2(Height, Width);
                float angleDegrees = -1*(float) (angleRadians*180/Math.PI);
                angleDegrees *= 0.9f;
                e.Graphics.RotateTransform(angleDegrees, MatrixOrder.Append);
                e.Graphics.TranslateTransform(20, Height - 75, MatrixOrder.Append);
                e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
                Font font = new Font("Ariel", 50);
                e.Graphics.DrawString(Text, font, Brushes.Gray, 1, 2); //Shadow
                e.Graphics.DrawString(Text, font, Brushes.Red, 0, 0);
            }
        }
    }
    

提交回复
热议问题