첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.

728x90
반응형
728x170

TestProject.zip
7.61MB

▶ DragHelper.cs

using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Media;

namespace TestProject
{
    /// <summary>
    /// 드래그 헬퍼
    /// </summary>
    public class DragHelper : DependencyObject
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 드래그 여부 속성 - DragProperty

        /// <summary>
        /// 드래그 여부 속성
        /// </summary>
        public static readonly DependencyProperty DragProperty = DependencyProperty.RegisterAttached
        (
            "Drag",
            typeof(bool),
            typeof(DragHelper),
            new UIPropertyMetadata(false, DragPropertyChangedCallback)
        );

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 윈도우
        /// </summary>
        private static Window _window;

        /// <summary>
        /// 드래그 시작 포인트
        /// </summary>
        private static Point _dragStartPoint;

        /// <summary>
        /// 드래그 엘리먼트
        /// </summary>
        private static FrameworkElement _drgaElement;

        /// <summary>
        /// 드래그 Z 인덱스
        /// </summary>
        private static int _dragZIndex;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 윈도우 설정하기 - SetWindow(window)

        /// <summary>
        /// 윈도우 설정하기
        /// </summary>
        /// <param name="window">윈도우</param>
        public static void SetWindow(Window window)
        {
            _window = window;
        }

        #endregion

        #region 드래그 여부 설정하기 - SetDrag(element, flag)

        /// <summary>
        /// 드래그 여부 설정하기
        /// </summary>
        /// <param name="element">엘리먼트</param>
        /// <param name="flag">플래그</param>
        public static void SetDrag(UIElement element, bool flag)
        {
            if(_window != null)
            {
                element.SetValue(DragProperty, flag);
            }
        }

        #endregion
        #region 드래그 여부 구하기 - GetDrag(UIElement element)

        /// <summary>
        /// 드래그 여부 구하기
        /// </summary>
        /// <param name="element">엘리먼트</param>
        /// <returns>드래그 여부</returns>
        public static bool GetDrag(UIElement element)
        {
            return (bool)element.GetValue(DragProperty);
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Private

        #region 윈도우 PREVIEW 마우스 이동시 처리하기 - window_PreviewMouseMove(sender, e)

        /// <summary>
        /// 윈도우 PREVIEW 마우스 이동시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private static void window_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if(_drgaElement != null && e.LeftButton == MouseButtonState.Pressed || e.RightButton == MouseButtonState.Pressed)
            {
                Point mousePoint = e.GetPosition(_drgaElement);

                TranslateTransform transform = _drgaElement.RenderTransform as TranslateTransform;

                if(transform != null)
                {
                    _drgaElement.LayoutTransform = transform;

                    transform.X += mousePoint.X - _dragStartPoint.X;
                    transform.Y += mousePoint.Y - _dragStartPoint.Y;
                }
            }          
        }

        #endregion
        #region 엘리먼트 PREVIEW 마우스 DOWN 처리하기 - element_PreviewMouseDown(sender, e)

        /// <summary>
        /// 엘리먼트 PREVIEW 마우스 DOWN 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private static void element_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            _drgaElement = sender as FrameworkElement;

            _dragZIndex = Canvas.GetZIndex(_drgaElement);

            Canvas.SetZIndex(_drgaElement, 100);

            _dragStartPoint = e.GetPosition(_drgaElement);

            _window.PreviewMouseMove += window_PreviewMouseMove;
        }

        #endregion
        #region 엘리먼트 PREVIEW 마우스 UP 처리하기 - element_PreviewMouseUp(sender, e)

        /// <summary>
        /// 엘리먼트 PREVIEW 마우스 UP 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private static void element_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;

            Canvas.SetZIndex(element, _dragZIndex);

            _drgaElement = _window;

            _window.MouseMove -= window_PreviewMouseMove;
        }

        #endregion

        #region 드래그 속성 변경시 콜백 처리하기 - DragPropertyChangedCallback(d, e)

        /// <summary>
        /// 드래그 속성 변경시 콜백 처리하기
        /// </summary>
        /// <param name="d">의존 객체</param>
        /// <param name="e">이벤트 인자</param>
        private static void DragPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            FrameworkElement element = d as FrameworkElement;

            if(element != null)
            {
                if((bool)e.NewValue)
                {
                    element.RenderTransform = new TranslateTransform(0, 0);

                    element.PreviewMouseDown += element_PreviewMouseDown;
                    element.PreviewMouseUp   += element_PreviewMouseUp;
                }
                else
                { 
                    element.PreviewMouseDown -= element_PreviewMouseDown;
                    element.PreviewMouseUp   -= element_PreviewMouseUp;
                }
            }
        }

        #endregion
    }
}

 

728x90

 

▶ 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="Canvas 클래스 : 자식 엘리먼트 드래그하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Canvas>
        <Image Name="image1" Canvas.Left="50" Canvas.Top="50"
            Width="200"
            Height="150"
            Stretch="Fill"
            Source="IMAGE/sample1.png" />
        <Image Name="image2" Canvas.Left="50" Canvas.Top="210"
            Width="200"
            Height="150"
            Stretch="Fill"
            Source="IMAGE/sample2.png" />
        <Button Name="button" Canvas.Left="300" Canvas.Top="50"
            Width="100"
            Height="30"
            Content="Test" />
        <TextBlock Name="textBlock" Canvas.Left="300" Canvas.Top="210"
            Width="200"
            Text="TEXT BLOCK" />
    </Canvas>
</Window>

 

300x250

 

▶ MainWindow.xaml.cs

using System.Windows;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            Loaded += Window_Loaded;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e)

        /// <summary>
        /// 윈도우 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DragHelper.SetWindow(this);

            DragHelper.SetDrag(this.image1   , true);
            DragHelper.SetDrag(this.image2   , true);
            DragHelper.SetDrag(this.button   , true);
            DragHelper.SetDrag(this.textBlock, true);
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요