Shadow Label Control C#

Architecture

Introduction

When a customer looks at an application for the first time, they make a lot of decisions based upon the overall appearance of the application. To a developer this can be very frustrating. Many people don’t care if the form that is launched by a button click took ten hours to code. They only care about the appearance. Recently I was getting ready for a software rollout. During my testing and QA I noticed that the plain old Label component bundled in the .NET framework just was too plain to use a banner across the top of my forms. Therefore in order to help the appearance of my form I created a label that can draw a gradient background and some 3D text.  

Using the code

I started by creating a new component that derives from the standard System.Windows.Forms.Label control. Then I overrode the Paint method. Originally I hard coded the colors used in the gradient, drop shadow color etc.

    protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            if (_drawGradient == true)
            {
                using (LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, Width, Height), _startColor, _endColor, _angle, true))
                {
                    e.Graphics.FillRectangle(brush, 0, 0, Width, Height);
                }
            }
            using (StringFormat Sf = new StringFormat())
            {
                switch (TextAlign)
                {
                    case ContentAlignment.BottomCenter:
                        Sf.Alignment = StringAlignment.Center;
                        Sf.LineAlignment = StringAlignment.Far;
                        break;
                    case ContentAlignment.BottomLeft:
                        Sf.Alignment = StringAlignment.Near;
                        Sf.LineAlignment = StringAlignment.Far;
                        break;
                    case ContentAlignment.BottomRight:
                        Sf.Alignment = StringAlignment.Far;
                        Sf.LineAlignment = StringAlignment.Far;
                        break;
                    case ContentAlignment.MiddleCenter:
                        Sf.Alignment = StringAlignment.Center;
                        Sf.LineAlignment = StringAlignment.Center;
                        break;
                    case ContentAlignment.MiddleLeft:
                        Sf.Alignment = StringAlignment.Near;
                        Sf.LineAlignment = StringAlignment.Center;
                        break;
                    case ContentAlignment.MiddleRight:
                        Sf.Alignment = StringAlignment.Far;
                        Sf.LineAlignment = StringAlignment.Center;
                        break;
                    case ContentAlignment.TopCenter:
                        Sf.Alignment = StringAlignment.Center;
                        Sf.LineAlignment = StringAlignment.Near;
                        break;
                    case ContentAlignment.TopLeft:
                        Sf.Alignment = StringAlignment.Near;
                        Sf.LineAlignment = StringAlignment.Near;
                        break;
                    case ContentAlignment.TopRight:
                        Sf.Alignment = StringAlignment.Far;
                        Sf.LineAlignment = StringAlignment.Near;
                        break;
                }
                Sf.FormatFlags = StringFormatFlags.NoClip;
                if (_drawShadow == true)
                {
                    Rectangle retc = new Rectangle();
                    retc = ClientRectangle;
                    retc.X = _xOffset;
                    retc.Y = _yOffset;
                    e.Graphics.DrawString(Text, Font, new SolidBrush(_shadowColor), retc, Sf);
                }
                e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), ClientRectangle, Sf);
            }

Download source – 45.62 Kb

Leave a Reply

Your email address will not be published. Required fields are marked *