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

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

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

        #region 색상 설정하기 대리자 - SetColorDelegate(x, y, color)

        /// <summary>
        /// 색상 설정하기 대리자
        /// </summary>
        /// <param name="x">X 좌표</param>
        /// <param name="y">Y 좌표</param>
        /// <param name="color">색상</param>
        private delegate void SetColorDelegate(int x, int y, Color color);

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 스레드
        /// </summary>
        private Thread thread;

        #endregion

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

        #region 생성자 - MainForm()

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

            #region 이벤트를 설정한다.

            Load        += Form_Load;
            FormClosing += Form_FormClosing;
            KeyDown     += Form_KeyDown;

            #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)
        {
            this.thread = new Thread(new ThreadStart(ProcessThread));

            this.thread.Start();
        }

        #endregion
        #region 폼을 닫을 경우 처리하기 - Form_FormClosing(sender, e)

        /// <summary>
        /// 폼을 닫을 경우 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(this.thread != null && this.thread.IsAlive)
            {
                this.thread.Abort();
            }
        }

        #endregion
        #region 폼 키 DOWN 처리하기 - Form_KeyDown(sender, e)

        /// <summary>
        /// 폼 키 DOWN 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.C && e.Control)
            {
                if(this.thread.IsAlive)
                {
                    this.thread.Abort();

                    this.thread = null;

                    this.messageLabel.Text = "CTRL + S 키를 누르면 작업을 재개합니다.";
                }
            }
            else if(e.KeyCode == Keys.S && e.Control)
            {
                if(this.thread == null)
                {
                    this.thread = new Thread(new ThreadStart(ProcessThread));

                    this.thread.Start();

                    this.messageLabel.Text = "CTRL + C 키를 누르면 작업을 중단합니다.";
                }
            }
        }

        #endregion

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

        #region 마우스 위치 색상 구하기 - GetMousePointColor(mousePoint)

        /// <summary>
        /// 마우스 위치 색상 구하기
        /// </summary>
        /// <param name="mousePoint">마우스 위치</param>
        /// <returns>마우스 위치 색상</returns>
        private Color GetMousePointColor(Point mousePoint)
        {
            Size     size     = new Size(1, 1);
            Bitmap   bitmap   = new Bitmap(1, 1);
            Graphics graphics = Graphics.FromImage(bitmap);

            graphics.CopyFromScreen(mousePoint.X, mousePoint.Y, 0, 0, size);

            return bitmap.GetPixel(0, 0);
        }

        #endregion
        #region 16진수 문자열 구하기 - GetHexadecimalString(value)

        /// <summary>
        /// 16진수 문자열 구하기
        /// </summary>
        /// <param name="value">값</param>
        /// <returns>16진수 문자열</returns>
        private string GetHexadecimalString(int value)
        {
            byte[] byteArray       = BitConverter.GetBytes(value);
            int    byteArrayLength = byteArray.Length;

            StringBuilder stringBuilder = new StringBuilder();

            for(int i = 0; i < byteArrayLength; i++)
            {
                stringBuilder.Append(byteArray[i].ToString("X2"));
            }

            return stringBuilder.ToString();
        }

        #endregion
        #region 색상 설정하기 - SetColor(x, y, color)

        /// <summary>
        /// 색상 설정하기
        /// </summary>
        /// <param name="x">X 좌표</param>
        /// <param name="y">Y 좌표</param>
        /// <param name="color">색상</param>
        private void SetColor(int x, int y, Color color)
        {
            if(InvokeRequired)
            {
                SetColorDelegate setColorDelegate = new SetColorDelegate(SetColor);

                Invoke(setColorDelegate, x, y, color);
            }
            else
            {
                this.xTextBox.Text          = x.ToString();
                this.yTextBox.Text          = y.ToString();
                this.redTextBox.Text        = color.R.ToString();
                this.greenTextBox.Text      = color.G.ToString();
                this.blueTextBox.Text       = color.B.ToString();
                this.colorPanel.BackColor   = color;
                this.colorValueTextBox.Text = GetHexadecimalString(color.R).Substring(0, 2) +
                                              GetHexadecimalString(color.G).Substring(0, 2) +
                                              GetHexadecimalString(color.B).Substring(0, 2);
            }
        }

        #endregion
        #region 스레드 처리하기 - ProcessThread()

        /// <summary>
        /// 스레드 처리하기
        /// </summary>
        private void ProcessThread()
        {
            while(true)
            {
                Point mousePoint = Control.MousePosition;

                int x = mousePoint.X;
                int y = mousePoint.Y;

                Color color = GetMousePointColor(mousePoint);

                SetColor(x, y, color);
            }
        }

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

댓글을 달아 주세요