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

■ StringFormat 클래스 : Alignment 속성을 사용해 문자열 정렬하기

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


TestProject.zip


MainForm.cs

 

 

using System.Drawing;

using System.Drawing.Text;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

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

        {

            e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;

 

            string headerList = "Title\tPrice\t# Pages\tYear";

 

            string[] lineArray =

            {

                "WPF 3d\t$34.95\t430\t2018",

                "The C# Helper Top 100\t$24.95\t380\t2017",

                "Interview Puzzles Dissected\t$15.95\t300\t2016",

                "C# 24-Hour Trainer, Second Edition\t$45.00\t600\t2015",

                "Beginning Software Engineering\t$45.00\t480\t2015",

                "Essential Algorithms\t$60.00\t624\t2013",

                "Beginning Database Design Solutions\t$44.99\t552\t2008",

                "Powers of Two\t$2.04\t8\t16",

            };

 

            using(StringFormat stringFormat = new StringFormat())

            {

                float[] positionXArray = { 20, 410, 500, 565 };

 

                StringAlignment[] stringAlignmentArray =

                {

                    StringAlignment.Near,

                    StringAlignment.Far,

                    StringAlignment.Far,

                    StringAlignment.Far,

                };

 

                float margin = 10;

                float y      = 10;

 

                using(Font font = new Font("Times New Roman", 15, FontStyle.Bold))

                {

                    string[] headerArray = headerList.Split('\t');

 

                    for(int i = 0; i < headerArray.Length; i++)

                    {

                        stringFormat.Alignment = stringAlignmentArray[i];

 

                        e.Graphics.DrawString(headerArray[i], font, Brushes.Blue, positionXArray[i], y, stringFormat);

                    }

                }

 

                y += 1.4f * Font.Height;

 

                float width = positionXArray[positionXArray.Length - 1] + 5;

 

                e.Graphics.DrawLine(Pens.Blue, margin, y, width, y);

 

                y += 5;

 

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

                {

                    foreach(string line in lineArray)

                    {

                        string[] stringArray = line.Split('\t');

 

                        for(int i = 0; i < stringArray.Length; i++)

                        {

                            stringFormat.Alignment = stringAlignmentArray[i];

 

                            e.Graphics.DrawString(stringArray[i], font, Brushes.Black, positionXArray[i], y, stringFormat);

                        }

 

                        y += 1.2f * this.Font.Height;

                    }

                }

            }

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요