첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ OutlineTextLabel.cs

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

namespace TestProject
{
    /// <summary>
    /// 외곽선 레이블
    /// </summary>
    public partial class OutlineTextLabel : Label
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 테두리 크기
        /// </summary>
        private float borderSize;

        /// <summary>
        /// 테두리 색상
        /// </summary>
        private Color borderColor;

        /// <summary>
        /// 전경색 브러시
        /// </summary>
        private SolidBrush foregroundColorBrush;

        /// <summary>
        /// 그리기 크기
        /// </summary>
        private SizeF drawSize;

        /// <summary>
        /// 그리기 포인트
        /// </summary>
        private PointF drawPoint;

        /// <summary>
        /// 그리기 펜
        /// </summary>
        private Pen drawPen;

        /// <summary>
        /// 그리기 경로
        /// </summary>
        private GraphicsPath drawPath;

        /// <summary>
        /// 빈 포인트
        /// </summary>
        private PointF emptyPoint = PointF.Empty;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 테두리 크기 - BorderSize

        /// <summary>
        /// 테두리 크기
        /// </summary>
        [Category("OutlineTextLabel")]
        [Browsable(true)]
        [DefaultValue(1f)]
        [Description("테두리 두께")]
        public float BorderSize
        {
            get
            {
                return this.borderSize;
            }
            set
            {
                this.borderSize = value;

                if(value == 0)
                {
                    this.drawPen.Color = Color.Transparent;
                }
                else
                {
                    this.drawPen.Color = this.BorderColor;

                    this.drawPen.Width = value;
                }

                OnTextChanged(EventArgs.Empty);
            }
        }

        #endregion
        #region 테두리 색상 - BorderColor

        /// <summary>
        /// 테두리 색상
        /// </summary>
        [Category("OutlineTextLabel")]
        [Browsable(true)]
        [DefaultValue(typeof(Color), "White")]
        [Description("테두리 색상")]
        public Color BorderColor
        {
            get
            {
                return this.borderColor;
            }
            set
            {
                this.borderColor = value;

                if(this.BorderSize != 0)
                {
                    this.drawPen.Color = value;
                }

                Invalidate();
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - OutlineTextLabel()

        /// <summary>
        /// 생성자
        /// </summary>
        public OutlineTextLabel()
        {
            this.borderSize           = 1f;
            this.borderColor          = Color.White;
            this.drawPen              = new Pen(new SolidBrush(this.borderColor), this.borderSize);
            this.drawPath             = new GraphicsPath();
            this.foregroundColorBrush = new SolidBrush(this.ForeColor);

            Invalidate();
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Protected

        #region 전경색 변경시 처리하기 - OnForeColorChanged(e)

        /// <summary>
        /// 전경색 변경시 처리하기
        /// </summary>
        /// <param name="e">이벤트 발생자</param>
        protected override void OnForeColorChanged(EventArgs e)
        {
            this.foregroundColorBrush.Color = base.ForeColor;

            base.OnForeColorChanged(e);
        }

        #endregion
        #region 페인트시 처리하기 - OnPaint(e)

        /// <summary>
        /// 페인트시 처리하기
        /// </summary>
        /// <param name="e">이벤트 발생자</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            if(Text.Length == 0)
            {
                return;
            }

            e.Graphics.SmoothingMode      = SmoothingMode.AntiAlias;
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;

            this.drawSize = e.Graphics.MeasureString
            (
                Text,
                Font,
                this.emptyPoint,
                StringFormat.GenericTypographic
            );
            
            if(AutoSize)
            {
                this.drawPoint.X = this.Padding.Left;
                this.drawPoint.Y = this.Padding.Top;
            }
            else
            {
                if
                (
                    TextAlign == ContentAlignment.TopLeft    ||
                    TextAlign == ContentAlignment.MiddleLeft ||
                    TextAlign == ContentAlignment.BottomLeft
                )
                {
                    this.drawPoint.X = this.Padding.Left;
                }
                else if
                (
                    TextAlign == ContentAlignment.TopCenter    ||
                    TextAlign == ContentAlignment.MiddleCenter ||
                    TextAlign == ContentAlignment.BottomCenter
                )
                {
                    this.drawPoint.X = (Width - this.drawSize.Width) / 2;
                }
                else
                {
                    this.drawPoint.X = Width - (Padding.Right + this.drawSize.Width);
                }

                if
                (
                    TextAlign == ContentAlignment.TopLeft   ||
                    TextAlign == ContentAlignment.TopCenter ||
                    TextAlign == ContentAlignment.TopRight
                )
                {
                    this.drawPoint.Y = Padding.Top;
                }
                else if
                (
                    TextAlign == ContentAlignment.MiddleLeft   ||
                    TextAlign == ContentAlignment.MiddleCenter ||
                    TextAlign == ContentAlignment.MiddleRight
                )
                {
                    this.drawPoint.Y = (Height - this.drawSize.Height) / 2;
                }
                else
                {
                    this.drawPoint.Y = Height - (Padding.Bottom + this.drawSize.Height);
                }
            }

            float fontSize = e.Graphics.DpiY * Font.SizeInPoints / 72;
            
            this.drawPath.Reset();

            this.drawPath.AddString
            (
                Text,
                Font.FontFamily,
                (int)Font.Style,
                fontSize,
                this.drawPoint,
                StringFormat.GenericTypographic
            );

            e.Graphics.FillPath(this.foregroundColorBrush, this.drawPath);

            e.Graphics.DrawPath(this.drawPen, this.drawPath);
        }

        #endregion

        #region 리소스 해제하기 - Dispose(disposing)

        /// <summary>
        /// 리소스 해제하기
        /// </summary>
        /// <param name="disposing">리소스 해제 여부</param>
        protected override void Dispose(bool disposing)
        {
            if(disposing)
            {
                if(this.foregroundColorBrush != null)
                {
                    this.foregroundColorBrush.Dispose();
                }

                if(this.drawPath != null)
                {
                    this.drawPath.Dispose();
                }

                if(this.drawPen != null)
                {
                    this.drawPen.Dispose();
                }
            }

            base.Dispose(disposing);
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요