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

■ StringFormat 클래스 : Trimming 속성을 사용해 문자열 잘라내는 방법(String Trimming) 설정하기

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


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

 

            ResizeRedraw = true;

 

            #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)

        {

            const string TEXT   = "The trouble ain't that there is too many fools, " +

                                  "but that the lightning ain't distributed right.";

            const int    MARGIN = 5;

 

            StringTrimming[] stringTrimmingArray = (StringTrimming[])Enum.GetValues(typeof(StringTrimming));

 

            int width  = ClientSize.Width - 2 * MARGIN;

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

 

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

            {

                using(StringFormat stringFormat = new StringFormat())

                {

                    int y = MARGIN;

 

                    foreach(StringTrimming stringTrimming in stringTrimmingArray)

                    {

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

 

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

 

                        stringFormat.Trimming = stringTrimming;

 

                        e.Graphics.DrawString

                        (

                            stringTrimming.ToString() + " : " + TEXT,

                            font,

                            Brushes.Blue,

                            rectangle,

                            stringFormat

                        );

 

                        y += height + MARGIN;

                    }

                }

            }

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요