■ 컬럼 크기에 맞춰 텍스트를 그리는 방법을 보여준다.
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
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 } } |