■ Form 클래스에서 마우스 위치에 따라 폼 불투명도를 변경하는 방법을 보여준다.
▶ 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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; using System.Threading; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Delegate ////////////////////////////////////////////////////////////////////////////////////////// Private #region Delegate /// <summary> /// 불투명도 변경하기 대리자 /// </summary> /// <param name="value">값</param> private delegate void ChangeOpacityDelegate(double value); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 마우스 이탈시 불투명도 /// </summary> private double mouseLeaveOpacity = 0.7; /// <summary> /// FADE 비율 /// </summary> private double fadeRate = 0.001; /// <summary> /// FADE IN 스레드 /// </summary> private Thread fadeInThread; /// <summary> /// FADE OUT 스레드 /// </summary> private Thread fadeOutThread; /// <summary> /// 마지막 마우스 위치 /// </summary> private Point lastPoint; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); Load += Form_Load; Paint += Form_Paint; MouseEnter += Form_MouseEnter; MouseDown += Form_MouseDown; MouseMove += Form_MouseMove; MouseLeave += Form_MouseLeave; this.exitButton.Click += exitButton_Click; } #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) { DoubleBuffered = true; TransparencyKey = Color.Lime; Opacity = mouseLeaveOpacity; this.fadeInThread = new Thread(new ThreadStart(ProcessFadeIn )); this.fadeOutThread = new Thread(new ThreadStart(ProcessFadeOut)); } #endregion #region 폼 마우스 진입시 처리하기 - Form_MouseEnter(sender, e) /// <summary> /// 폼 마우스 진입시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_MouseEnter(object sender, EventArgs e) { if(this.fadeInThread.ThreadState == ThreadState.Running) { this.fadeInThread.Abort(); } if(this.fadeOutThread.ThreadState == ThreadState.Running) { fadeOutThread.Abort(); } this.fadeInThread = new Thread(new ThreadStart(ProcessFadeIn)); this.fadeInThread.Start(); } #endregion #region 폼 마우스 DOWN 처리하기 - Form_MouseDown(sender, e) /// <summary> /// 폼 마우스 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_MouseDown(object sender, MouseEventArgs e) { this.lastPoint = new Point(e.X, e.Y); } #endregion #region 폼 마우스 이동시 처리하기 - Form_MouseMove(sender, e) /// <summary> /// 폼 마우스 이동시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_MouseMove(object sender, MouseEventArgs e) { if(e.Button == MouseButtons.Left || e.Button == MouseButtons.Right) { Left += e.X - this.lastPoint.X; Top += e.Y - this.lastPoint.Y; } } #endregion #region 폼 마우스 이탈시 처리하기 - Form_MouseLeave(sender, e) /// <summary> /// 폼 마우스 이탈시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_MouseLeave(object sender, EventArgs e) { if(Bounds.Contains(Cursor.Position)) { return; } if(this.fadeInThread.ThreadState == ThreadState.Running) { this.fadeInThread.Abort(); } if(this.fadeOutThread.ThreadState == ThreadState.Running) { this.fadeOutThread.Abort(); } this.fadeOutThread = new Thread(new ThreadStart(ProcessFadeOut)); this.fadeOutThread.Start(); } #endregion #region 폼 페인트시 처리하기 - Form_Paint(sender, e) /// <summary> /// 폼 페인트시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Paint(object sender, PaintEventArgs e) { Graphics graphics = e.Graphics; graphics.SmoothingMode = SmoothingMode.AntiAlias; Rectangle areaRectangle = this.ClientRectangle; areaRectangle.Width -= 1; areaRectangle.Height -= 1; using(Pen outlinePen = new Pen(Color.Gold, 1)) { graphics.FillRectangle(Brushes.LightYellow, ClientRectangle); graphics.DrawRectangle(outlinePen, areaRectangle); } } #endregion #region 종료 버튼 클릭시 처리하기 - exitButton_Click(sender, e) /// <summary> /// 종료 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void exitButton_Click(object sender, EventArgs e) { Close(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 불투명도 변경하기 - ChangeOpacity(value) /// <summary> /// 불투명도 변경하기 /// </summary> /// <param name="value">값</param> private void ChangeOpacity(double value) { if(InvokeRequired) { ChangeOpacityDelegate changeOpacityDelegate = new ChangeOpacityDelegate(ChangeOpacity); this.Invoke(changeOpacityDelegate, value); } else { Opacity = value; } } #endregion #region FADE IN 처리하기 - ProcessFadeIn() /// <summary> /// FADE IN 처리하기 /// </summary> private void ProcessFadeIn() { for(double i = Opacity; i <= 1; i += this.fadeRate) { ChangeOpacity(i); } if(Opacity != 1.0) { ChangeOpacity(1.0); } } #endregion #region FADE OUT 처리하기 - ProcessFadeOut() /// <summary> /// FADE OUT 처리하기 /// </summary> private void ProcessFadeOut() { for(double i = Opacity; i >= this.mouseLeaveOpacity; i -= this.fadeRate) { ChangeOpacity(i); } if(Opacity != this.mouseLeaveOpacity) { ChangeOpacity(this.mouseLeaveOpacity); } } #endregion } } |