■ FontHelper 클래스 사용하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


FontHelper.cs

 

 

using System.Windows;

using System.Windows.Media;

 

namespace TestProject

{

    /// <summary>

    /// 폰트 헬퍼

    /// </summary>

    [System.Runtime.Serialization.DataContract]

    public class FontHelper

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 폰트 패밀리

        /// </summary>

        [System.Runtime.Serialization.DataMember]

        private string fontFamily;

 

        /// <summary>

        /// 폰트 가중치

        /// </summary>

        [System.Runtime.Serialization.DataMember]

        private int fontWeight;

        

        /// <summary>

        /// 폰트 신축성

        /// </summary>

        [System.Runtime.Serialization.DataMember]

        private int fontStretch;

        

        /// <summary>

        /// 폰트 스타일

        /// </summary>

        [System.Runtime.Serialization.DataMember]

        private int fontStyle;

 

        /// <summary>

        /// 폰트 크기

        /// </summary>

        [System.Runtime.Serialization.DataMember]

        private double fontSize;

 

        /// <summary>

        /// 색상

        /// </summary>

        [System.Runtime.Serialization.DataMember]

        private Color color;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 폰트 - Font

 

        /// <summary>

        /// 폰트

        /// </summary>

        public System.Drawing.Font Font

        {

            get

            {

                System.Drawing.FontStyle fontStyle = System.Drawing.FontStyle.Regular;

 

                if(FontWeight.ToOpenTypeWeight() >= FontWeights.Bold.ToOpenTypeWeight())

                {

                    fontStyle |= System.Drawing.FontStyle.Bold;

                }

 

                if(FontStyle == FontStyles.Italic)

                {

                    fontStyle |= System.Drawing.FontStyle.Italic;

                }

 

                return new System.Drawing.Font(this.fontFamily, (float)this.fontSize, fontStyle);

            }

            set

            {

                this.fontFamily = value.FontFamily.Name;

 

                this.fontStretch = FontStretches.Normal.ToOpenTypeStretch();

 

                this.fontSize = value.Size;

 

                if(value.Bold == true)

                {

                    this.fontWeight = FontWeights.Bold.ToOpenTypeWeight();

                }

                else

                {

                    this.fontWeight = FontWeights.Normal.ToOpenTypeWeight();

                }

 

                if(value.Italic == true)

                {

                    FontStyle = FontStyles.Italic;

                }

                else

                {

                    FontStyle = FontStyles.Normal;

                }

            }

        }

 

        #endregion

        #region 폰트 가중치 - FontWeight

 

        /// <summary>

        /// 폰트 가중치

        /// </summary>

        public FontWeight FontWeight

        {

            get

            {

                return FontWeight.FromOpenTypeWeight(this.fontWeight);

            }

            set

            {

                this.fontWeight = value.ToOpenTypeWeight();

            }

        }

 

        #endregion

        #region 폰트 신축성 - FontStretch

 

        /// <summary>

        /// 폰트 신축성

        /// </summary>

        public FontStretch FontStretch

        {

            get

            {

                return FontStretch.FromOpenTypeStretch(this.fontStretch);

            }

            set

            {

                this.fontStretch = value.ToOpenTypeStretch();

            }

        }

 

        #endregion

        #region 폰트 스타일 - FontStyle

 

        /// <summary>

        /// 폰트 스타일

        /// </summary>

        public FontStyle FontStyle

        {

            get

            {

                switch(this.fontStyle)

                {

                    case 1  : return FontStyles.Oblique;

                    case 2  : return FontStyles.Italic;

                    default : return FontStyles.Normal;

                }

            }

            set

            {

                if(value == FontStyles.Oblique)

                {

                    this.fontStyle = 1;

                }

                else if(value == FontStyles.Italic)

                {

                    this.fontStyle = 2;

                }

                else

                {

                    this.fontStyle = 0;

                }

            }

        }

 

        #endregion

        #region 폰트 크기 - FontSize

 

        /// <summary>

        /// 폰트 크기

        /// </summary>

        public double FontSize

        {

            get

            {

                return this.fontSize;

            }

            set

            {

                this.fontSize = value;

            }

        }

 

        #endregion

        #region 폰트 패밀리 - FontFamily

 

        /// <summary>

        /// 폰트 패밀리

        /// </summary>

        public FontFamily FontFamily

        {

            get

            {

                return new FontFamily(this.fontFamily);

            }

            set

            {

                this.fontFamily = value.Source;

            }

        }

 

        #endregion

        #region 폰트 전경색 - Foreground

 

        /// <summary>

        /// 폰트 전경색

        /// </summary>

        public Brush Foreground

        {

            get

            {

                return new SolidColorBrush(this.color);

            }

            set

            {

                color = (value as SolidColorBrush).Color;

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - FontHelper()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public FontHelper()

        {

            fontFamily  = "Arial";

            fontWeight  = FontWeights.Normal.ToOpenTypeWeight();

            fontStretch = FontStretches.Normal.ToOpenTypeStretch();

            fontStyle   = 0;

            fontSize    = 12.0;

            color       = Brushes.Black.Color;

        }

 

        #endregion

        #region 생성자 - FontHelper(font, color)

 

        /// <summary>

        /// 생성자

        /// </summary>

        /// <param name="font">폰트</param>

        /// <param name="color">색상</param>

        public FontHelper(System.Drawing.Font font, Color color)

        {

            Font = font;

 

            this.color = color;

        }

 

        #endregion

    }

}

 

 

MainWindow.xaml.cs

 

 

using System.Windows;

using System.Windows.Controls;

using System.Windows.Media;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            this.fontButton.Click += fontButton_Click;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 폰트 버튼 클릭시 처리하기 - fontButton_Click(sender, e)

 

        /// <summary>

        /// 폰트 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void fontButton_Click(object sender, RoutedEventArgs e)

        {

            System.Windows.Forms.FontDialog fontDialog = new System.Windows.Forms.FontDialog();

 

            fontDialog.ShowColor = true;

 

            if(fontDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)

            {

                ApplyFont(this.textBlock, fontDialog.Font, fontDialog.Color);

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 폰트 적용하기 - ApplyFont(textBlock, font, fontColor)

 

        /// <summary>

        /// 폰트 적용하기

        /// </summary>

        /// <param name="textBlock">텍스트 블럭</param>

        /// <param name="font">폰트</param>

        /// <param name="fontColor">폰트 색상</param>

        private void ApplyFont(TextBlock textBlock, System.Drawing.Font font, System.Drawing.Color fontColor)

        {

            FontHelper helper = new FontHelper(font, Color.FromArgb(255, fontColor.R, fontColor.G, fontColor.B));

 

            textBlock.FontFamily  = helper.FontFamily;

            textBlock.FontSize    = helper.FontSize;

            textBlock.FontStretch = helper.FontStretch;

            textBlock.FontStyle   = helper.FontStyle;

            textBlock.FontWeight  = helper.FontWeight;

            textBlock.Foreground  = helper.Foreground;

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요