이미지를 마우스로 회전 하는 예제입니다.
소스는 다음과 같습니다.
public partial class Window1 : Window { private RotateTransform rotateTransform; private double angle; private double rotate; private Vector position; public Window1() { InitializeComponent(); Loaded += new RoutedEventHandler(Window1_Loaded); } private void Window1_Loaded(object sender, RoutedEventArgs e) { vinyl.MouseLeftButtonDown += new MouseButtonEventHandler(vinyl_MouseLeftButtonDown); vinyl.MouseMove += new MouseEventHandler(vinyl_MouseMove); vinyl.MouseLeftButtonUp += new MouseButtonEventHandler(vinyl_MouseLeftButtonUp); rotateTransform = new RotateTransform(); vinyl.RenderTransform = rotateTransform; vinyl.RenderTransformOrigin = new Point(0.5, 0.5); position = VisualTreeHelper.GetOffset(vinyl); position.X += vinyl.Width / 2; position.Y += vinyl.Height / 2; } private void vinyl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { Mouse.Capture(vinyl, CaptureMode.None); } private void vinyl_MouseMove(object sender, MouseEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { Point currentPoint = e.GetPosition(this); double _angle = Math.Atan2(position.Y - currentPoint.Y, position.X - currentPoint.X) * (180 / Math.PI); rotateTransform.Angle = rotate + _angle - angle; } } private void vinyl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Mouse.Capture(vinyl, CaptureMode.Element); Point point = e.GetPosition(this); angle = Math.Atan2(position.Y - point.Y, position.X - point.X) * (180 / Math.PI); rotate = rotateTransform.Angle; } } |
'Window > WPF' 카테고리의 다른 글
Storyboard.SetTargetProperty에 RenderTransformProperty 설정하기 (0) | 2011.03.04 |
---|---|
ListBox ItemsPanel 변경하기 (0) | 2011.03.04 |
[ WPF ] Custom Slider (4) | 2010.08.13 |
[ WPF ] ClickOnce로 배포하기 (9) | 2010.08.03 |
[ WPF ] RegisterWindowMessage 를 이용한 프로그램 간 메시지 보내기 (178) | 2010.07.21 |