■ Graphics 클래스의 MeasureString 메소드를 사용해 텍스트를 그리는 방법을 보여준다.
▶ 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 |
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(); #region 이벤트를 설정한다. Load += Form_Load; Resize += Form_Resize; #endregion } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 폼 로드시 처리하기 - Form_Load(sender, e) /// <summary> /// 폼 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Load(object sender, EventArgs e) { DrawText(); } #endregion #region 폼 크기 조정시 처리하기 - Form_Resize(sender, e) /// <summary> /// 폼 크기 조정시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Resize(object sender, EventArgs e) { DrawText(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 텍스트 그리기 - DrawText() /// <summary> /// 텍스트 그리기 /// </summary> private void DrawText() { string text = @"This is a fairly long piece of text that won't necessarily fit in the left PictureBox. When you change the form's height, that changes the amount of text that fits in the left PictureBox. The program draws as much text as will fit in the left PictureBox and then draws the remainder of the text in the right PictureBox."; using(Font font = new Font("나눔고딕코딩", 16)) { using(StringFormat stringFormat = new StringFormat()) { stringFormat.Trimming = StringTrimming.Word; Bitmap leftBitmap = new Bitmap ( this.leftPictureBox.ClientSize.Width, this.leftPictureBox.ClientSize.Height ); using(Graphics leftGraphics = Graphics.FromImage(leftBitmap)) { leftGraphics.Clear(this.leftPictureBox.BackColor); const int MARGIN = 5; Rectangle rectangle = new Rectangle ( MARGIN, MARGIN, this.leftPictureBox.ClientSize.Width - 2 * MARGIN, this.leftPictureBox.ClientSize.Height - 2 * MARGIN ); leftGraphics.DrawRectangle(Pens.LightGreen, rectangle); int fitCharacterCount; int fillLineCount; SizeF availableSize = new SizeF(rectangle.Width, rectangle.Height); leftGraphics.MeasureString ( text, font, availableSize, stringFormat, out fitCharacterCount, out fillLineCount ); string fitText = text.Substring(0, fitCharacterCount); leftGraphics.DrawString(fitText, font, Brushes.Black, rectangle, stringFormat); this.leftPictureBox.Image = leftBitmap; text = text.Substring(fitCharacterCount); } Bitmap rightBitmap = new Bitmap ( this.rightPictureBox.ClientSize.Width, this.rightPictureBox.ClientSize.Height ); using(Graphics rightGraphics = Graphics.FromImage(rightBitmap)) { rightGraphics.Clear(this.rightPictureBox.BackColor); const int MARGIN = 5; Rectangle rectangle = new Rectangle ( MARGIN, MARGIN, this.rightPictureBox.ClientSize.Width - 2 * MARGIN, this.rightPictureBox.ClientSize.Height - 2 * MARGIN ); rightGraphics.DrawRectangle(Pens.LightGreen, rectangle); rightGraphics.DrawString(text, font, Brushes.Black, rectangle, stringFormat); } this.rightPictureBox.Image = rightBitmap; } } } #endregion } } |