첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
0.02MB

▶ CaptureChangedDelegate.cs

namespace TestProject
{
    /// <summary>
    /// 캡처 변경시 대리자
    /// </summary>
    public delegate void CaptureChangedDelegate();
}

 

728x90

 

▶ CaptureChangedWindow.cs

using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 캡처 변경시 윈도우
    /// </summary>
    public class CaptureChangedWindow : NativeWindow
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// 캡처 변경시 대리자
        /// </summary>
        public CaptureChangedDelegate CaptureChanged;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Protected

        #region 윈도우 프로시저 처리하기 - WndProc(message)

        /// <summary>
        /// 윈도우 프로시저 처리하기
        /// </summary>
        /// <param name="message">메시지</param>
        protected override void WndProc(ref Message message)
        {
            if(message.Msg == 533) // WM_CAPTURECHANGED
            {
                CaptureChanged();
            }

            base.WndProc(ref message);
        }

        #endregion
    }
}

 

반응형

 

▶ Style3D.cs

namespace TestProject
{
    /// <summary>
    /// 3D 스타일
    /// </summary>
    public enum Style3D
    {
        /// <summary>
        /// INSET
        /// </summary>
        Inset,

        /// <summary>
        /// RAISEd
        /// </summary>
        Raised,

        /// <summary>
        /// RIDGE
        /// </summary>
        Ridge,

        /// <summary>
        /// GROOVE
        /// </summary>
        Groove
    }
}

 

300x250

 

▶ InvalidArgumentException.cs

using System;

namespace TestProject
{
    /// <summary>
    /// 무효 인자 예외
    /// </summary>
    public class InvalidArgumentException : ArgumentException
    {
    }
}

 

▶ GraphicsHelper.cs

using System.Drawing;
using System.Drawing.Drawing2D;

namespace TestProject
{
    /// <summary>
    /// 그래픽스 헬퍼
    /// </summary>
    public class GraphicsHelper
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 그래픽스
        /// </summary>
        private Graphics graphics;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - GraphicsHelper(graphics)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="graphics">그래픽스</param>
        public GraphicsHelper(Graphics graphics)
        {
            this.graphics = graphics;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 선 그리기 - DrawLine(pen, startPoint, endPoint)

        /// <summary>
        /// 선 그리기
        /// </summary>
        /// <param name="pen">펜</param>
        /// <param name="startPoint">시작 포인트</param>
        /// <param name="endPoint">종료 포인트</param>
        public void DrawLine(Pen pen, Point startPoint, Point endPoint)
        {
            Point point1 = new Point(startPoint.X, startPoint.Y);
            Point point2 = new Point(endPoint.X  , endPoint.Y  );

            if(point2.X < point1.X)
            {
                Point temporaryPoint = point2;

                point2 = point1;

                point1 = temporaryPoint;
            }

            if(point1.Y == point2.Y)
            {
                point2.X--;
            } 
            else if(point1.X == point2.X)
            {
                point2.Y--;
            }
            else if(point2.Y > point1.Y)
            {
                point2.X--;
                point2.Y--;
            }
            else if(point2.Y < point1.Y)
            {
                point1.Y--;
                point2.X--;
            }

            graphics.DrawLine(pen, point1, point2);
        }

        #endregion
        #region 선 그리기 - DrawLine(pen, startX, startY, endX, endY)

        /// <summary>
        /// 선 그리기
        /// </summary>
        /// <param name="pen">펜</param>
        /// <param name="startX">시작 X</param>
        /// <param name="startY">시작 Y</param>
        /// <param name="endX">종료 X</param>
        /// <param name="endY">종료 Y</param>
        public void DrawLine(Pen pen, int startX, int startY, int endX, int endY)
        {
            DrawLine(pen, new Point(startX, startY), new Point(endX, endY));
        }

        #endregion

        #region 사각형 그리기 - DrawRectangle(pen, rectangle)

        /// <summary>
        /// 사각형 그리기
        /// </summary>
        /// <param name="pen">펜</param>
        /// <param name="rectangle">사각형</param>
        public void DrawRectangle(Pen pen, Rectangle rectangle)
        {
            if(pen.Width == 1) 
            {
                Rectangle rectangle2 = rectangle;

                rectangle2.Width--;
                rectangle2.Height--;

                graphics.DrawRectangle(pen, rectangle2);
            } 
            else 
            {
                PenAlignment penAlignment = pen.Alignment;

                pen.Alignment = PenAlignment.Inset;

                graphics.DrawRectangle(pen, rectangle);

                pen.Alignment = penAlignment;
            }
        }

        #endregion
        #region 사각형 그리기 - DrawRectangle(pen, x, y, width, height)

        /// <summary>
        /// 사각형 그리기
        /// </summary>
        /// <param name="pen">펜</param>
        /// <param name="x">X</param>
        /// <param name="y">Y</param>
        /// <param name="width">너비</param>
        /// <param name="height">높이</param>
        public void DrawRectangle(Pen pen, int x, int y, int width, int height)
        {
            Rectangle rectangle = new Rectangle(x, y, width, height);

            DrawRectangle(pen, rectangle);
        }

        #endregion

        #region 사각형 채우기 - FillRectangle(brush, rectangle)

        /// <summary>
        /// 사각형 채우기
        /// </summary>
        /// <param name="brush">브러시</param>
        /// <param name="rectangle">사각형</param>
        public void FillRectangle(Brush brush, Rectangle rectangle)
        {
            graphics.FillRectangle(brush, rectangle);
        }

        #endregion

        #region 3D 선 그리기 - Draw3DLine(startPoint, endPoint, style, depth)

        /// <summary>
        /// 3D 선 그리기
        /// </summary>
        /// <param name="startPoint">시작 포인트</param>
        /// <param name="endPoint">종료 포인트</param>
        /// <param name="style">스타일</param>
        /// <param name="depth">깊이</param>
        public void Draw3DLine(Point startPoint, Point endPoint, Style3D style, int depth)
        {
            if(startPoint.X != endPoint.X && startPoint.Y != endPoint.Y)
            {
                throw new InvalidArgumentException();
            }

            if(depth != 1 && depth != 2)
            {
                throw new InvalidArgumentException();
            }

            if(depth == 1) 
            {
                switch(style)
                {
                    case Style3D.Inset  :
                    case Style3D.Groove :

                        if(startPoint.Y == endPoint.Y)
                        {
                            DrawLine(SystemPens.ControlDark, startPoint, endPoint);

                            Point point1 = new Point(startPoint.X, startPoint.Y + 1);
                            Point point2 = new Point(endPoint.X  , endPoint.Y   + 1);

                            DrawLine(SystemPens.ControlLightLight, point1, point2);
                        } 
                        else 
                        {
                            DrawLine(SystemPens.ControlDark, startPoint, endPoint);

                            Point point1 = new Point(startPoint.X + 1, startPoint.Y);
                            Point point2 = new Point(endPoint.X + 1  , endPoint.Y  );

                            DrawLine(SystemPens.ControlLightLight, point1, point2);
                        }

                        break;

                    case Style3D.Raised :
                    case Style3D.Ridge  :

                        if(startPoint.Y == endPoint.Y)
                        {
                            DrawLine(SystemPens.ControlLightLight, startPoint, endPoint);

                            Point point1 = new Point(startPoint.X, startPoint.Y + 1);
                            Point point2 = new Point(endPoint.X  , endPoint.Y   + 1);

                            DrawLine(SystemPens.ControlDark, point1, point2);
                        }
                        else
                        {
                            DrawLine(SystemPens.ControlLightLight, startPoint, endPoint);

                            Point point1 = new Point(startPoint.X + 1, startPoint.Y);
                            Point point2 = new Point(endPoint.X   + 1, endPoint.Y  );

                            DrawLine(SystemPens.ControlDark, point1, point2);
                        }

                        break;
                }
            }
            else if(depth == 2)
            {
                switch(style)
                {
                    case Style3D.Inset  :
                    case Style3D.Groove :

                        if(startPoint.Y == endPoint.Y)
                        {
                            DrawLine(SystemPens.ControlDarkDark, startPoint, endPoint);

                            Point point1 = new Point(startPoint.X, startPoint.Y + 1);
                            Point point2 = new Point(endPoint.X  , endPoint.Y   + 1);

                            DrawLine(SystemPens.ControlDark, point1, point2);

                            point1.Y++;
                            point2.Y++;

                            DrawLine(SystemPens.ControlLight, point1, point2);

                            point1.Y++;
                            point2.Y++;

                            DrawLine(SystemPens.ControlLightLight, point1, point2);
                        } 
                        else 
                        {
                            DrawLine(SystemPens.ControlDarkDark, startPoint, endPoint);

                            Point point1 = new Point(startPoint.X + 1, startPoint.Y);
                            Point point2 = new Point(endPoint.X   + 1, endPoint.Y  );

                            DrawLine(SystemPens.ControlDark, point1, point2);

                            point1.X++;
                            point2.X++;

                            DrawLine(SystemPens.ControlLight, point1, point2);

                            point1.X++;
                            point2.X++;

                            DrawLine(SystemPens.ControlLightLight, point1, point2);
                        }

                        break;

                    case Style3D.Raised :
                    case Style3D.Ridge  :

                        if(startPoint.Y == endPoint.Y)
                        {
                            DrawLine(SystemPens.ControlLightLight, startPoint, endPoint);

                            Point point1 = new Point(startPoint.X, startPoint.Y + 1);
                            Point point2 = new Point(endPoint.X  , endPoint.Y   + 1);

                            DrawLine(SystemPens.ControlLight, point1, point2);

                            point1.Y++;
                            point2.Y++;

                            DrawLine(SystemPens.ControlDark, point1, point2);

                            point1.Y++;
                            point2.Y++;

                            DrawLine(SystemPens.ControlDarkDark, point1, point2);
                        }
                        else
                        {
                            DrawLine(SystemPens.ControlLightLight, startPoint, endPoint);

                            Point point1 = new Point(startPoint.X + 1, startPoint.Y);
                            Point point2 = new Point(endPoint.X   + 1, endPoint.Y  );

                            DrawLine(SystemPens.ControlLight, point1, point2);

                            point1.X++;
                            point2.X++;

                            DrawLine(SystemPens.ControlDark, point1, point2);

                            point1.X++;
                            point2.X++;

                            DrawLine(SystemPens.ControlDarkDark, point1, point2);
                        }

                        break;
                }
            }
        }

        #endregion
        #region 3D 선 그리기 - Draw3DLine(startX, startY, endX, endY, style, depth)

        /// <summary>
        /// 3D 선 그리기
        /// </summary>
        /// <param name="startX">시작 X</param>
        /// <param name="startY">시작 Y</param>
        /// <param name="endX">종료 X</param>
        /// <param name="endY">종료 Y</param>
        /// <param name="style">스타일</param>
        /// <param name="depth">깊이</param>
        public void Draw3DLine(int startX, int startY, int endX, int endY, Style3D style, int depth)
        {
            Draw3DLine(new Point(startX, startY), new Point(endX, endY), style, depth);
        }

        #endregion

        #region 3D 사각형 그리기 - Draw3DRectangle(rectangle, style, depth)

        /// <summary>
        /// 3D 사각형 그리기
        /// </summary>
        /// <param name="rectangle">사각형</param>
        /// <param name="style">스타일</param>
        /// <param name="depth">깊이</param>
        public void Draw3DRectangle(Rectangle rectangle, Style3D style, int depth)
        {
            if(depth != 1 && depth != 2)
            {
                throw new InvalidArgumentException();
            }

            if(depth == 1) 
            {
                switch(style)
                {
                    case Style3D.Ridge :

                        DrawDepth1RidgeGrooveRectangle(rectangle, SystemPens.ControlLightLight, SystemPens.ControlDark);

                        break;

                    case Style3D.Groove :

                        DrawDepth1RidgeGrooveRectangle(rectangle, SystemPens.ControlDark, SystemPens.ControlLightLight);

                        break;

                    case Style3D.Raised :

                        DrawDepth1InsetRaisedRectangle(rectangle, SystemPens.ControlLightLight, SystemPens.ControlDark);

                        Rectangle rectangle2 = new Rectangle(rectangle.Location, rectangle.Size);

                        rectangle2.Inflate(-1, -1);

                        FillRectangle(SystemBrushes.Control, rectangle2);

                        break;

                    case Style3D.Inset :

                        DrawDepth1InsetRaisedRectangle(rectangle, SystemPens.ControlDark, SystemPens.ControlLightLight);

                        Rectangle rectangle3 = new Rectangle(rectangle.Location, rectangle.Size);

                        rectangle3.Inflate(-1, -1);

                        FillRectangle(SystemBrushes.Control, rectangle3);

                        break;
                }
            }
            else if(depth == 2)
            {
                switch(style)
                {
                    case Style3D.Ridge :

                        DrawDepth2RidgeGrooveRectangle
                        (
                            rectangle,
                            SystemPens.ControlLightLight,
                            SystemPens.ControlLight,
                            SystemPens.ControlDark,
                            SystemPens.ControlDarkDark
                        );

                        break;

                    case Style3D.Groove :

                        DrawDepth2RidgeGrooveRectangle
                        (
                            rectangle,
                            SystemPens.ControlDarkDark,
                            SystemPens.ControlDark,
                            SystemPens.ControlLight,
                            SystemPens.ControlLightLight
                        );

                        break;

                    case Style3D.Raised :

                        DrawDepth2InsetRaisedRectangle
                        (
                            rectangle,
                            SystemPens.ControlLightLight,
                            SystemPens.ControlLight,
                            SystemPens.ControlDark,
                            SystemPens.ControlDarkDark
                        );

                        Rectangle rectangle2 = new Rectangle(rectangle.Location, rectangle.Size);

                        rectangle2.Inflate(-2, -2);

                        FillRectangle(SystemBrushes.Control, rectangle2);

                        break;

                    case Style3D.Inset :

                        DrawDepth2InsetRaisedRectangle
                        (
                            rectangle,
                            SystemPens.ControlDarkDark,
                            SystemPens.ControlDark,
                            SystemPens.ControlLight,
                            SystemPens.ControlLightLight
                        );

                        Rectangle rectangle3 = new Rectangle(rectangle.Location, rectangle.Size);

                        rectangle3.Inflate(-2, -2);

                        FillRectangle(SystemBrushes.Control, rectangle3);

                        break;
                }
            }
        }

        #endregion
        #region 3D 사각형 그리기 - Draw3DRectangle(x, y, width, height, style, depth)

        /// <summary>
        /// 3D 사각형 그리기
        /// </summary>
        /// <param name="x">X</param>
        /// <param name="y">Y</param>
        /// <param name="width">너비</param>
        /// <param name="height">높이</param>
        /// <param name="style">스타일</param>
        /// <param name="depth">깊이</param>
        public void Draw3DRectangle(int x, int y, int width, int height, Style3D style, int depth)
        {
            Draw3DRectangle(new Rectangle(x, y, width, height), style, depth);
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 깊이 1 INSET/RAISED 사각형 그리기 - DrawDepth1InsetRaisedRectangle(rectangle, pen1, pen2)

        /// <summary>
        /// 깊이 1 INSET/RAISED 사각형 그리기
        /// </summary>
        /// <param name="rectangle">사각형</param>
        /// <param name="pen1">펜 1</param>
        /// <param name="pen2">펜 2</param>
        private void DrawDepth1InsetRaisedRectangle(Rectangle rectangle, Pen pen1, Pen pen2)
        {
            Point point1 = new Point(rectangle.Left                  , rectangle.Top);
            Point point2 = new Point(rectangle.Left + rectangle.Width, rectangle.Top);

            DrawLine(pen1, point1, point2);

            point1 = new Point(rectangle.Left, rectangle.Top                   );
            point2 = new Point(rectangle.Left, rectangle.Top + rectangle.Height);

            DrawLine(pen1, point1, point2);

            point1 = new Point(rectangle.Left                  , rectangle.Top + rectangle.Height - 1);
            point2 = new Point(rectangle.Left + rectangle.Width, rectangle.Top + rectangle.Height - 1);

            DrawLine(pen2, point1, point2);

            point1 = new Point(rectangle.Left + rectangle.Width - 1, rectangle.Top                   );
            point2 = new Point(rectangle.Left + rectangle.Width - 1, rectangle.Top + rectangle.Height);

            DrawLine(pen2, point1, point2);
        }

        #endregion
        #region 깊이 1 RIDGE/GROOVE 사각형 그리기 - DrawDepth1RidgeGrooveRectangle(rectangle, pen1, pen2)

        /// <summary>
        /// 깊이 1 RIDGE/GROOVE 사각형 그리기
        /// </summary>
        /// <param name="rectangle">사각형</param>
        /// <param name="pen1">펜 1</param>
        /// <param name="pen2">펜 2</param>
        private void DrawDepth1RidgeGrooveRectangle(Rectangle rectangle, Pen pen1, Pen pen2)
        {
            Point point1 = new Point(rectangle.Left                  , rectangle.Top);
            Point point2 = new Point(rectangle.Left + rectangle.Width, rectangle.Top);

            DrawLine(pen1, point1, point2);

            point1.X++;
            point2.X--;

            point1.Y++;
            point2.Y++;

            DrawLine(pen2, point1, point2);

            point1 = new Point(rectangle.Left, rectangle.Top                   );
            point2 = new Point(rectangle.Left, rectangle.Top + rectangle.Height);

            DrawLine(pen1, point1, point2);

            point1.X++;
            point2.X++;

            point1.Y++;
            point2.Y--;

            DrawLine(pen2, point1, point2);

            point1 = new Point(rectangle.Left                  , rectangle.Top + rectangle.Height - 1);
            point2 = new Point(rectangle.Left + rectangle.Width, rectangle.Top + rectangle.Height - 1);

            DrawLine(pen2, point1, point2);

            point1.X++;
            point2.X--;

            point1.Y--;
            point2.Y--;

            DrawLine(pen1, point1, point2);

            point1 = new Point(rectangle.Left + rectangle.Width - 1, rectangle.Top                   );
            point2 = new Point(rectangle.Left + rectangle.Width - 1, rectangle.Top + rectangle.Height);

            DrawLine(pen2, point1, point2);

            point1.X--;
            point2.X--;

            point1.Y++;
            point2.Y--;

            DrawLine(pen1, point1, point2);
        }

        #endregion

        #region 깊이 2 INSERT/RAISED 사각형 그리기 - DrawDepth2InsetRaisedRectangle(rectangle, pen1, pen2, pen3, pen4)

        /// <summary>
        /// 깊이 2 INSERT/RAISED 사각형 그리기
        /// </summary>
        /// <param name="rectangle">사각형</param>
        /// <param name="pen1">펜 1</param>
        /// <param name="pen2">펜 2</param>
        /// <param name="pen3">펜 3</param>
        /// <param name="pen4">펜 4</param>
        private void DrawDepth2InsetRaisedRectangle(Rectangle rectangle, Pen pen1, Pen pen2, Pen pen3, Pen pen4)
        {
            Point point1 = new Point(rectangle.Left                  , rectangle.Top);
            Point point2 = new Point(rectangle.Left + rectangle.Width, rectangle.Top);

            DrawLine(pen1, point1, point2);

            point1.X++;
            point2.X--;

            point1.Y++;
            point2.Y++;

            DrawLine(pen2, point1, point2);

            point1 = new Point(rectangle.Left, rectangle.Top                   );
            point2 = new Point(rectangle.Left, rectangle.Top + rectangle.Height);

            DrawLine(pen1, point1, point2);

            point1.X++;
            point2.X++;

            point1.Y++;
            point2.Y--;

            DrawLine(pen2, point1, point2);

            point1 = new Point(rectangle.Left                  , rectangle.Top + rectangle.Height - 1);
            point2 = new Point(rectangle.Left + rectangle.Width, rectangle.Top + rectangle.Height - 1);

            DrawLine(pen4, point1, point2);

            point1.X++;
            point2.X--;

            point1.Y--;
            point2.Y--;

            DrawLine(pen3, point1, point2);

            point1 = new Point(rectangle.Left + rectangle.Width - 1, rectangle.Top                   );
            point2 = new Point(rectangle.Left + rectangle.Width - 1, rectangle.Top + rectangle.Height);

            DrawLine(pen4, point1, point2);

            point1.X--;
            point2.X--;

            point1.Y++;
            point2.Y--;

            DrawLine(pen3, point1, point2);
        }

        #endregion
        #region 깊이 2 RIDGE/GROOVE 사각형 그리기 - DrawDepth2RidgeGrooveRectangle(rectangle, pen1, pen2, pen3, pen4)

        /// <summary>
        /// 깊이 2 RIDGE/GROOVE 사각형 그리기
        /// </summary>
        /// <param name="rectangle">사각형</param>
        /// <param name="pen1">펜 1</param>
        /// <param name="pen2">펜 2</param>
        /// <param name="pen3">펜 3</param>
        /// <param name="pen4">펜 4</param>
        private void DrawDepth2RidgeGrooveRectangle(Rectangle rectangle, Pen pen1, Pen pen2, Pen pen3, Pen pen4)
        {
            Point point1 = new Point(rectangle.Left                  , rectangle.Top);
            Point point2 = new Point(rectangle.Left + rectangle.Width, rectangle.Top);

            DrawLine(pen1, point1, point2);

            point1.X++;
            point2.X--;

            point1.Y++;
            point2.Y++;

            DrawLine(pen2, point1, point2);

            point1.X++;
            point2.X--;

            point1.Y++;
            point2.Y++;

            DrawLine(pen3, point1, point2);

            point1.X++;
            point2.X--;

            point1.Y++;
            point2.Y++;

            DrawLine(pen4, point1, point2);

            point1 = new Point(rectangle.Left, rectangle.Top                   );
            point2 = new Point(rectangle.Left, rectangle.Top + rectangle.Height);

            DrawLine(pen1, point1, point2);

            point1.X++;
            point2.X++;

            point1.Y++;
            point2.Y--;

            DrawLine(pen2, point1, point2);

            point1.X++;
            point2.X++;

            point1.Y++;
            point2.Y--;

            DrawLine(pen3, point1, point2);

            point1.X++;
            point2.X++;

            point1.Y++;
            point2.Y--;

            DrawLine(pen4, point1, point2);

            point1 = new Point(rectangle.Left                  , rectangle.Top + rectangle.Height - 1);
            point2 = new Point(rectangle.Left + rectangle.Width, rectangle.Top + rectangle.Height - 1);

            DrawLine(pen4, point1, point2);

            point1.X++;
            point2.X--;

            point1.Y--;
            point2.Y--;

            DrawLine(pen3, point1, point2);

            point1.X++;
            point2.X--;

            point1.Y--;
            point2.Y--;

            DrawLine(pen2, point1, point2);

            point1.X++;
            point2.X--;

            point1.Y--;
            point2.Y--;

            DrawLine(pen1, point1, point2);

            point1 = new Point(rectangle.Left + rectangle.Width - 1, rectangle.Top                   );
            point2 = new Point(rectangle.Left + rectangle.Width - 1, rectangle.Top + rectangle.Height);

            DrawLine(pen4, point1, point2);

            point1.X--;
            point2.X--;

            point1.Y++;
            point2.Y--;

            DrawLine(pen3, point1, point2);

            point1.X--;
            point2.X--;

            point1.Y++;
            point2.Y--;

            DrawLine(pen2, point1, point2);

            point1.X--;
            point2.X--;

            point1.Y++;
            point2.Y--;

            DrawLine(pen1, point1, point2);
        }

        #endregion
    }
}

 

▶ MainForm.cs

using System.Drawing;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 캡처 변경시 윈도우
        /// </summary>
        private CaptureChangedWindow captureChangedWindow;

        /// <summary>
        /// 버튼 사각형
        /// </summary>
        private Rectangle buttonRectangle = new Rectangle(400, 100, 100, 40);

        /// <summary>
        /// 버튼 내에서 마우스 DOWN 여부
        /// </summary>
        private bool mouseDownInButton;

        /// <summary>
        /// 버튼 DOWN 여부
        /// </summary>
        private bool buttonDown;

        /// <summary>
        /// 적색 사각형
        /// </summary>
        private Rectangle redRectangle = new Rectangle(400, 200, 100, 40);

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            this.captureChangedWindow = new CaptureChangedWindow();

            this.captureChangedWindow.AssignHandle(Handle);

            MouseDown                                += Form_MouseDown;
            MouseMove                                += Form_MouseMove;
            MouseUp                                  += Form_MouseUp;
            Paint                                    += Form_Paint;
            this.captureChangedWindow.CaptureChanged += captureChangedWindow_CaptureChanged;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #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)
        {
            if(e.Button == MouseButtons.Left && this.buttonRectangle.Contains(e.X, e.Y))
            {
                this.mouseDownInButton = true;

                this.buttonDown = true;

                DrawButton(this.buttonRectangle, null, false);
            }
        }

        #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(this.mouseDownInButton)
            {
                if(this.buttonRectangle.Contains(e.X, e.Y))
                {
                    if(!this.buttonDown)
                    {
                        DrawButton(buttonRectangle, null, false);

                        this.buttonDown = true;
                    }
                }
                else
                {
                    if(this.buttonDown)
                    {
                        DrawButton(this.buttonRectangle, null, true);

                        this.buttonDown = false;
                    }
                }
            }

            if(this.redRectangle.Contains(new Point(e.X, e.Y)))
            {
                MessageBox.Show("배터리가 충전되었습니다.");
            }
        }

        #endregion
        #region 폼 마우스 UP 처리하기 - Form_MouseUp(sender, e)

        /// <summary>
        /// 폼 마우스 UP 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            if(this.mouseDownInButton && e.Button == MouseButtons.Left)
            {
                DrawButton(this.buttonRectangle, null, true);

                this.mouseDownInButton = false;

                if(this.buttonRectangle.Contains(e.X, e.Y))
                {
                    MessageBox.Show("버튼을 눌렀습니다.");
                }
            }
        }

        #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;

            DrawButton(this.buttonRectangle, graphics, true);

            graphics.FillRectangle(Brushes.Red, this.redRectangle);
        }

        #endregion
        #region 캡처 변경시 윈도우 캡처 변경시 처리하기 - captureChangedWindow_CaptureChanged()

        /// <summary>
        /// 캡처 변경시 윈도우 캡처 변경시 처리하기
        /// </summary>
        private void captureChangedWindow_CaptureChanged()
        {
            this.mouseDownInButton = false;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Function

        #region 버튼 그리기 - DrawButton(rectangle, graphics, buttonUp)

        /// <summary>
        /// 버튼 그리기
        /// </summary>
        /// <param name="rectangle">사각형</param>
        /// <param name="graphics">그래픽스</param>
        /// <param name="buttonUp">버튼 UP 여부</param>
        private void DrawButton(Rectangle rectangle, Graphics graphics, bool buttonUp)
        {
            bool disposeGraphics = false;

            if(graphics == null)
            {
                graphics = CreateGraphics();

                disposeGraphics = true;
            }

            GraphicsHelper helper = new GraphicsHelper(graphics);

            helper.DrawRectangle(Pens.Black, rectangle);

            Rectangle innerRectangle = new Rectangle(rectangle.Location, rectangle.Size);

            innerRectangle.Inflate(new Size(-1, -1));

            if(buttonUp)
            {
                helper.Draw3DRectangle(innerRectangle, Style3D.Raised, 2);
            }
            else
            {
                helper.FillRectangle(Brushes.LightGray, innerRectangle);
            }

            if(disposeGraphics)
            {
                graphics.Dispose();
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요