첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

728x90
반응형

■ UIElement 클래스 : DragEnter/GiveFeedback/DragOver/Drop/DragLeave 이벤트를 사용해 드래그하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip



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="UIElement 클래스 : DragEnter/GiveFeedback/DragOver/Drop/DragLeave 이벤트를 사용해 드래그하기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <Grid>

        <StackPanel

            HorizontalAlignment="Center"

            VerticalAlignment="Center"

            Margin="10">

            <Ellipse Name="ellipse1"

                Margin="10"

                Width="50"

                Height="50"

                Fill="Green"

                AllowDrop="True" />

            <Ellipse Name="ellipse2"

                Margin="10"

                Width="50"

                Height="50"

                Fill="Red"

                AllowDrop="True" />

            <RichTextBox Name="richTextBox"

                Margin="10"

                AllowDrop="True" />

        </StackPanel>

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System;

using System.IO;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Shapes;

 

namespace TestProject

{

    /// <summary>

    /// 메인 애플리케이션

    /// </summary>

    public partial class MainWindow : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

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

 

        #region Field

 

        /// <summary>

        /// 이전 브러시

        /// </summary>

        private Brush previousBrush = null;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            this.ellipse1.MouseMove    += ellipse_MouseMove;

            this.ellipse1.DragEnter    += ellipse_DragEnter;

            this.ellipse1.GiveFeedback += ellipse_GiveFeedback;

            this.ellipse1.DragOver     += ellipse_DragOver;

            this.ellipse1.Drop         += ellipse_Drop;

            this.ellipse1.DragLeave    += ellipse_DragLeave;

 

            this.ellipse2.MouseMove    += ellipse_MouseMove;

            this.ellipse2.DragEnter    += ellipse_DragEnter;

            this.ellipse2.GiveFeedback += ellipse_GiveFeedback;

            this.ellipse2.DragOver     += ellipse_DragOver;

            this.ellipse2.Drop         += ellipse_Drop;

            this.ellipse2.DragLeave    += ellipse_DragLeave;

 

            this.richTextBox.AddHandler(RichTextBox.DragOverEvent, new DragEventHandler(richTextBox_DragOver), true);

            this.richTextBox.AddHandler(RichTextBox.DropEvent    , new DragEventHandler(richTextBox_Drop    ), true);

 

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

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

 

        #region 타원 마우스 이동시 처리하기 - ellipse_MouseMove(sender, e)

 

        /// <summary>

        /// 타원 마우스 이동시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void ellipse_MouseMove(object sender, MouseEventArgs e)

        {

            Ellipse ellipse = sender as Ellipse;

 

            if(ellipse != null && e.LeftButton == MouseButtonState.Pressed)

            {

                DragDrop.DoDragDrop(ellipse, ellipse.Fill.ToString(), DragDropEffects.Copy);

            }

        }

 

        #endregion

        #region 타원 드래그 진입시 처리하기 - ellipse_DragEnter(sender, e)

 

        /// <summary>

        /// 타원 드래그 진입시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void ellipse_DragEnter(object sender, DragEventArgs e)

        {

            Ellipse ellipse = sender as Ellipse;

 

            if(ellipse != null)

            {

                this.previousBrush = ellipse.Fill;

                

                if(e.Data.GetDataPresent(DataFormats.StringFormat))

                {

                    string dataString = (string)e.Data.GetData(DataFormats.StringFormat);

 

                    BrushConverter converter = new BrushConverter();

 

                    if(converter.IsValid(dataString))

                    {

                        Brush newBrush = (Brush)converter.ConvertFromString(dataString);

 

                        ellipse.Fill = newBrush;

                    }

                }

            }

        }

 

        #endregion

        #region 타원 피드백 주기 - ellipse_GiveFeedback(sender, e)

 

        /// <summary>

        /// 타원 피드백 주기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void ellipse_GiveFeedback(object sender, GiveFeedbackEventArgs e)

        {

            try

            {

                if(e.Effects.HasFlag(DragDropEffects.Move))

                {

                    Mouse.SetCursor(Cursors.ArrowCD);

                }

                else if(e.Effects.HasFlag(DragDropEffects.Copy))

                {

                    Mouse.SetCursor(Cursors.Wait);

                }

            }

            catch(Exception)

            {

                e.UseDefaultCursors = true;

            }

 

            e.Handled = true;

        }

 

        #endregion

        #region 타원 드래그 OVER 처리하기 - ellipse_DragOver(sender, e)

 

        /// <summary>

        /// 타원 드래그 OVER 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void ellipse_DragOver(object sender, DragEventArgs e)

        {

            e.Effects = DragDropEffects.None;

 

            if(e.Data.GetDataPresent(DataFormats.StringFormat))

            {

                string dataString = (string)e.Data.GetData(DataFormats.StringFormat);

 

                BrushConverter converter = new BrushConverter();

 

                if(converter.IsValid(dataString))

                {

                    e.Effects = DragDropEffects.Copy | DragDropEffects.Move;

                }

            }

        }

 

        #endregion

        #region 타원 드래그 DROP 처리하기 - ellipse_Drop(sender, e)

 

        /// <summary>

        /// 타원 드래그 DROP 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void ellipse_Drop(object sender, DragEventArgs e)

        {

            Ellipse ellipse = sender as Ellipse;

 

            if(ellipse != null)

            {

                if(e.Data.GetDataPresent(DataFormats.StringFormat))

                {

                    string dataString = (string)e.Data.GetData(DataFormats.StringFormat);

 

                    BrushConverter converter = new BrushConverter();

 

                    if(converter.IsValid(dataString))

                    {

                        Brush newBrush = (Brush)converter.ConvertFromString(dataString);

 

                        ellipse.Fill = newBrush;

                    }

                }

            }

        }

 

        #endregion

        #region 타원 드래그 이탈시 처리하기 - ellipse_DragLeave(sender, e)

 

        /// <summary>

        /// 타원 드래그 이탈시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void ellipse_DragLeave(object sender, DragEventArgs e)

        {

            Ellipse ellipse = sender as Ellipse;

 

            if(ellipse != null)

            {

                ellipse.Fill = this.previousBrush;

            }

        }

 

        #endregion

        #region 리치 텍스트 박스 드래그 OVER 처리하기 - richTextBox_DragOver(sender, e)

 

        /// <summary>

        /// 리치 텍스트 박스 드래그 OVER 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void richTextBox_DragOver(object sender, DragEventArgs e)

        {

            if(e.Data.GetDataPresent(DataFormats.FileDrop))

            {

                e.Effects = DragDropEffects.All;

            }

            else

            {

                e.Effects = DragDropEffects.None;

            }

 

            e.Handled = false;

        }

 

        #endregion

        #region 리치 텍스트 박스 드래그 DROP 처리하기 - richTextBox_Drop(sender, e)

 

        /// <summary>

        /// 리치 텍스트 박스 드래그 DROP 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void richTextBox_Drop(object sender, DragEventArgs e)

        {

            if(e.Data.GetDataPresent(DataFormats.FileDrop))

            {

                string[] filePathArray = (string[])e.Data.GetData(DataFormats.FileDrop);

 

                string dataFormat = DataFormats.Rtf;

 

                if(e.KeyStates == DragDropKeyStates.ShiftKey)

                {

                    dataFormat = DataFormats.Text;

                }

 

                TextRange  textRange;

                FileStream fileStream;

 

                if(File.Exists(filePathArray[0]))

                {

                    try

                    {

                        textRange = new TextRange

                        (

                            this.richTextBox.Document.ContentStart,

                            this.richTextBox.Document.ContentEnd

                        );

 

                        fileStream = new FileStream(filePathArray[0], FileMode.OpenOrCreate);

 

                        textRange.Load(fileStream, dataFormat);

 

                        fileStream.Close();

                    }

                    catch(Exception)

                    {

                        MessageBox.Show("File could not be opened. Make sure the file is a text file.");

                    }

                }

            }

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

728x90
반응형
Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요