■ InkCanvas 클래스에서 잉크 드래그 & 드롭을 사용하는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<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="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Margin="10"> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="10" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="10" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Border Grid.Row="0" Grid.Column="0" BorderThickness="1" BorderBrush="Black"> <InkCanvas Name="inkCanvas1" Background="AliceBlue" AllowDrop="True" /> </Border> <Border Grid.Row="0" Grid.Column="2" BorderThickness="1" BorderBrush="Black"> <InkCanvas Name="inkCanvas2" Background="Beige" AllowDrop="True" /> </Border> <CheckBox Name="selectionModeCheckBox" Grid.Row="2" Padding="10" VerticalContentAlignment="Center" Content="선택 모드" /> </Grid> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.inkCanvas1.PreviewMouseDown += inkCanvas_PreviewMouseDown; this.inkCanvas1.Drop += inkCanvas_Drop; this.inkCanvas2.PreviewMouseDown += inkCanvas_PreviewMouseDown; this.inkCanvas2.Drop += inkCanvas_Drop; this.selectionModeCheckBox.Checked += selectionModeCheckBox_Checked; this.selectionModeCheckBox.Unchecked += selectionModeCheckBox_Unchecked; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 잉크 캔버스 프리뷰 마우스 DOWN 처리하기 - inkCanvas_PreviewMouseDown(sender, e) /// <summary> /// 잉크 캔버스 프리뷰 마우스 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void inkCanvas_PreviewMouseDown(object sender, MouseEventArgs e) { InkCanvas inkCanvas = sender as InkCanvas; Point point = e.GetPosition(inkCanvas); if(inkCanvas.HitTestSelection(point) == InkCanvasSelectionHitResult.Selection) { StrokeCollection selectedStrokeCollection = inkCanvas.GetSelectedStrokes(); StrokeCollection strokeCollectionToMove = selectedStrokeCollection.Clone(); Rect inkBoundRectangle = strokeCollectionToMove.GetBounds(); TranslateStrokeCollection(strokeCollectionToMove, -inkBoundRectangle.X, -inkBoundRectangle.Y); MemoryStream memoryStream = new MemoryStream(); strokeCollectionToMove.Save(memoryStream); DataObject dataObject = new DataObject(StrokeCollection.InkSerializedFormat, memoryStream); DragDropEffects effects = DragDrop.DoDragDrop(inkCanvas, dataObject, DragDropEffects.Move); if((effects & DragDropEffects.Move) == DragDropEffects.Move) { inkCanvas.Strokes.Remove(selectedStrokeCollection); } } } #endregion #region 잉크 캔버스 DROP 처리하기 - inkCanvas_Drop(sender, e) /// <summary> /// 잉크 캔버스 DROP 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void inkCanvas_Drop(object sender, DragEventArgs e) { InkCanvas inkCanvas = sender as InkCanvas; MemoryStream memoryStream = (MemoryStream)e.Data.GetData(StrokeCollection.InkSerializedFormat); memoryStream.Position = 0; StrokeCollection strokeCollection = new StrokeCollection(memoryStream); Point point = e.GetPosition(inkCanvas); TranslateStrokeCollection(strokeCollection, point.X, point.Y); inkCanvas.Strokes.Add(strokeCollection); inkCanvas.Select(strokeCollection); } #endregion #region 선택 모드 체크 박스 체크시 처리하기 - selectionModeCheckBox_Checked(sender, e) /// <summary> /// 선택 모드 체크 박스 체크시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void selectionModeCheckBox_Checked(object sender, RoutedEventArgs e) { this.inkCanvas1.EditingMode = InkCanvasEditingMode.Select; this.inkCanvas2.EditingMode = InkCanvasEditingMode.Select; } #endregion #region 선택 모드 체크 박스 체크 해제시 처리하기 - selectionModeCheckBox_Unchecked(sender, e) /// <summary> /// 선택 모드 체크 박스 체크 해제시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void selectionModeCheckBox_Unchecked(object sender, RoutedEventArgs e) { this.inkCanvas1.EditingMode = InkCanvasEditingMode.Ink; this.inkCanvas2.EditingMode = InkCanvasEditingMode.Ink; } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 스트로크 컬렉션 이동하기 - TranslateStrokeCollection(strokeCollection, x, y) /// <summary> /// 스트로크 컬렉션 이동하기 /// </summary> /// <param name="strokeCollection">스트로크 컬렉션</param> /// <param name="x">X</param> /// <param name="y">Y</param> private void TranslateStrokeCollection(StrokeCollection strokeCollection, double x, double y) { Matrix matrix = new Matrix(); matrix.Translate(x, y); strokeCollection.Transform(matrix, false); } #endregion } } |