728x90
반응형
728x170
▶ MainForm.cs
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TestProject
{
/// <summary>
/// 메인 폼
/// </summary>
public partial class MainForm : Form
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region Field
/// <summary>
/// 드래그 비트맵
/// </summary>
private Bitmap dragBitmap;
/// <summary>
/// 드래그 사각형
/// </summary>
private Rectangle dragRectangle;
/// <summary>
/// 드래그 여부
/// </summary>
private bool isDragging = false;
/// <summary>
/// 오프셋 X
/// </summary>
private int offsetX;
/// <summary>
/// 오프셋 Y
/// </summary>
private int offsetY;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary>
/// 생성자
/// </summary>
public MainForm()
{
InitializeComponent();
#region 이벤트를 설정한다.
Load += Form_Load;
this.pictureBox.Paint += pictureBox_Paint;
this.pictureBox.MouseDown += pictureBox_MouseDown;
this.pictureBox.MouseMove += pictureBox_MouseMove;
this.pictureBox.MouseUp += pictureBox_MouseUp;
#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.dragBitmap = new Bitmap(Properties.Resources.smile);
this.dragBitmap.MakeTransparent(Color.White);
this.dragRectangle = new Rectangle(10, 10, dragBitmap.Width, dragBitmap.Height);
}
#endregion
#region 픽처 박스 마우스 DOWN 처리하기 - pictureBox_MouseDown(sender, e)
/// <summary>
/// 픽처 박스 마우스 DOWN 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
if(IsPointOverPicture(e.X, e.Y))
{
this.isDragging = true;
this.offsetX = this.dragRectangle.X - e.X;
this.offsetY = this.dragRectangle.Y - e.Y;
}
}
#endregion
#region 픽처 박스 마우스 이동시 처리하기 - pictureBox_MouseMove(sender, e)
/// <summary>
/// 픽처 박스 마우스 이동시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void pictureBox_MouseMove(object sender, MouseEventArgs e)
{
if(this.isDragging)
{
this.dragRectangle.X = e.X + this.offsetX;
this.dragRectangle.Y = e.Y + this.offsetY;
this.pictureBox.Invalidate();
}
else
{
Cursor cursor = Cursors.Default;
if(IsPointOverPicture(e.X, e.Y))
{
cursor = Cursors.Hand;
}
if(this.pictureBox.Cursor != cursor)
{
this.pictureBox.Cursor = cursor;
}
}
}
#endregion
#region 픽처 박스 마우스 UP 처리하기 - pictureBox_MouseUp(sender, e)
/// <summary>
/// 픽처 박스 마우스 UP 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
this.isDragging = false;
}
#endregion
#region 픽처 박스 페인트시 처리하기 - pictureBox_Paint(sender, e)
/// <summary>
/// 픽처 박스 페인트시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void pictureBox_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(this.dragBitmap, this.dragRectangle);
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region 포인트의 그림 상위 여부 구하기 - IsPointOverPicture(x, y)
/// <summary>
/// 포인트의 그림 상위 여부 구하기
/// </summary>
/// <param name="x">X 좌표</param>
/// <param name="y">Y 좌표</param>
/// <returns>포인트의 그림 상위 여부</returns>
private bool IsPointOverPicture(int x, int y)
{
if((x < this.dragRectangle.Left) || (x >= this.dragRectangle.Right) || (y < this.dragRectangle.Top) || (y >= this.dragRectangle.Bottom))
{
return false;
}
int i = x - this.dragRectangle.X;
int j = y - this.dragRectangle.Y;
return (dragBitmap.GetPixel(i, j).A > 0);
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] 다각형 에디터 사용하기 (0) | 2018.12.13 |
---|---|
[C#/WINFORM] 라인 에디터 사용하기 (0) | 2018.12.13 |
[C#/WINFORM] 확대 창 사용하기 (0) | 2018.12.13 |
[C#/WINFORM] 2D/3D 테두리 그리기 (0) | 2018.12.13 |
[C#/WINFORM] 소수 프랙탈(Prime Number Fractal) 그리기 (0) | 2018.12.11 |
[C#/WINFORM] Region 클래스 : 특정 모양을 갖는 폼 만들기 (0) | 2018.12.10 |
[C#/WINFORM] TextureBrush 클래스 사용하기 (0) | 2018.12.10 |
[C#/WINFORM] 영역 채우기(Flood Fill) (0) | 2018.12.10 |
[C#/WINFORM] 유니코드 문자 조회하기 (0) | 2018.12.09 |
[C#/WINFORM] 움직이는 GIF 이미지 사용하기 (0) | 2018.12.09 |
댓글을 달아 주세요