Implementing Undo/Redo in Managed Ink Objects

I had some difficulty figuring this out. ALthough the ink can interact with the clipboard, I wanted to have an explicit place to tuck away my strokes and retrieve them again. A few hints in the Tablet Newsgroup pointed me in to some interesting methods and I was able to come up with the following solution. Take a look at the functionality of ink.CreateStrokes, ink.ExtractStrokes and ink.AddStrokestoRectangle to better understand this code.

(DeletedStrokes is just a ListArray created elsewhere in the class)

Case “undo”

With (InkPicture1.Ink)
Dim id As
Int32 = .Strokes(.Strokes.Count – 1).Id
Dim ids() As
Int32 = {id}
Dim delStrokes As
Strokes = .CreateStrokes(ids)
Dim storeInk As
Ink = .ExtractStrokes(delStrokes, ExtractFlags.RemoveFromOriginal)
DeletedStrokes.Add(storeInk)
.Strokes.Remove(.Strokes(.Strokes.Count – 1))
End
With
InkPicture1.Refresh()

Case “redo”
If DeletedStrokes.Count > 0
Then
Dim storeInk As
Ink = DeletedStrokes(DeletedStrokes.Count – 1)
InkPicture1.Ink.AddStrokesAtRectangle(storeInk.Strokes, thisink.GetBoundingBox)
DeletedStrokes.Remove(thisink)
InkPicture1.Refresh()
End If

  Sign up for my newsletter so you don't miss my conference & Pluralsight course announcements!  

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.