728x90
반응형
728x170
▶ 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 bool isPressed = false;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary>
/// 생성자
/// </summary>
public MainForm()
{
InitializeComponent();
#region 이벤트를 설정한다.
this.canvasPictureBox.MouseDown += canvasPictureBox_MouseDown;
this.canvasPictureBox.MouseMove += canvasPictureBox_MouseMove;
this.canvasPictureBox.MouseClick += canvasPictureBox_MouseClick;
this.canvasPictureBox.MouseUp += canvasPictureBox_MouseUp;
#endregion
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
//////////////////////////////////////////////////////////////////////////////// Event
#region 캔버스 픽처 박스 마우스 DOWN 처리하기 - canvasPictureBox_MouseDown(sender, e)
/// <summary>
/// 캔버스 픽처 박스 마우스 DOWN 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void canvasPictureBox_MouseDown(object sender, MouseEventArgs e)
{
if(IsMouseOverButton(e.Location))
{
this.isPressed = true;
this.canvasPictureBox.Image = Properties.Resources.ButtonDown;
}
}
#endregion
#region 캔버스 픽처 박스 마우스 이동시 처리하기 - canvasPictureBox_MouseMove(sender, e)
/// <summary>
/// 캔버스 픽처 박스 마우스 이동시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void canvasPictureBox_MouseMove(object sender, MouseEventArgs e)
{
Image targetImage = Properties.Resources.ButtonUp;
if(IsMouseOverButton(e.Location))
{
if(this.isPressed)
{
targetImage = Properties.Resources.ButtonDown;
}
else
{
targetImage = Properties.Resources.ButtonMouseOver;
}
}
else
{
targetImage = Properties.Resources.ButtonUp;
}
if(this.canvasPictureBox.Image != targetImage)
{
this.canvasPictureBox.Image = targetImage;
}
}
#endregion
#region 캔버스 픽처 박스 마우스 클릭시 처리하기 - canvasPictureBox_MouseClick(sender, e)
/// <summary>
/// 캔버스 픽처 박스 마우스 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void canvasPictureBox_MouseClick(object sender, MouseEventArgs e)
{
if(IsMouseOverButton(e.Location))
{
MessageBox.Show("Clicked");
}
}
#endregion
#region 캔버스 픽처 박스 마우스 UP 처리하기 - canvasPictureBox_MouseUp(sender, e)
/// <summary>
/// 캔버스 픽처 박스 마우스 UP 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void canvasPictureBox_MouseUp(object sender, MouseEventArgs e)
{
this.isPressed = false;
this.canvasPictureBox.Image = Properties.Resources.ButtonUp;
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region 마우스 버튼 상위 여부 구하기 - IsMouseOverButton(mousePoint)
/// <summary>
/// 마우스 버튼 상위 여부 구하기
/// </summary>
/// <param name="mousePoint">마우스 포인트</param>
/// <returns>마우스 버튼 상위 여부</returns>
private bool IsMouseOverButton(Point mousePoint)
{
if(mousePoint.X < 0)
{
return false;
}
if(mousePoint.Y < 0)
{
return false;
}
if(mousePoint.X >= Properties.Resources.ButtonMask.Width)
{
return false;
}
if(mousePoint.Y >= Properties.Resources.ButtonMask.Height)
{
return false;
}
Color color = Properties.Resources.ButtonMask.GetPixel(mousePoint.X, mousePoint.Y);
return ((color.A == 255) && (color.R == 0) && (color.G == 0) && (color.B == 0));
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] ListView 클래스 : 커스텀 아이콘 사용하기 (0) | 2019.01.14 |
---|---|
[C#/WINFORM] Cursor 클래스 : 사용자 정의 커서 사용하기 (0) | 2019.01.14 |
[C#/WINFORM] 사용자가 스크롤바 최대값을 선택할 수 있게 하기 (0) | 2019.01.14 |
[C#/WINFORM] StringFormat 클래스 : Alignment 속성을 사용해 문자열 정렬하기 (0) | 2019.01.14 |
[C#/WINFORM] StringFormat 클래스 : SetTabStops 메소드를 사용해 탭 설정하기 (0) | 2019.01.14 |
[C#/WINFORM] 극 좌표(Polar Coordinates)에서 곡선 그리기 (0) | 2019.01.14 |
[C#/WINFORM] RichTextBox 클래스 : 내용에 따라 크기 맞추기 (0) | 2019.01.14 |
[C#/WINFORM] TextBox 클래스 : 텍스트에 따라 크기 맞추기 (0) | 2019.01.14 |
[C#/WINFORM] 문단 그리기 (0) | 2019.01.14 |
[C#/WINFORM] 사용자 정의 좌표 체계 사용하기 (0) | 2019.01.14 |
댓글을 달아 주세요