첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.02MB

▶ 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>

 

728x90

 

▶ 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
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요