첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ StringFormat 클래스 : FormatFlags 속성을 사용해 문자열 라인 클리핑 설정하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainForm.cs

 

 

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();

 

            ResizeRedraw = true;

 

            #region 이벤트를 설정한다.

 

            Paint += Form_Paint;

 

            #endregion

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 폼 페인트시 처리하기 - Form_Paint(sender, e)

 

        /// <summary>

        /// 폼 페인트시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void Form_Paint(object sender, PaintEventArgs e)

        {

            const string TEXT   = "The trouble ain't that there is too many fools, but that the lightning ain't distributed right.";

            const int    MARGIN = 20;

 

            StringFormatFlags[] stringFormatFlagArray =

            {

                StringFormatFlags.FitBlackBox,

                StringFormatFlags.LineLimit,

                StringFormatFlags.NoClip,

                StringFormatFlags.NoWrap

            };

 

            int width  = ClientSize.Width - 2 * MARGIN;

            int height = (ClientSize.Height - (stringFormatFlagArray.Length + 1) * MARGIN) / stringFormatFlagArray.Length;

 

            using(Font font = new Font("Times New Roman", 20))

            {

                using(StringFormat stringFormat = new StringFormat())

                {

                    int y = MARGIN;

 

                    foreach(StringFormatFlags flag in stringFormatFlagArray)

                    {

                        Rectangle rectangle = new Rectangle(MARGIN, y, width, height);

 

                        e.Graphics.DrawRectangle(Pens.Black, rectangle);

 

                        stringFormat.FormatFlags = flag;

 

                        e.Graphics.DrawString(flag.ToString() + " : " + TEXT, font, Brushes.Blue, rectangle, stringFormat);

 

                        y += height + MARGIN;

                    }

                }

            }

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요