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

TestProject.zip
다운로드

▶ MainForm.cs

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

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

        #region 생성자 - MainForm()

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

            #region 이벤트를 설정한다.

            Load   += Form_Load;
            Resize += Form_Resize;

            #endregion
        }

        #endregion

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

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

        /// <summary>
        /// 폼 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_Load(object sender, EventArgs e)
        {
            DrawText();
        }

        #endregion
        #region 폼 크기 조정시 처리하기 - Form_Resize(sender, e)

        /// <summary>
        /// 폼 크기 조정시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_Resize(object sender, EventArgs e)
        {
            DrawText();
        }

        #endregion

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

        #region 텍스트 그리기 - DrawText()

        /// <summary>
        /// 텍스트 그리기
        /// </summary>
        private void DrawText()
        {
            string text = @"This is a fairly long piece of text that won't necessarily fit in the left PictureBox.
When you change the form's height, that changes the amount of text that fits in the left PictureBox.
The program draws as much text as will fit in the left PictureBox and then draws the remainder of the text in the right PictureBox.";

            using(Font font = new Font("나눔고딕코딩", 16))
            {
                using(StringFormat stringFormat = new StringFormat())
                {
                    stringFormat.Trimming = StringTrimming.Word;

                    Bitmap leftBitmap = new Bitmap
                    (
                        this.leftPictureBox.ClientSize.Width,
                        this.leftPictureBox.ClientSize.Height
                    );

                    using(Graphics leftGraphics = Graphics.FromImage(leftBitmap))
                    {
                        leftGraphics.Clear(this.leftPictureBox.BackColor);

                        const int MARGIN = 5;

                        Rectangle rectangle = new Rectangle
                        (
                            MARGIN,
                            MARGIN,
                            this.leftPictureBox.ClientSize.Width  - 2 * MARGIN,
                            this.leftPictureBox.ClientSize.Height - 2 * MARGIN
                        );

                        leftGraphics.DrawRectangle(Pens.LightGreen, rectangle);

                        int fitCharacterCount;
                        int fillLineCount;

                        SizeF availableSize = new SizeF(rectangle.Width, rectangle.Height);

                        leftGraphics.MeasureString
                        (
                            text,
                            font,
                            availableSize,
                            stringFormat,
                            out fitCharacterCount,
                            out fillLineCount
                        );

                        string fitText = text.Substring(0, fitCharacterCount);

                        leftGraphics.DrawString(fitText, font, Brushes.Black, rectangle, stringFormat);

                        this.leftPictureBox.Image = leftBitmap;

                        text = text.Substring(fitCharacterCount);
                    }

                    Bitmap rightBitmap = new Bitmap
                    (
                        this.rightPictureBox.ClientSize.Width,
                        this.rightPictureBox.ClientSize.Height
                    );

                    using(Graphics rightGraphics = Graphics.FromImage(rightBitmap))
                    {
                        rightGraphics.Clear(this.rightPictureBox.BackColor);

                        const int MARGIN = 5;

                        Rectangle rectangle = new Rectangle
                        (
                            MARGIN,
                            MARGIN,
                            this.rightPictureBox.ClientSize.Width  - 2 * MARGIN,
                            this.rightPictureBox.ClientSize.Height - 2 * MARGIN
                        );

                        rightGraphics.DrawRectangle(Pens.LightGreen, rectangle);

                        rightGraphics.DrawString(text, font, Brushes.Black, rectangle, stringFormat);
                    }

                    this.rightPictureBox.Image = rightBitmap;
                }
            }
        }

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

댓글을 달아 주세요