첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

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
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요