■ Cursor 클래스에서 애니메이션 커서를 사용하는 방법을 보여준다.
▶ 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 |
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 커서 배열 /// </summary> private Cursor[] cursorArray; /// <summary> /// 커서 카운트 /// </summary> private const int CURSOR_COUNT = 18; /// <summary> /// 현재 커서 인덱스 /// </summary> private int currentCursorIndex = 0; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); #region 이벤트를 설정한다. Load += Form_Load; this.timer.Tick += timer_Tick; #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) { const int CURSOR_WIDTH = 32; const int CURSOR_HEIGHT = 32; float centetX = CURSOR_WIDTH / 2f; float centerY = CURSOR_HEIGHT / 2f; float radiusX = centetX * 0.9f; float radiusY = centetX * 0.4f; RectangleF rectangle = new RectangleF(-radiusX, -radiusY, 2 * radiusX, 2 * radiusY); float radius = centetX * 0.15f; Matrix transform1 = new Matrix(); transform1.Rotate(60f, MatrixOrder.Append); transform1.Translate(centetX, centerY, MatrixOrder.Append); Matrix transform2 = new Matrix(); transform2.Rotate(-60f, MatrixOrder.Append); transform2.Translate(centetX, centerY, MatrixOrder.Append); Matrix transform3 = new Matrix(); transform3.Translate(centetX, centerY, MatrixOrder.Append); Bitmap sourceBitmap = new Bitmap(CURSOR_WIDTH, CURSOR_HEIGHT); using(Graphics sourceGraphics = Graphics.FromImage(sourceBitmap)) { sourceGraphics.SmoothingMode = SmoothingMode.AntiAlias; sourceGraphics.Clear(Color.Transparent); sourceGraphics.Transform = transform1; sourceGraphics.DrawEllipse(Pens.Red, rectangle); sourceGraphics.Transform = transform2; sourceGraphics.DrawEllipse(Pens.Red, rectangle); sourceGraphics.Transform = transform3; sourceGraphics.DrawEllipse(Pens.Red, rectangle); sourceGraphics.FillEllipse(Brushes.Black, -radius, -radius, 2 * radius, 2 * radius); } this.cursorArray = new Cursor[CURSOR_COUNT]; double theta1 = 0; double dtheta1 = 2 * Math.PI / CURSOR_COUNT; double theta2 = 0; double dtheta2 = 2 * Math.PI / CURSOR_COUNT * 2; double theta3 = 0; double dtheta3 = 2 * Math.PI / CURSOR_COUNT * 3; for(int i = 0; i < CURSOR_COUNT; i++) { Bitmap cursorBitmap = new Bitmap(CURSOR_WIDTH, CURSOR_HEIGHT); using(Graphics cursorGraphics = Graphics.FromImage(cursorBitmap)) { cursorGraphics.SmoothingMode = SmoothingMode.AntiAlias; cursorGraphics.DrawImage(sourceBitmap, 0, 0); cursorGraphics.Transform = transform1; double x1 = radiusX * Math.Cos(theta1); double y1 = radiusY * Math.Sin(theta1); cursorGraphics.FillEllipse(Brushes.Red, (int)(x1 - radius), (int)(y1 - radius), 2 * radius, 2 * radius); theta1 += dtheta1; cursorGraphics.Transform = transform2; double x2 = radiusX * Math.Cos(theta2); double y2 = radiusY * Math.Sin(theta2); cursorGraphics.FillEllipse(Brushes.Green, (int)(x2 - radius), (int)(y2 - radius), 2 * radius, 2 * radius); theta2 += dtheta2; cursorGraphics.Transform = transform3; double x3 = radiusX * Math.Cos(theta3); double y3 = radiusY * Math.Sin(theta3); cursorGraphics.FillEllipse(Brushes.Blue, (int)(x3 - radius), (int)(y3 - radius), 2 * radius, 2 * radius); theta3 += dtheta3; } this.cursorArray[i] = new Cursor(cursorBitmap.GetHicon()); theta1 += dtheta1; } } #endregion #region 타이머 틱 처리하기 - timer_Tick(sender, e) /// <summary> /// 타이머 틱 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void timer_Tick(object sender, EventArgs e) { this.currentCursorIndex = (this.currentCursorIndex + 1) % CURSOR_COUNT; Cursor = this.cursorArray[this.currentCursorIndex]; } #endregion } } |