첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

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

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

        #region Field

        /// <summary>
        /// 소스 텍스트
        /// </summary>
        private string sourceText = "박항서 감독이 이끄는 베트남 축구대표팀이 지난 15일 10년 만에 처음으로";

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

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

        #endregion

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

        #region 픽처 박스 크기 조정하기 - pictureBox_Resize(sender, e)

        /// <summary>
        /// 픽처 박스 크기 조정하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void pictureBox_Resize(object sender, EventArgs e)
        {
            this.pictureBox.Refresh();
        }

        #endregion
        #region 픽처 박스 페인트시 처리하기 - pictureBox_Paint(sender, e)

        /// <summary>
        /// 픽처 박스 페인트시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void pictureBox_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.Clear(this.pictureBox.BackColor);

            e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

            RectangleF rectangle = new RectangleF
            (
                5,
                5,
                this.pictureBox.ClientSize.Width  - 10,
                this.pictureBox.ClientSize.Height - 10
            );

            using(Font font = new Font("나눔고딕코딩", 12))
            {
                DrawString(e.Graphics, rectangle, font, Brushes.Blue, this.sourceText);
            }

            e.Graphics.DrawRectangle(Pens.Silver, Rectangle.Round(rectangle));
        }

        #endregion

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

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

        /// <summary>
        /// 문자열 그리기
        /// </summary>
        /// <param name="graphics">그래픽스</param>
        /// <param name="rectangle">사각형</param>
        /// <param name="font">폰트</param>
        /// <param name="brush">브러시</param>
        /// <param name="text">텍스트</param>
        private void DrawString(Graphics graphics, RectangleF rectangle, Font font, Brush brush, string text)
        {
            string[] wordArray      = text.Split(' ');
            float[]  wordWidthArray = new float[wordArray.Length];
            float    totalWidth     = 0f;

            for(int i = 0; i < wordArray.Length; i++)
            {
                SizeF size = graphics.MeasureString(wordArray[i], font);

                wordWidthArray[i] = size.Width;

                totalWidth += wordWidthArray[i];
            }

            float extraSpace = rectangle.Width - totalWidth;
            int   spaceCount = wordArray.Length - 1;

            if(wordArray.Length > 1)
            {
                extraSpace /= spaceCount;
            }

            float x = rectangle.Left;
            float y = rectangle.Top;

            for(int i = 0; i < wordArray.Length; i++)
            {
                graphics.DrawString(wordArray[i], font, brush, x, y);

                x += wordWidthArray[i] + extraSpace;
            }
        }

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

댓글을 달아 주세요