Export (0) Print
Expand All

How to: Drag and Drop Ink

The following example creates an application that enables the user to drag selected strokes from one InkCanvas to the other.

<Window x:Class="Window1"
    Title="InkDragDropSample" Height="500" Width="700"
      <RowDefinition Height="Auto"/>
      <RowDefinition Height="*"/>

    <InkCanvas Name="ic1" AllowDrop="True" 
               Grid.Column="0" Grid.Row="0" 
               Margin="10,10,10,10" Background="AliceBlue" 

    <InkCanvas Name="ic2" AllowDrop="True" 
               Grid.Column="1" Grid.Row="0" 
               Margin="10,10,10,10" Background="Beige" 

    <CheckBox Grid.Row="1" 
              Checked="switchToSelect" Unchecked="switchToInk">
      Select Mode
Imports System
Imports System.IO
Imports System.Windows
Imports System.Windows.Ink
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Input
Imports System.Windows.Media

Class Window1
    Inherits Window

    Public Sub New() 

    End Sub 'New 

    Private Sub InkCanvas_PreviewMouseDown(ByVal sender As Object, _
                                   ByVal e As MouseButtonEventArgs)

        Dim ic As InkCanvas = CType(sender, InkCanvas)

        Dim pt As Point = e.GetPosition(ic)

        ' If the user is moving selected strokes, prepare the strokes to be 
        ' moved to another InkCanvas. 
        If ic.HitTestSelection(pt) = InkCanvasSelectionHitResult.Selection Then 

            Dim selectedStrokes As StrokeCollection = _

            Dim strokesToMove As StrokeCollection = _

            ' Remove the offset of the selected strokes so they 
            ' are positioned when the strokes are dropped. 
            Dim inkBounds As Rect = strokesToMove.GetBounds()
            TranslateStrokes(strokesToMove, -inkBounds.X, -inkBounds.Y)

            ' Perform drag and drop. 
            Dim ms As New MemoryStream()

            Dim dataObject As New DataObject _
                (StrokeCollection.InkSerializedFormat, ms)

            Dim effects As DragDropEffects = _
                DragDrop.DoDragDrop(ic, dataObject, DragDropEffects.Move)

            If (effects And DragDropEffects.Move) = DragDropEffects.Move Then 

                ' Remove the selected strokes from the current InkCanvas.
            End If 
        End If 

    End Sub 'OnMouseDown

    Private Sub InkCanvas_Drop(ByVal sender As Object, _
                       ByVal e As DragEventArgs)

        ' Get the strokes that were moved. 
        Dim ic As InkCanvas = CType(sender, InkCanvas)
        Dim ms As MemoryStream = CType(e.Data.GetData( _
                  StrokeCollection.InkSerializedFormat),  _

        ms.Position = 0
        Dim strokes As New StrokeCollection(ms)

        ' Translate the strokes to the position at which 
        ' they were dropped. 
        Dim pt As Point = e.GetPosition(ic)
        TranslateStrokes(strokes, pt.X, pt.Y)

        ' Add the strokes to the InkCanvas and keep them selected.

    End Sub 'OnDrop

    ' Helper method that transletes the specified strokes. 
    Sub TranslateStrokes(ByVal strokes As StrokeCollection, _
                         ByVal x As Double, ByVal y As Double)

        Dim mat As New Matrix()
        mat.Translate(x, y)
        strokes.Transform(mat, False)

    End Sub 'TranslateStrokes

    Private Sub switchToSelect(ByVal sender As Object, _
                       ByVal e As RoutedEventArgs)

        ic1.EditingMode = InkCanvasEditingMode.Select
        ic2.EditingMode = InkCanvasEditingMode.Select 

    End Sub 'switchToSelect

    Private Sub switchToInk(ByVal sender As Object, _
                    ByVal e As RoutedEventArgs)

        ic1.EditingMode = InkCanvasEditingMode.Ink
        ic2.EditingMode = InkCanvasEditingMode.Ink

    End Sub 'switchToInk

End Class 'Window1
© 2014 Microsoft