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

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;

            DrawString(e.Graphics, new PointF(120, 120), new PointF(430, 250), "직선 세그먼트 위의 텍스트 입니다.", true );
            DrawString(e.Graphics, new PointF(120, 120), new PointF(430, 250), "직선 세그먼트 아래 텍스트 입니다.", false);

            DrawString(e.Graphics, new PointF(430, 300), new PointF(120, 220), "직선 세그먼트 위의 텍스트 입니다.", true );
            DrawString(e.Graphics, new PointF(430, 300), new PointF(120, 220), "직선 세그먼트 아래 텍스트 입니다.", 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, startPoint, endPoint, text, textAboveSegment)

        /// <summary>
        /// 문자열 그리기
        /// </summary>
        /// <param name="graphics">그래픽스</param>
        /// <param name="startPoint">시작 포인트</param>
        /// <param name="endPoint">종료 포인트</param>
        /// <param name="text">텍스트</param>
        /// <param name="textAboveSegment">세그먼트 위 텍스트 여부</param>
        private void DrawString(Graphics graphics, PointF startPoint, PointF endPoint, string text, bool textAboveSegment)
        {
            int firstCharacter = 0;

            graphics.DrawLine(Pens.Green, startPoint, endPoint);

            DrawString(graphics, Brushes.Blue, Font, text, ref firstCharacter, ref startPoint, endPoint, textAboveSegment);
        }

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

댓글을 달아 주세요