[C#/WPF] UIElement 클래스 : DragEnter/GiveFeedback/DragOver/Drop/DragLeave 이벤트를 사용해 드래그하기
C#/WPF 2020. 8. 8. 22:42728x90
반응형
728x170
▶ 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>
728x90
▶ 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
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] FrameworkElement 클래스 : MoveFocus 메소드를 사용해 포커스 이동하기 (0) | 2020.08.09 |
---|---|
[C#/WPF] Button 엘리먼트 : Style 속성 사용하기 (0) | 2020.08.09 |
[C#/WPF] Cursor 클래스 : 리소스 커서 로드하기 (0) | 2020.08.09 |
[C#/WPF] Mouse 클래스 : OverrideCursor 정적 속성을 사용해 커서 설정하기 (0) | 2020.08.09 |
[C#/WPF] Clipboard 클래스 : GetText 정적 메소드를 사용해 HTML 구하기 (0) | 2020.08.08 |
[C#/WPF] Thumb 엘리먼트 : DragStarted/DragDelta/DragCompleted 이벤트를 사용해 드래그 하기 (0) | 2020.08.08 |
[C#/WPF] Calendar 클래스 사용하기 (0) | 2020.08.08 |
[C#/WPF] Frame 클래스 : Navigate 메소드를 사용해 페이지 이동하기 (0) | 2020.08.08 |
[C#/WPF] Button 엘리먼트 : Background 속성 사용하기 (0) | 2020.08.08 |
[C#/WPF] BulletDecorator 엘리먼트 사용하기 (0) | 2020.08.08 |
댓글을 달아 주세요