■ 커스텀 안티-알리아싱을 만드는 방법을 보여준다.
▶ 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 |
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Text; using System.Globalization; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 아쿠아 버튼 /// </summary> private AquaButtonDrawingObject aquaButton = new AquaButtonDrawingObject(); /// <summary> /// 고양이 /// </summary> private CatDrawingObject cat = new CatDrawingObject(); /// <summary> /// 그리기 객체 /// </summary> private IDrawingObject drawingObject = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); SetStyle(ControlStyles.DoubleBuffer, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.ResizeRedraw, true); this.drawingObject = this.cat; this.catMenuItem.Checked = true; } #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) { StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; Font font = new Font("Arial", 13); SolidBrush whiteBrush = new SolidBrush(Color.White); PerformanceHelper performanceHelper = new PerformanceHelper(); SolidBrush blueBrush = new SolidBrush(Color.RoyalBlue); SolidBrush cornflowerBrush = new SolidBrush(Color.CornflowerBlue); double rectangleWidth = ClientRectangle.Width / 3d; double startTime; double originalExecutionTime; double standardExecutionTime; double customExecutionTime; Rectangle originalRectangle = new Rectangle(ClientRectangle.Left , ClientRectangle.Top, (int)rectangleWidth, ClientRectangle.Height); Rectangle standardRectangle = new Rectangle((int)(ClientRectangle.Left + rectangleWidth), ClientRectangle.Top, (int)rectangleWidth, ClientRectangle.Height); Rectangle customRectangle = new Rectangle((int)(ClientRectangle.Right - rectangleWidth), ClientRectangle.Top, (int)rectangleWidth, ClientRectangle.Height); Rectangle originalDrawingRectangle = new Rectangle(originalRectangle.Left, originalRectangle.Bottom - 60, originalRectangle.Width, 60); Rectangle standardDrawingRectangle = new Rectangle(standardRectangle.Left, standardRectangle.Bottom - 60, standardRectangle.Width, 60); Rectangle customDrawingRectangle = new Rectangle(customRectangle.Left , customRectangle.Bottom - 60, customRectangle.Width , 60); Rectangle topLabelRectangle = new Rectangle(ClientRectangle.Left, ClientRectangle.Top, ClientRectangle.Width, 60); e.Graphics.FillRectangle(blueBrush , ClientRectangle ); e.Graphics.FillRectangle(cornflowerBrush, standardRectangle); originalRectangle.Inflate(new Size(-originalRectangle.Height / 15, -originalRectangle.Height / 15)); standardRectangle.Inflate(new Size(-standardRectangle.Height / 15, -standardRectangle.Height / 15)); customRectangle.Inflate (new Size(-customRectangle.Height / 15, -customRectangle.Height / 15)); e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias; e.Graphics.SmoothingMode = SmoothingMode.None; startTime = performanceHelper.ElapsedTime; drawingObject.Paint(e.Graphics, originalRectangle, 1.0f); originalExecutionTime = performanceHelper.ElapsedTime - startTime; e.Graphics.SmoothingMode = SmoothingMode.None; startTime = performanceHelper.ElapsedTime; e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; drawingObject.Paint(e.Graphics, standardRectangle); standardExecutionTime = performanceHelper.ElapsedTime - startTime; e.Graphics.SmoothingMode = SmoothingMode.None; startTime = performanceHelper.ElapsedTime; drawingObject.Paint(e.Graphics, customRectangle); customExecutionTime = performanceHelper.ElapsedTime - startTime; NumberFormatInfo numberFormatInfo = new CultureInfo("en-US", false).NumberFormat; numberFormatInfo.NumberDecimalDigits = 3; e.Graphics.DrawString("GDI+ No AntiAlias\n(" + originalExecutionTime.ToString("N", numberFormatInfo) + "s)", font, whiteBrush, originalDrawingRectangle, format); e.Graphics.DrawString("GDI+ AntiAlias\n(" + standardExecutionTime.ToString("N", numberFormatInfo) + "s)", font, whiteBrush, standardDrawingRectangle, format); e.Graphics.DrawString("Custom AntiAlias\n(" + customExecutionTime.ToString ("N", numberFormatInfo) + "s)", font, whiteBrush, customDrawingRectangle , format); RectangleF boundRectangle = drawingObject.GetBoundingRectangle(drawingObject.GetStandardTransform(standardRectangle)); e.Graphics.DrawString(((int)(boundRectangle.Width + 0.5)) + "×" + ((int)(boundRectangle.Height + 0.5)), font, whiteBrush, topLabelRectangle, format); } #endregion #region 아쿠아 버튼 메뉴 항목 클릭시 처리하기 - acuaButtonMenuItem_Click(sender, e) /// <summary> /// 아쿠아 버튼 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void acuaButtonMenuItem_Click(object sender, EventArgs e) { this.aquaButtonMenuItem.Checked = true; this.catMenuItem.Checked = false; this.drawingObject = this.aquaButton; Invalidate(); } #endregion #region 고양이 메뉴 항목 클릭시 처리하기 - catMenuItem_Click(sender, e) /// <summary> /// 고야이 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void catMenuItem_Click(object sender, EventArgs e) { this.aquaButtonMenuItem.Checked = false; this.catMenuItem.Checked = true; this.drawingObject = this.cat; Invalidate(); } #endregion } } |