A Professional Looking Label

4:23 AM
A Professional Looking Label

Some Picks:
Without Glossy Effect


With Glossy Effect


Coding
This on Inherits Label So
public partial class ProfLabel : Label

Properties
  public string _Text;

        private Color _SecondBackColor = Color.Silver;
        public Color SecondBackColor
        {
            get { return _SecondBackColor; }
            set { _SecondBackColor = value; Invalidate(); }
        }

        private bool _IsGlossyeffect = false;
        public bool GlossyEffect
        {
            get { return _IsGlossyeffect; }
            set { _IsGlossyeffect = value; Invalidate(); }
        }

        private int _BottomLineWidth = 3;
        public int BottomLineWidth
        {
            get { return _BottomLineWidth; }
            set { _BottomLineWidth = value; Invalidate(); }
        }

Now Override the OnPaint Method

 protected override void OnPaint(PaintEventArgs pe)
        {
           
            pe.Graphics.FillRectangle(new SolidBrush(Color.Transparent), this.ClientRectangle);

            _Text = this.Text;
            
            this.AutoSize = false;
            SizeF size = pe.Graphics.MeasureString(_Text, this.Font);

            Rectangle r = new Rectangle();
            r.Size = new Size(int.Parse((Math.Round(size.Width)+2).ToString()),int.Parse(( Math.Round(size.Height)+2).ToString()));
            r.Location = new Point(5, 5);


            SolidBrush b = new SolidBrush(_SecondBackColor);
            pe.Graphics.FillPath(b, GetRoundPath(r, 3));


            if (_IsGlossyeffect)
            {
                Rectangle upperrect = new Rectangle();
                upperrect.Location = new Point(5, 5);
                upperrect.Width = r.Width;
                upperrect.Height = r.Height / 2;

                GraphicsPath upPath = GetRoundPath(upperrect, 3);

                using (Brush brushUpper = new LinearGradientBrush(upperrect, Color.White, _SecondBackColor, LinearGradientMode.Vertical))
                {
                    pe.Graphics.FillPath(brushUpper, upPath);
                }
            }
            
            Point pDrawStrPoint = new Point();
            pDrawStrPoint.X = 5;
            pDrawStrPoint.Y = 5;
            pe.Graphics.DrawString(_Text, this.Font, new SolidBrush(this.ForeColor), pDrawStrPoint);
            
            //Drawing Fine Line
            Point startPoint = new Point();
            startPoint.X = r.Left;
            startPoint.Y = r.Bottom;

            Point endpoint = new Point();
            endpoint.X = this.Width;
            endpoint.Y = r.Bottom;
            pe.Graphics.DrawLine(new Pen(_SecondBackColor, _BottomLineWidth), startPoint, endpoint);
         
        }
Other Helping Functions
 public static GraphicsPath GetRoundPath(Rectangle r, int depth)
        {
            GraphicsPath graphPath = new GraphicsPath();

            graphPath.AddArc(r.X, r.Y, depth, depth, 180, 90);
            graphPath.AddArc(r.X + r.Width - depth, r.Y, depth, depth, 270, 90);
            graphPath.AddArc(r.X + r.Width - depth, r.Y + r.Height - depth, depth, depth, 0, 90);
            graphPath.AddArc(r.X, r.Y + r.Height - depth, depth, depth, 90, 90);
            graphPath.AddLine(r.X, r.Y + r.Height - depth, r.X, r.Y + depth / 2);

            return graphPath;
        }

For Complete Source code email me at : sandeepparekh9@gmail.com

0 comments:

Post a Comment