반응형

 

그림자(Shadow) + 그라디언트(Gradient) 배경 라벨(Label)

  • 그림자 색상, 위치 지정 가능
  • 그라디언트 색상, 방향 지정 가능

 

ShadowLabel.cs

using System;
using System.ComponentModel;
using System.Collections;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace ShadowLabelTest {
    [ToolboxItem(true)]
    public class ShadowLabel : Label
    {
        private bool _drawGradient = true;
        private Color _startColor = Color.White;
        private Color _endColor = Color.LightSkyBlue;
        private float _angle = 0;

        private bool _drawShadow = true;
        private float _yOffset = 1;
        private float _xOffset = 1;
        private Color _shadowColor = Color.Black;

        public ShadowLabel() {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs e) {
            //base.OnPaint (e);

            Rectangle rectText = this.ClientRectangle;
            Rectangle rectShadow = new Rectangle(rectText.Left + (int)_xOffset, rectText.Top + (int)_yOffset, rectText.Width, rectText.Height);

            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            if (_drawGradient == true) {
                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, this.Width, this.Height), _startColor, _endColor, _angle, true);
                e.Graphics.FillRectangle(brush, 0, 0, this.Width, this.Height);
            }

            //정렬값
            TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter; ;
            if(this.TextAlign == ContentAlignment.BottomCenter) {
                flags = TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter;
            } else if(this.TextAlign == ContentAlignment.BottomLeft) {
                flags = TextFormatFlags.Bottom | TextFormatFlags.Left;
            } else if(this.TextAlign == ContentAlignment.BottomRight) {
                flags = TextFormatFlags.Bottom | TextFormatFlags.Right;
            } else if(this.TextAlign == ContentAlignment.MiddleCenter) {
                flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter;
            } else if(this.TextAlign == ContentAlignment.MiddleLeft) {
                flags = TextFormatFlags.VerticalCenter | TextFormatFlags.Left;
            } else if(this.TextAlign == ContentAlignment.MiddleRight) {
                flags = TextFormatFlags.VerticalCenter | TextFormatFlags.Right;
            } else if(this.TextAlign == ContentAlignment.TopCenter) {
                flags = TextFormatFlags.Top | TextFormatFlags.HorizontalCenter;
            } else if(this.TextAlign == ContentAlignment.TopLeft) {
                flags = TextFormatFlags.Top | TextFormatFlags.Left;
            } else if(this.TextAlign == ContentAlignment.TopRight) {
                flags = TextFormatFlags.Top | TextFormatFlags.Right;
            }

            //그림자 그리기
            if(_drawShadow == true) {
                TextRenderer.DrawText(e.Graphics, Text, this.Font, rectShadow, this._shadowColor, flags);
            }

            //텍스트 그리기
            TextRenderer.DrawText(e.Graphics, Text, this.Font, rectText, this.ForeColor, flags);            
        }

        [Category("Gradient"), Description("그라디언트 사용여부"), DefaultValue(true)]
        public bool DrawGradient {
            get { return this._drawGradient; }
            set { this._drawGradient = value; this.Invalidate(); }
        }

        [Category("Gradient"), Description("그라디언트 시작 색상"), DefaultValue(typeof(Color), "Color.White")]
        public Color StartColor {
            get { return this._startColor; }
            set { this._startColor = value; this.Invalidate(); }
        }

        [Category("Gradient"), Description("그라디언트 끝 색상"), DefaultValue(typeof(Color), "Color.LightSkyBlue")]
        public Color EndColor {
            get { return this._endColor; }
            set { this._endColor = value; this.Invalidate(); }
        }

        [Category("Gradient"), Description("그라디언트 방향"), DefaultValue(0)]
        public float Angle {
            get { return this._angle; }
            set { this._angle = value; this.Invalidate(); }
        }

        [Category("Drop Shadow"), Description("그림자 사용여부"), DefaultValue(true)]
        public bool DrawShadow {
            get { return this._drawShadow; }
            set { this._drawShadow = value; this.Invalidate(); }
        }

        [Category("Drop Shadow"), Description("그림자 X 너비"), DefaultValue(1)]
        public float XOffset {
            get { return this._xOffset; }
            set { this._xOffset = value; this.Invalidate(); }
        }

        [Category("Drop Shadow"), Description("그림자 Y 너비"), DefaultValue(1)]
        public float YOffset {
            get { return this._yOffset; }
            set { this._yOffset = value; this.Invalidate(); }
        }

        [Category("Drop Shadow"), Description("그림자 색상"), DefaultValue(typeof(System.Drawing.Color), "Color.Black")]
        public Color ShadowColor {
            get { return this._shadowColor; }
            set { this._shadowColor = value; this.Invalidate(); }
        }

        private void InitializeComponent() {
            this.ForeColor = Color.LightSkyBlue;
        }
    }
}

 

 

프로젝트 소스 다운↓

ShadowLabel.zip
0.06MB

반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기