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

728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

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

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

        #region 생성자 - MainForm()

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

            #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;
            e.Graphics.InterpolationMode = InterpolationMode.High;

            GraphicsPath path = new GraphicsPath();

            path.AddArc(new RectangleF(90, 90, 320, 220), 180, 180);

            e.Graphics.DrawPath(Pens.Green, path);

            DrawString(e.Graphics, Brushes.Blue, Font, "경로 위의 텍스트 입니다.", path, true );
            DrawString(e.Graphics, Brushes.Blue, Font, "경로 아래 텍스트 입니다.", path, false);

            path = new GraphicsPath();

            path.AddArc(new RectangleF(90, 100, 320, 220), 0, 180);

            e.Graphics.DrawPath(Pens.Red, path);

            DrawString(e.Graphics, Brushes.Blue, Font, "경로 위의 텍스트 입니다.", path, true );
            DrawString(e.Graphics, Brushes.Blue, Font, "경로 아래 텍스트 입니다.", path, false);
        }

        #endregion

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

        #region 문자열 그리기 - DrawString(graphics, brush, font, text, firstCharacter, startPoint, endPoint, textAboveSegment)

        /// <summary>
        /// 문자열 그리기
        /// </summary>
        /// <param name="graphics">그래픽스</param>
        /// <param name="brush">브러시</param>
        /// <param name="font">폰트</param>
        /// <param name="text">텍스트</param>
        /// <param name="firstCharacter">첫번째 문자</param>
        /// <param name="startPoint">시작 포인트</param>
        /// <param name="endPoint">종료 포인트</param>
        /// <param name="textAboveSegment">세그먼트 위 텍스트 여부</param>
        private void DrawString(Graphics graphics, Brush brush, Font font, string text, ref int firstCharacter, ref PointF startPoint, PointF endPoint, bool textAboveSegment)
        {
            float dx = endPoint.X - startPoint.X;
            float dy = endPoint.Y - startPoint.Y;

            float distance = (float)Math.Sqrt(dx * dx + dy * dy);

            dx /= distance;
            dy /= distance;

            int lastCharacter = firstCharacter;

            while(lastCharacter < text.Length)
            {
                string testString = text.Substring(firstCharacter, lastCharacter - firstCharacter + 1);

                if(graphics.MeasureString(testString, font).Width > distance)
                {
                    lastCharacter--;

                    break;
                }

                lastCharacter++;
            }

            if(lastCharacter < firstCharacter)
            {
                return;
            }

            if(lastCharacter >= text.Length)
            {
                lastCharacter = text.Length - 1;
            }

            string fitString = text.Substring(firstCharacter, lastCharacter - firstCharacter + 1);

            GraphicsState state = graphics.Save();

            if(textAboveSegment)
            {
                graphics.TranslateTransform(0, -graphics.MeasureString(fitString, font).Height, MatrixOrder.Append);
            }

            float angle = (float)(180 * Math.Atan2(dy, dx) / Math.PI);

            graphics.RotateTransform(angle, MatrixOrder.Append);

            graphics.TranslateTransform(startPoint.X, startPoint.Y, MatrixOrder.Append);

            graphics.DrawString(fitString, font, brush, 0, 0);

            graphics.Restore(state);

            firstCharacter = lastCharacter + 1;

            float textWidth = graphics.MeasureString(fitString, font).Width;

            startPoint = new PointF
            (
                startPoint.X + dx * textWidth,
                startPoint.Y + dy * textWidth
            );
        }

        #endregion
        #region 문자열 그리기 - DrawString(graphics, brush, font, text, graphicsPath, textAbovePath)

        /// <summary>
        /// 문자열 그리기
        /// </summary>
        /// <param name="graphics">그래픽스</param>
        /// <param name="brush">브러시</param>
        /// <param name="font">폰트</param>
        /// <param name="text">텍스트</param>
        /// <param name="graphicsPath">그래픽스 패스</param>
        /// <param name="textAbovePath">경로 위 텍스트 여부</param>
        private void DrawString(Graphics graphics, Brush brush, Font font, string text, GraphicsPath graphicsPath, bool textAbovePath)
        {
            graphicsPath = (GraphicsPath)graphicsPath.Clone();

            graphicsPath.Flatten();

            int firstCharacter = 0;

            PointF startPoint = graphicsPath.PathPoints[0];

            for(int i = 1; i < graphicsPath.PointCount; i++)
            {
                PointF endPoint = graphicsPath.PathPoints[i];

                DrawString(graphics, brush, font, text, ref firstCharacter, ref startPoint, endPoint, textAbovePath);

                if(firstCharacter >= text.Length)
                {
                    break;
                }
            }
        }

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

댓글을 달아 주세요