첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ DraggableAdorner.cs

using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;

namespace TestProject
{
    /// <summary>
    /// 드래그 가능 어도너
    /// </summary>
    public class DraggableAdorner : Adorner
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// 중심 포인트 오프셋
        /// </summary>
        public Point CenterPointOffset;

        #endregion

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

        #region Field

        /// <summary>
        /// 렌더 사각형
        /// </summary>
        private Rect renderRectangle;

        /// <summary>
        /// 렌더 브러시
        /// </summary>
        private Brush renderBrush;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - DraggableAdorner(draggable)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="draggable">드래그 가능 객체</param>
        public DraggableAdorner(Draggable draggable) : base(draggable)
        {
            this.renderRectangle = new Rect(draggable.RenderSize);

            IsHitTestVisible = false;

            this.renderBrush = draggable.Background.Clone();

            CenterPointOffset = new Point(-this.renderRectangle.Width / 2, -this.renderRectangle.Height / 2);
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Protected

        #region 렌더링 처리하기 - OnRender(drawingContext)

        /// <summary>
        /// 렌더링 처리하기
        /// </summary>
        /// <param name="drawingContext">그리기 컨텍스트</param>
        protected override void OnRender(DrawingContext drawingContext)
        {
            drawingContext.DrawRectangle(this.renderBrush, null, this.renderRectangle);
        }

        #endregion
    }
}

 

▶ Draggable.xaml

<UserControl x:Class="TestProject.Draggable"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    mc:Ignorable="d" 
    d:DesignWidth="200"
    d:DesignHeight="100"
    BorderBrush="Black"
    BorderThickness="5"
    Background="Red">
    <Grid>
        <Label
            Margin="10 10 0 0"
            FontFamily="나눔고딕코딩"
            FontSize="20"
            FontWeight="Bold"
            Foreground="White"
            Content="DRAGGABLE" />
    </Grid>
</UserControl>

 

▶ Draggable.xaml.cs

using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;

namespace TestProject
{
    /// <summary>
    /// 드래그 가능 객체
    /// </summary>
    public partial class Draggable : UserControl
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Import
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 커서 위치 구하기 - GetCursorPos(point)

        /// <summary>
        /// 커서 위치 구하기
        /// </summary>
        /// <param name="point">포인트</param>
        [DllImport("user32.dll")]
        private static extern void GetCursorPos(ref POINT point);

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 드래그 가능 어도너
        /// </summary>
        private DraggableAdorner draggableAdorner;

        /// <summary>
        /// 커서 포인트
        /// </summary>
        private POINT cursorPoint = new POINT();

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - Draggable()

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

            MouseMove           += UserControl_MouseMove;
            PreviewGiveFeedback += UserControl_PreviewGiveFeedback;
        }

        #endregion

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

        #region 사용자 컨트롤 마우스 이동시 처리하기 - UserControl_MouseMove(sender, e)

        /// <summary>
        /// 사용자 컨트롤 마우스 이동시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void UserControl_MouseMove(object sender, MouseEventArgs e)
        {
            if(e.LeftButton == MouseButtonState.Pressed)
            {
                DataObject dataObject = new DataObject("COLOR", Background);

                AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(this);

                draggableAdorner = new DraggableAdorner(this);

                adornerLayer.Add(draggableAdorner);

                DragDrop.DoDragDrop(this, dataObject, DragDropEffects.Copy);

                adornerLayer.Remove(draggableAdorner);
            }
        }

        #endregion
        #region 사용자 컨트롤 프리뷰 피드백 주기 - UserControl_PreviewGiveFeedback(sender, e)

        /// <summary>
        /// 사용자 컨트롤 프리뷰 피드백 주기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void UserControl_PreviewGiveFeedback(object sender, GiveFeedbackEventArgs e)
        {            
            GetCursorPos(ref cursorPoint);

            Point point = PointFromScreen(cursorPoint.GetPoint(draggableAdorner.CenterPointOffset));

            draggableAdorner.Arrange(new Rect(point, draggableAdorner.DesiredSize));
        }

        #endregion
    }
}

 

▶ DropZone.xaml

<UserControl x:Class="TestProject.DropZone"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    mc:Ignorable="d" 
    d:DesignWidth="400"
    d:DesignHeight="300"
    Background="#ff2e2eac"
    AllowDrop="True">
    <Grid>
        <Rectangle Name="rectangle"
            Stroke="Black"
            StrokeThickness="5" />
        <Label
            Margin="10 10 0 0"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Foreground="White"
            FontFamily="나눔고딕코딩"
            FontWeight="Bold"
            FontSize="20"
            Content="DROP ZONE" />
    </Grid>
</UserControl>

 

▶ DropZone.xaml.cs

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

namespace TestProject
{
    /// <summary>
    /// DROP ZONE
    /// </summary>
    public partial class DropZone : UserControl
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - DropZone()

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

            DragEnter += UserControl_DragEnter;
            Drop      += UserControl_Drop;
            DragLeave += UserControl_DragLeave;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 사용자 컨트롤 드래그 ENTER 처리하기 - UserControl_DragEnter(sender, e)

        /// <summary>
        /// 사용자 컨트롤 드래그 ENTER 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void UserControl_DragEnter(object sender, DragEventArgs e)
        {
            this.rectangle.Stroke          = Brushes.LightBlue;
            this.rectangle.StrokeDashArray = new DoubleCollection() { 4, 2 };
        }

        #endregion
        #region 사용자 컨트롤 DROP 처리하기 - UserControl_Drop(sender, e)

        /// <summary>
        /// 사용자 컨트롤 DROP 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void UserControl_Drop(object sender, DragEventArgs e)
        {
            this.rectangle.Stroke          = Brushes.Black;
            this.rectangle.StrokeDashArray = null;

            Brush brush = (Brush)e.Data.GetData("COLOR");

            Background = brush;
        }

        #endregion
        #region 사용자 컨트롤 드래그 이탈시 처리하기 - UserControl_DragLeave(sender, e)

        /// <summary>
        /// 사용자 컨트롤 드래그 이탈시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void UserControl_DragLeave(object sender, DragEventArgs e)
        {
            this.rectangle.Stroke          = Brushes.Black;
            this.rectangle.StrokeDashArray = null;
        }

        #endregion
    }
}

 

▶ 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"
    xmlns:local="clr-namespace:TestProject"
    Width="800"
    Height="600"
    Title="DragDrop 클래스 : DoDragDrop 정적 메소드를 사용해 드래그 &amp; 드롭 사용하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Canvas>
        <local:Draggable Canvas.Left="20"  Canvas.Top="20"  Width="150" Height="80"                        />
        <local:Draggable Canvas.Left="20"  Canvas.Top="120" Width="150" Height="80" Background="#ff00c12c" />
        <local:Draggable Canvas.Left="20"  Canvas.Top="220" Width="150" Height="80" Background="#ff00d1ff" />
        <local:Draggable Canvas.Left="20"  Canvas.Top="320" Width="150" Height="80" Background="#ffae00ff" />
        <local:DropZone  Canvas.Left="300" Canvas.Top="20"  Width="300" Height="380" />
    </Canvas>
</Window>

 

▶ MainWindow.xaml

using System.Windows;

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

        #region 생성자 - MainWindow()

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

        #endregion
    }
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요