728x90
반응형
728x170
▶ 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
반응형
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] ColorTranslator 클래스 : FromHtml 정적 메소드를 사용해 16진수 색상 코드에서 색상 구하기 (0) | 2020.02.28 |
---|---|
[C#/WINFORM] 크로미엄 브라우저 사용하기 (0) | 2020.02.28 |
[C#/WINFORM] Form 클래스 : CreateParams 속성을 사용해 포커스 설정 방지하기 (0) | 2020.02.12 |
[C#/WINFORM] 멀티 원격 데스크톱 프로토콜 클라이언트(Remote Desktop Protocol Client) 사용하기 (0) | 2020.02.10 |
[C#/WINFORM] GIF 에디터 사용하기 (0) | 2020.01.28 |
[C#/WINFORM] Cursor 클래스 : Clip 정적 속성을 사용해 마우스 이동 영역 제한하기 (0) | 2020.01.13 |
[C#/WINFORM] GroupBox 클래스 : 익스팬더(Expander) 그룹 박스 사용하기 (0) | 2020.01.10 |
[C#/WINFORM] ScrollableControl 클래스 : 접을 수 있는 패널 사용하기 (0) | 2020.01.05 |
[C#/WINFORM] UserControl 클래스 : 푯말 레이블 사용하기 (0) | 2019.12.28 |
[C#/WINFORM] UserControl 클래스 : 체크 버튼 사용하기 (0) | 2019.12.28 |
댓글을 달아 주세요