첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.Drawing;
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();

            Paint += Form_Paint;
        }

        #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.SmoothingMode = SmoothingMode.AntiAlias;

            using(Font font = new Font("나눔고딕코딩", 14))
            {
                PointF startPoint;
                PointF endPoint;

                startPoint = new PointF( 20,  20);
                endPoint   = new PointF(350, 100);

                e.Graphics.DrawLine(Pens.Blue, startPoint, endPoint);

                DrawTextOverSegment
                (
                    e.Graphics,
                    Brushes.Blue,
                    font,
                    "세그먼트 위 1",
                    startPoint,
                    endPoint,
                    true
                );

                DrawTextOverSegment
                (
                    e.Graphics,
                    Brushes.Blue,
                    font,
                    "세그먼트 아래 1",
                    startPoint,
                    endPoint,
                    false
                );

                startPoint = new PointF(270, 250);
                endPoint   = new PointF( 30,  60);

                e.Graphics.DrawLine(Pens.Red, startPoint, endPoint);

                DrawTextOverSegment
                (
                    e.Graphics,
                    Brushes.Red,
                    font,
                    "세그먼트 위 2",
                    startPoint,
                    endPoint,
                    true
                );

                DrawTextOverSegment
                (
                    e.Graphics,
                    Brushes.Red,
                    font,
                    "세그먼트 아래 2",
                    startPoint,
                    endPoint,
                    false
                );
            }
        }

        #endregion

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

        #region 세그먼트 상에서 텍스트 그리기 - DrawTextOverSegment(graphics, brush, font, text, startPoint, endPoint, textAboveSegment)

        /// <summary>
        /// 세그먼트 상에서 텍스트 그리기
        /// </summary>
        /// <param name="graphics">그래픽스</param>
        /// <param name="brush">브러시</param>
        /// <param name="font">폰트</param>
        /// <param name="text">텍스트</param>
        /// <param name="startPoint">시작 포인트</param>
        /// <param name="endPoint">종료 포인트</param>
        /// <param name="textAboveSegment">세그먼트 상 텍스트 여부</param>
        private void DrawTextOverSegment
        (
            Graphics graphics,
            Brush    brush,
            Font     font,
            string   text,
            PointF   startPoint,
            PointF   endPoint,
            bool     textAboveSegment
        )
        {
            GraphicsState state = graphics.Save();

            float deltaX = endPoint.X - startPoint.X;
            float deltaY = endPoint.Y - startPoint.Y;
            float angle  = (float)(180 * Math.Atan2(deltaY, deltaX) / Math.PI);

            float centerX = (endPoint.X + startPoint.X) / 2;
            float centerY = (endPoint.Y + startPoint.Y) / 2;

            graphics.RotateTransform(angle, MatrixOrder.Append);

            graphics.TranslateTransform(centerX, centerY, MatrixOrder.Append);

            SizeF size = graphics.MeasureString(text, font);

            float y = 0;

            if(textAboveSegment)
            {
                y = -size.Height;
            }

            RectangleF rectangle = new RectangleF
            (
                -size.Width / 2,
                y,
                size.Width,
                size.Height
            );

            using(StringFormat stringFormat = new StringFormat())
            {
                stringFormat.Alignment     = StringAlignment.Center;
                stringFormat.LineAlignment = StringAlignment.Center;

                graphics.DrawString(text, font, brush, rectangle, stringFormat);
            }

            graphics.Restore(state);
        }

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

댓글을 달아 주세요