FujitsuStylistic.com

Objects

Along with the controls we have been using in this chapter, the SDK also provides several objects that we can use throughout our applications.
InkCollector Object The InkCollector object captures ink input that is placed into a known application window. The InkCollector object captures the input in real time and then directs it into an Ink object. The ink strokes can then be manipulated or sent to a recognizer for recognition.
InkOverlay Object The next object to look at is the InkOverlay object, which is a superset of the InkCollector object and provides editing support. Both objects, as does the InkPicture control, use common constructs, such as the Ink object and the InkDrawingAttributes collection, so that the basic way to change the color of ink is the same everywhere. This enables you to reuse code and makes it much easier for you to remember. InkOverlay is perfect for annotation in which pen size, ink, color, and position are the most important aspects for a program.
InkOverlay differs from InkCollector in several ways: It raises events for begin and end stroke, along with ink attribute changes. It enables users to select, erase, and resize ink. It supports Cut, Copy, and Paste commands. The process for using InkOverlay and InkCollector are very similar. Let's create a sample application. First, open VB .NET and create a new Windows Forms application. Next, add a label control to the form and resize it so that it takes up much of the form's visible area. Open the Code Editor and then add references to the following: Microsoft Tablet PC API The next step is to add a button to the form and just leave it with its given name of Button1. You can also leave the standard Text property of 'Button1.' Now, back in the Code Editor, we can use the Form_Load event to programmatically set some of these properties and create the InkCollector: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Label1.BackColor = Color.White Label1.Text = "" Button1.Text = "Recognize It" theInkCollector = New Microsoft.Ink.InkCollector(Label1.Handle) theInkCollector.Handle = Label1.Handle theInkCollector.Enabled = True End Sub The next step is to add the Button1_Click event, which we'll use to display a message box with the recognized text from our strokes. We'll use a simple Try and Catch to make sure we are executing this on a Tablet PC. This is good programming practice, but because we know that the applications we are developing in the majority of the examples are only going to run on a Tablet PC, we may skip this step. Here is the code: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim strokes As Microsoft.Ink.Strokes = theInkCollector.Ink.Strokes Try MsgBox(strokes.ToString(), MsgBoxStyle.OKOnly, "VB.NET API") Catch MsgBox("Not a Tablet PC", MsgBoxStyle.OKOnly, "Error in VB.NET API") End Try End Sub The last thing we need to do is use the Dispose method of the InkCollector to prevent memory leaks. We can use the Form1_Closing event as a place to handle this: Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing theInkCollector.Dispose() End Sub Selecting Ink You can now save and run the application. Enter some ink into the white area and then click the button that says 'Recognize It.' This should display the recognized text in a message similar to the one seen in Figure 13.7.

Now, if you want to change the InkCollector to an InkOverlay, it's as simple as changing these two lines: Dim WithEvents theInkCollector As Microsoft.Ink.InkCollector theInkCollector = New Microsoft.Ink.InkCollector(Label1.Handle) to read: Dim WithEvents theInkCollector As Microsoft.Ink.InkOverlay theInkCollector = New Microsoft.Ink.InkOverlay(Label1.Handle) After changing the code, you can check the application to make sure everything works as expected. Now, let's expand upon the application. The InkOverlay object enables users to use a Lasso tool to select Ink objects or they can select ink by tapping any Ink object. It also allows us to erase ink. Like InkPicture, we also need to have a way to get back to Drawing mode. Now, let's add three additional buttons to the application, leaving them as named by default (i.e., Button2, Button3, Button4). We now go into the Form_Load event and add the following three lines of code:
Button2.Text = "Erase" Button3.Text = "Draw" Button4.Text = "Selection" You're already familiar with the properties, so we can simply write the code for each of the events:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click theInkCollector.EraserMode = Microsoft.Ink.InkOverlayEraserMode.StrokeErase theInkCollector.EditingMode = Microsoft.Ink.InkOverlayEditingMode.Delete End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click theInkCollector.EditingMode = Microsoft.Ink.InkOverlayEditingMode.Ink End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click theInkCollector.EditingMode = Microsoft.Ink.InkOverlayEditingMode.Select End Sub