728x90
반응형
728x170
▶ MainForm.cs
using System;
using System.Windows.Forms;
namespace TestProject
{
/// <summary>
/// 메인 폼
/// </summary>
public partial class MainForm : Form
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary>
/// 생성자
/// </summary>
public MainForm()
{
InitializeComponent();
// 로또 회차를 설정한다.
this.lottoControl.Sequence = 775;
// 로또 추첨일을 설정한다.
this.lottoControl.DrawDate = new DateTime(2017, 10, 7);
// 로또 당첨 번호를 표시한다.
this.lottoControl.ShowLottoNumber = true;
// 편집 가능 여부를 설정한다.
this.lottoControl.Editable = true;
// 로또 당첨 번호를 설정한다.
this.lottoControl.SetLottoNumber(11, 12, 29, 33, 38, 42, 17);
// 로또 번호를 선택한다.
this.lottoControl.SetMyLottoNumber(19, 34, 38, 40, 41, 43);
}
#endregion
}
}
728x90
▶ LottoControl.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace TestProject
{
/// <summary>
/// 로또 컨트롤
/// </summary>
public partial class LottoControl : UserControl
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 제목 폰트
/// </summary>
private static Font _titleFont;
/// <summary>
/// 히트 폰트
/// </summary>
private static Font _hitFont;
/// <summary>
/// 드로잉 브러시
/// </summary>
private static SolidBrush _drawingBrush;
/// <summary>
/// 히팅 브러시
/// </summary>
private static SolidBrush _hittingBrush;
/// <summary>
/// 번호 사각형 딕셔너리
/// </summary>
private static Dictionary<int, Rectangle> _numberRectangleDictionary = null;
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Instance
//////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 회차
/// </summary>
private int? sequence;
/// <summary>
/// 추첨일
/// </summary>
private DateTime? drawDate;
/// <summary>
/// 제목
/// </summary>
private string caption;
// 로또 딕셔너리
private Dictionary<int, int> lottoDictionary = new Dictionary<int, int>();
/// <summary>
/// 보너스 번호
/// </summary>
private int bonusNumber = 0;
/// <summary>
/// 나의 로또 딕셔너리
/// </summary>
private Dictionary<int, int> myLottoDictionary = new Dictionary<int, int>();
/// <summary>
/// 히트 카운트
/// </summary>
private int hitCount = 0;
/// <summary>
/// 편집 가능 여부
/// </summary>
private bool editable = false;
/// <summary>
/// 로또 번호 표시 여부
/// </summary>
private bool showLottoNumber = true;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 회차 - Sequence
/// <summary>
/// 회차
/// </summary>
[Category("LottoControl")]
[Browsable(true)]
public int? Sequence
{
get
{
return this.sequence;
}
set
{
this.sequence = value;
SetCaption();
Invalidate();
}
}
#endregion
#region 추첨일 - DrawDate
/// <summary>
/// 추첨일
/// </summary>
[Category("LottoControl")]
[Browsable(true)]
public DateTime? DrawDate
{
get
{
return this.drawDate;
}
set
{
this.drawDate = value;
SetCaption();
Invalidate();
}
}
#endregion
#region 로또 딕셔너리 - LottoDictionary
/// <summary>
/// 로또 딕셔너리
/// </summary>
[Category("LottoControl")]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Dictionary<int, int> LottoDictionary
{
get
{
return this.lottoDictionary;
}
}
#endregion
#region 나의 로또 딕셔너리 - MyLottoDictionary
/// <summary>
/// 나의 로또 딕셔너리
/// </summary>
[Category("LottoControl")]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Dictionary<int, int> MyLottoDictionary
{
get
{
return this.myLottoDictionary;
}
}
#endregion
#region 편집 가능 여부 - Editable
/// <summary>
/// 편집 가능 여부
/// </summary>
[Category("LottoControl")]
[Browsable(true)]
public bool Editable
{
get
{
return this.editable;
}
set
{
this.editable = value;
}
}
#endregion
#region 로또 번호 표시 여부 - ShowLottoNumber
/// <summary>
/// 로또 번호 표시 여부
/// </summary>
public bool ShowLottoNumber
{
get
{
return this.showLottoNumber;
}
set
{
this.showLottoNumber = value;
Invalidate();
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// static
#region 생성자 - LottoControl()
/// <summary>
/// 생성자
/// </summary>
static LottoControl()
{
_titleFont = new Font("Tahoma", 8f);
_hitFont = new Font("Times New Roman", 16f, FontStyle.Bold | FontStyle.Italic);
_drawingBrush = new SolidBrush(Color.FromArgb(90, Color.Blue));
_hittingBrush = new SolidBrush(Color.FromArgb(90, Color.Red ));
_numberRectangleDictionary = new Dictionary<int, Rectangle>();
for(int i = 0; i < 45; i++)
{
int x = i % 7;
int y = i / 7;
int left = x * 15 + 5;
int right = left + 10;
int top = y * 24 + 43;
int bottom = top + 15;
Rectangle rectangle = new Rectangle();
rectangle.X = left;
rectangle.Y = top;
rectangle.Width = right - left + 1;
rectangle.Height = bottom - top + 1;
_numberRectangleDictionary.Add(i, rectangle);
}
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Instance
//////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - LottoControl()
/// <summary>
/// 생성자
/// </summary>
public LottoControl()
{
InitializeComponent();
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 로또 번호 설정하기 - SetLottoNumber(number1, number2, number3, number4, number5, number6, bonusNumber)
/// <summary>
/// 로또 번호 설정하기
/// </summary>
/// <param name="number1">번호 1</param>
/// <param name="number2">번호 2</param>
/// <param name="number3">번호 3</param>
/// <param name="number4">번호 4</param>
/// <param name="number5">번호 5</param>
/// <param name="number6">번호 6</param>
/// <param name="bonusNumber">보너스 번호</param>
public void SetLottoNumber(int number1, int number2, int number3, int number4, int number5, int number6, int bonusNumber)
{
this.lottoDictionary.Clear();
this.lottoDictionary.Add(number1, number1);
this.lottoDictionary.Add(number2, number2);
this.lottoDictionary.Add(number3, number3);
this.lottoDictionary.Add(number4, number4);
this.lottoDictionary.Add(number5, number5);
this.lottoDictionary.Add(number6, number6);
this.bonusNumber = bonusNumber;
}
#endregion
#region 나의 로또 번호 설정하기 - SetMyLottoNumber(number1, number2, number3, number4, number5, number6)
/// <summary>
/// 나의 로또 번호 설정하기
/// </summary>
/// <param name="number1">번호 1</param>
/// <param name="number2">번호 2</param>
/// <param name="number3">번호 3</param>
/// <param name="number4">번호 4</param>
/// <param name="number5">번호 5</param>
/// <param name="number6">번호 6</param>
public void SetMyLottoNumber(int number1, int number2, int number3, int number4, int number5, int number6)
{
this.myLottoDictionary.Clear();
this.myLottoDictionary.Add(number1, number1);
this.myLottoDictionary.Add(number2, number2);
this.myLottoDictionary.Add(number3, number3);
this.myLottoDictionary.Add(number4, number4);
this.myLottoDictionary.Add(number5, number5);
this.myLottoDictionary.Add(number6, number6);
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 키 DOWN 처리하기 - OnKeyDown(e)
/// <summary>
/// 키 DOWN 처리하기
/// </summary>
/// <param name="e">이벤트 인자</param>
protected override void OnKeyDown(KeyEventArgs e)
{
if(e.KeyCode == Keys.Delete)
{
if(e.Control)
{
this.lottoDictionary.Clear();
}
else
{
this.myLottoDictionary.Clear();
}
Invalidate();
e.Handled = true;
}
if(e.KeyCode == Keys.L)
{
this.showLottoNumber = !this.showLottoNumber;
Invalidate();
e.Handled = true;
}
base.OnKeyDown(e);
}
#endregion
#region 마우스 DOWN 처리하기 - OnMouseDown(e)
/// <summary>
/// 마우스 DOWN 처리하기
/// </summary>
/// <param name="e">이벤트 발생자</param>
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if(!this.editable)
{
return;
}
bool controlKeyPressed = Control.ModifierKeys.HasFlag(Keys.Control);
foreach(KeyValuePair<int, Rectangle> keyValuePair in _numberRectangleDictionary)
{
if(keyValuePair.Value.Contains(e.Location))
{
int number = keyValuePair.Key;
if(controlKeyPressed)
{
if(this.lottoDictionary.ContainsKey(number))
{
this.lottoDictionary.Remove(number);
Invalidate();
}
else
{
if(this.lottoDictionary.Count < 6)
{
this.lottoDictionary.Add(number, number);
Invalidate();
}
}
}
else
{
if(this.myLottoDictionary.ContainsKey(number))
{
this.myLottoDictionary.Remove(number);
Invalidate();
}
else
{
if(this.myLottoDictionary.Count < 6)
{
this.myLottoDictionary.Add(number, number);
Invalidate();
}
}
}
break;
}
}
}
#endregion
#region 페인트시 처리하기 - OnPaint(e)
/// <summary>
/// 페인트시 처리하기
/// </summary>
/// <param name="e">이벤트 인자</param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics graphics = e.Graphics;
if(this.caption != null)
{
graphics.DrawString(this.caption, _titleFont, Brushes.White, new Point(7, 8));
}
if(this.myLottoDictionary.Count > 0)
{
foreach(KeyValuePair<int, int> keyValuePair in this.myLottoDictionary)
{
Rectangle rectangle = _numberRectangleDictionary[keyValuePair.Key];
if(this.lottoDictionary.Count > 0)
{
if(this.lottoDictionary.ContainsKey(keyValuePair.Key))
{
graphics.FillRectangle(_hittingBrush, rectangle);
}
else
{
graphics.FillRectangle(_drawingBrush, rectangle);
}
}
else
{
graphics.FillRectangle(_drawingBrush, rectangle);
}
}
}
if(this.lottoDictionary.Count > 0 && this.showLottoNumber)
{
foreach(KeyValuePair<int, int> keyValuePair in this.lottoDictionary)
{
Rectangle rectangle = _numberRectangleDictionary[keyValuePair.Key];
graphics.DrawLine(Pens.Black, rectangle.Left, rectangle.Top , rectangle.Right, rectangle.Bottom);
graphics.DrawLine(Pens.Black, rectangle.Left, rectangle.Bottom, rectangle.Right, rectangle.Top );
}
}
if(this.hitCount > 0)
{
graphics.DrawString(this.hitCount.ToString(), _hitFont, Brushes.Black, new Point(80, 190));
}
}
#endregion
#region 제목 설정하기 - SetCaption()
/// <summary>
/// 제목 설정하기
/// </summary>
private void SetCaption()
{
if(this.sequence.HasValue && this.drawDate.HasValue)
{
this.caption = string.Format("{0}회({1})", sequence.Value, drawDate.Value.ToString("yyyy/MM/dd"));
}
else
{
this.caption = null;
}
}
#endregion
#region 히트 카운트 설정하기 - SetHitCount()
/// <summary>
/// 히트 카운트 설정하기
/// </summary>
private void SetHitCount()
{
this.hitCount = 0;
if(this.lottoDictionary.Count == 0 || this.myLottoDictionary.Count == 0)
{
return;
}
foreach(KeyValuePair<int, int> keyValuePair in this.myLottoDictionary)
{
if(this.lottoDictionary.ContainsKey(keyValuePair.Key))
{
this.hitCount++;
}
}
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] 명령 프롬프트를 모방한 콘솔 컨트롤 사용하기 (0) | 2018.01.22 |
---|---|
[C#/WINFORM] 마우스 자동화 하기 (0) | 2018.01.20 |
[C#/WINFORM] Form 클래스 : InvokeOnClick 메소드를 사용해 컨트롤 클릭하기 (0) | 2018.01.20 |
[C#/WINFORM] SendMessage API를 사용해 컨트롤 클릭하기 (0) | 2018.01.20 |
[C#/WINFORM] 특수 키 상태 조사하기 (0) | 2018.01.17 |
[C#/WINFORM] 로또 컨트롤 사용하기 (0) | 2017.12.18 |
[C#/WINFORM] 바이트 배열에서 폰트 만들기 (0) | 2017.12.16 |
[C#/WINFORM] 폰트 설치 여부 구하기 (0) | 2017.11.25 |
[C#/WINFORM] 폰트 설치하기 (0) | 2017.11.23 |
[C#/WINFORM] DataGridView 클래스 : 사용자 열 순서 변경 가능하게 설정하기 (0) | 2017.10.27 |
[C#/WINFORM] DataGridView 클래스 : 사용자 행 삭제시 확인 메시지 표시하기 (0) | 2017.10.27 |
댓글을 달아 주세요