첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            ResizeRedraw = true;

            #region 이벤트를 설정한다.

            Paint += Form_Paint;

            #endregion
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 폼 페인트시 처리하기 - Form_Paint(sender, e)

        /// <summary>
        /// 폼 페인트시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
            e.Graphics.SmoothingMode     = SmoothingMode.AntiAlias;

            int width  = ClientSize.Width;
            int height = ClientSize.Height / 2;

            Rectangle rectangle = new Rectangle(0, 0, width, height);

            using(Font font = new Font("나눔고딕코딩", 60, FontStyle.Italic))
            {
                DrawStringWithCharacterRectangle(e.Graphics, "테스트", font, rectangle);
            }

            width /= 2;

            rectangle = new Rectangle(0, height, width, height);

            using(Font font = new Font("나눔고딕코딩", 60, FontStyle.Regular))
            {
                DrawStringWithCharacterRectangle(e.Graphics, "테스트", font, rectangle);
            }

            rectangle = new Rectangle(width, height, width, height);

            using(Font font = new Font("나눔고딕코딩", 60, FontStyle.Italic))
            {
                DrawStringWithCharacterRectangle(e.Graphics, "테스트", font, rectangle);
            }
        }

        #endregion

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

        #region 문자 사각형을 갖는 문자열 그리기 - DrawStringWithCharacterRectangle(graphics, text, font, rectangle)

        /// <summary>
        /// 문자 사각형을 갖는 문자열 그리기
        /// </summary>
        /// <param name="graphics">그래픽스</param>
        /// <param name="text">텍스트</param>
        /// <param name="font">폰트</param>
        /// <param name="rectangle">사각형</param>
        private void DrawStringWithCharacterRectangle(Graphics graphics, string text, Font font, Rectangle rectangle)
        {
            using(StringFormat stringFormat = new StringFormat())
            {
                stringFormat.Alignment     = StringAlignment.Center;
                stringFormat.LineAlignment = StringAlignment.Center;

                graphics.DrawString(text, font, Brushes.Blue, rectangle, stringFormat);

                List<CharacterRange> characterRangeList = new List<CharacterRange>();

                for(int i = 0; i < text.Length; i++)
                {
                    characterRangeList.Add(new CharacterRange(i, 1));
                }

                stringFormat.SetMeasurableCharacterRanges(characterRangeList.ToArray());

                Region[] regionArray = graphics.MeasureCharacterRanges(text, font, rectangle, stringFormat);

                for(int i = 0; i < text.Length; i++)
                {
                    Rectangle textRectangle = Rectangle.Round(regionArray[i].GetBounds(graphics));

                    graphics.DrawRectangle(Pens.Red, textRectangle);
                }
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker

댓글을 달아 주세요