728x90
반응형
728x170
■ Image 클래스를 사용해 마우스를 조작해이미지를 회전시키는 방법을 보여준다.
▶ 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"
Width="800"
Height="600"
Title="Image 클래스 : 마우스를 사용해 이미지 회전시키기"
FontFamily="나눔고딕코딩"
FontSize="16">
<Grid>
<Image Name="image"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Width="128"
Height="128"
Stretch="Fill"
Source="IMAGE/disk.png" />
</Grid>
</Window>
▶ MainWindow.xaml.cs
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
namespace TestProject
{
/// <summary>
/// 메인 윈도우
/// </summary>
public partial class MainWindow : Window
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 회전 변환
/// </summary>
private RotateTransform rotateTransform;
/// <summary>
/// 각도
/// </summary>
private double angle;
/// <summary>
/// 회전 각도
/// </summary>
private double rotateAngle;
/// <summary>
/// 위치
/// </summary>
private Vector position;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainWindow()
/// <summary>
/// 생성자
/// </summary>
public MainWindow()
{
InitializeComponent();
Loaded += Window_Loaded;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
//////////////////////////////////////////////////////////////////////////////// Event
#region 윈도우 로드시 처리하기 - Window_Loaded(sender, e)
/// <summary>
/// 윈도우 로드시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.rotateTransform = new RotateTransform();
this.image.RenderTransformOrigin = new Point(0.5, 0.5);
this.image.RenderTransform = this.rotateTransform;
this.position = VisualTreeHelper.GetOffset(this.image);
this.position.X += this.image.Width / 2;
this.position.Y += this.image.Height / 2;
this.image.MouseLeftButtonDown += image_MouseLeftButtonDown;
this.image.MouseMove += image_MouseMove;
this.image.MouseLeftButtonUp += image_MouseLeftButtonUp;
}
#endregion
#region 이미지 마우스 왼쪽 버튼 DOWN 처리하기 - image_MouseLeftButtonDown(sender, e)
/// <summary>
/// 이미지 마우스 왼쪽 버튼 DOWN 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Mouse.Capture(this.image, CaptureMode.Element);
Point mousePoint = e.GetPosition(this);
this.angle = Math.Atan2(position.Y - mousePoint.Y, position.X - mousePoint.X) * (180 / Math.PI);
this.rotateAngle = this.rotateTransform.Angle;
}
#endregion
#region 이미지 마우스 이동시 처리하기 - image_MouseMove(sender, e)
/// <summary>
/// 이미지 마우스 이동시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void image_MouseMove(object sender, MouseEventArgs e)
{
if(e.LeftButton == MouseButtonState.Pressed)
{
Point mousePoint = e.GetPosition(this);
double mouseAngle = Math.Atan2(position.Y - mousePoint.Y, position.X - mousePoint.X) * (180 / Math.PI);
this.rotateTransform.Angle = this.rotateAngle + mouseAngle - this.angle;
}
}
#endregion
#region 이미지 마우스 왼쪽 버튼 UP 처리하기 - image_MouseLeftButtonUp(sender, e)
/// <summary>
/// 이미지 마우스 왼쪽 버튼 UP 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Mouse.Capture(this.image, CaptureMode.None);
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] x:Reference 태그 확장 사용하기 (0) | 2021.02.21 |
---|---|
[C#/WPF] x:Shared 속성 사용하기 (0) | 2021.02.20 |
[C#/WPF] Shape 클래스 : 환형 진행바 사용하기 (0) | 2021.02.20 |
[C#/WPF] 마우스를 사용해 엘리먼트 이동/회전/확대/축소하기 (0) | 2021.02.20 |
[C#/WPF] EventTrigger 클래스 : 코드로 이벤트 트리거 사용하기 (0) | 2021.02.20 |
[C#/WPF] ComponentDispatcher 클래스 : ThreadFilterMessage 정적 이벤트를 사용해 프로세스 간 메시지 송수신하기 (0) | 2021.02.20 |
[C#/WPF] Slider 클래스 : 커스텀 슬라이더 사용하기 (0) | 2021.02.20 |
[C#/WPF] Window 클래스 : 반투명 윈도우 사용하기 (0) | 2021.02.19 |
[C#/WPF] MediaElement 클래스 : 움직이는 GIF 파일 재생하기 (0) | 2021.02.19 |
[C#/WPF] 워드 클라우드(Word Cloud) 이미지 만들기 (0) | 2021.02.19 |
댓글을 달아 주세요