■ 컬럼 크기에 맞춰 텍스트 그리기

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


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[][] valueArray =

        {

            new string[]

            {

                "WPF 3d, Three-Dimensional Graphics with WPF and C#",

                "http://www.csharphelper.com/wpf3d.html",

                "426 pages",

                "2/2018",

                "978-1983905964"

            },

            new string[]

            {

                "The C# Helper Top 100",

                "http://www.csharphelper.com/top100.htm",

                "382 pages",

                "9/2017",

                "978-1546886716"

            },

            new string[]

            {

                "Interview Puzzles Dissected, Solving and Understanding Interview Puzzles",

                "http://www.csharphelper.com/puzzles.htm",

                "300 pages",

                "11/2016",

                "978-1539504887"

            },

            new string[]

            {

                "C# 24-Hour Trainer, 2nd Edition",

                "http://www.wrox.com/WileyCDA/WroxTitle/C-24-Hour-Trainer-2nd-Edition.productCd-1119065666.html",

                "600 pages",

                "11/2015",

                "978-1119065661"

            },

            new string[]

            {

                "Beginning Software Engineering",

                "http://www.wrox.com/WileyCDA/WroxTitle/Beginning-Software-Engineering.productCd-1118969146.html",

                "480 pages",

                "3/2015",

                "978-1118969144"

            },

            new string[]

            {

                "C# 5.0 Programmer's Reference",

                "http://www.wrox.com/WileyCDA/WroxTitle/C-5-0-Programmer-s-Reference.productCd-1118847288.html",

                "960 pages",

                "4/2014",

                "978-1118847282"

            },

            new string[]

            {

                "Essential Algorithms : A Practical Approach to Computer Algorithms",

                "http://www.csharphelper.com/algorithms.html",

                "624 pages",

                "8/2013",

                "978-1118612101"

            },

        };

 

        /// <summary>

        /// 컬럼 너비 배열

        /// </summary>

        private int[] columnWidthArray = { 230, 230, 90, 70, 130};

 

        /// <summary>

        /// 수직 정렬 배열

        /// </summary>

        private StringAlignment[] verticalAlignmentArray =

        {

            StringAlignment.Center,

            StringAlignment.Center,

            StringAlignment.Near,

            StringAlignment.Near,

            StringAlignment.Near,

        };

 

        /// <summary>

        /// 수평 정렬 배열

        /// </summary>

        private StringAlignment[] horizontalAlignmentArray =

        {

            StringAlignment.Near,

            StringAlignment.Near,

            StringAlignment.Far,

            StringAlignment.Center,

            StringAlignment.Near,

        };

 

        #endregion

 

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            this.canvasPictureBox.Paint += canvasPictureBox_Paint;

 

            #endregion

        }

 

        #endregion

 

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

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

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 캔버스 픽처 박스 페인트시 처리하기 - canvasPictureBox_Paint(sender, e)

 

        /// <summary>

        /// 캔버스 픽처 박스 페인트시 처리하기

        /// </summary>

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

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

        private void canvasPictureBox_Paint(object sender, PaintEventArgs e)

        {

            e.Graphics.Clear(Color.White);

 

            e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;

 

            using(StringFormat stringFormat = new StringFormat())

            {

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

                {

                    const int MARGIN = 4;

 

                    int y = MARGIN;

 

                    foreach(string[] rowArray in this.valueArray)

                    {

                        int columnCount = rowArray.Length;

 

                        int maximumHeight = 0;

 

                        for(int column = 0; column < columnCount; column++)

                        {

                            SizeF columnSize = e.Graphics.MeasureString

                            (

                                rowArray[column],

                                font,

                                this.columnWidthArray[column] - 2 * MARGIN

                            );

 

                            int newHeight = (int)Math.Ceiling(columnSize.Height);

 

                            if(maximumHeight < newHeight)

                            {

                                maximumHeight = newHeight;

                            }

                        }

 

                        maximumHeight += 2 * MARGIN;

 

                        int x = MARGIN;

 

                        for(int column = 0; column < columnCount; column++)

                        {

                            Rectangle boxRectangle = new Rectangle

                            (

                                x,

                                y,

                                this.columnWidthArray[column],

                                maximumHeight

                            );

 

                            Rectangle textRectangle = boxRectangle;

 

                            textRectangle.Inflate(-MARGIN, -MARGIN);

 

                            stringFormat.Alignment     = this.horizontalAlignmentArray[column];

                            stringFormat.LineAlignment = this.verticalAlignmentArray[column];

 

                            e.Graphics.DrawString

                            (

                                rowArray[column],

                                font,

                                Brushes.Black,

                                textRectangle,

                                stringFormat

                            );

 

                            e.Graphics.DrawRectangle(Pens.Blue, boxRectangle);

 

                            x += this.columnWidthArray[column];

                        }

 

                        y += maximumHeight;

                    }

                }

            }

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요