728x90
반응형
728x170
▶ MainWindow.xaml
<Window
x:Class="TestProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="엘리먼트를 마우스로 드래그해 이동시키기"
Height="600"
Width="450">
<Canvas Name="canvas">
<Rectangle Name="rectangle"
Canvas.Left="50"
Canvas.Top="50"
Width="100"
Height="100"
Fill="Blue" />
</Canvas>
</Window>
728x90
▶ MainWindow.xaml.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace TestProject
{
/// <summary>
/// 메인 윈도우
/// </summary>
public partial class MainWindow : Window
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 드래그 여부
/// </summary>
private bool isDragging;
/// <summary>
/// 시작 위치
/// </summary>
private Point startPoint;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainWindow()
/// <summary>
/// 생성자
/// </summary>
public MainWindow()
{
InitializeComponent();
this.rectangle.MouseDown += rectangle_MouseDown;
this.rectangle.MouseMove += rectangle_MouseMove;
this.rectangle.MouseUp += rectangle_MouseUp;
this.rectangle.LostMouseCapture += rectangle_LostMouseCapture;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 사각형 마우스 DOWN 처리하기 - rectangle_MouseDown(sender, e)
/// <summary>
/// 사각형 마우스 DOWN 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void rectangle_MouseDown(object sender, MouseButtonEventArgs e)
{
this.startPoint = e.GetPosition(this.canvas);
this.isDragging = true;
this.rectangle.CaptureMouse();
}
#endregion
#region 사각형 마우스 이동시 처리하기 - rectangle_MouseMove(sender, e)
/// <summary>
/// 사각형 마우스 이동시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void rectangle_MouseMove(object sender, MouseEventArgs e)
{
if(this.isDragging)
{
Point mousePoint = e.GetPosition(this.canvas);
Vector vector = mousePoint - this.startPoint;
this.startPoint = mousePoint;
Canvas.SetLeft(this.rectangle, Canvas.GetLeft(this.rectangle) + vector.X);
Canvas.SetTop (this.rectangle, Canvas.GetTop (this.rectangle) + vector.Y);
}
}
#endregion
#region 사각형 마우스 UP 처리하기 - rectangle_MouseUp(sender, e)
/// <summary>
/// 사각형 마우스 UP 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void rectangle_MouseUp(object sender, MouseButtonEventArgs e)
{
this.isDragging = false;
Mouse.Capture(null);
}
#endregion
#region 사각형 마우스 캡처 상실시 처리하기 - rectangle_LostMouseCapture(sender, e)
/// <summary>
/// 사각형 마우스 캡처 상실시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void rectangle_LostMouseCapture(object sender, MouseEventArgs e)
{
this.isDragging = false;
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] WPF Window의 소유자를 실행하는 애플리케이션 종류에 따라 설정하기 (0) | 2015.12.19 |
---|---|
[C#/WPF] FontFamily 클래스 : 폰트명 구하기 (0) | 2015.12.19 |
[C#/WPF] DependencyObject 클래스 : 부모 의존 객체 찾기 (0) | 2015.12.19 |
[C#/WPF] Visual 클래스 : TransformToAncestor 메소드를 사용해 부모 엘리먼트 기준 좌표 구하기 (0) | 2015.11.05 |
[C#/WPF] MouseEventArgs 클래스 : GetPosition 메소드 사용하기 (0) | 2015.11.05 |
[C#/WPF] FrameworkElement 클래스 : 프레임워크 엘리먼트 전면으로 보이기 (0) | 2015.11.04 |
[C#/WPF] WindowsFormsHost 클래스 : WinForm 컨트롤의 부모 WindowsFormsHost 구하기 (0) | 2015.11.04 |
[C#/WPF] Adorner 클래스 사용하기 (0) | 2015.11.04 |
[C#/WPF] Popup 클래스 : 팝업 활성화하기 (0) | 2015.11.04 |
[C#/WPF] UIElement 클래스 : JPEG 이미지 구하기 (0) | 2015.11.04 |
댓글을 달아 주세요