Ink Recognition Sample

Ink Recognition Sample

Description of ink recognition sample for the Tablet PC.

This application demonstrates how you can build a handwriting recognition application.

Note: The Microsoft® Windows® XP Tablet PC Edition Software Development Kit (SDK) provides versions of this sample written in the following languages: C#, Microsoft Visual Basic® .NET, and Visual Basic 6.0. The version discussed in this section is Visual Basic .NET. The concepts are the same between versions.

Access the Tablet PC Interfaces

First, reference the Tablet PC interfaces, which are installed with the Tablet PC SDK.

' The Ink namespace, which contains the Tablet PC Platform API
Imports Microsoft.Ink

Initialize the InkCollector

There is code added to the form's Load Leave Site event handler that serves to associate the ink collector with the group box window and enable the ink collector.

Private Sub InkRecognition_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

   ' Create the recognizers collection
    myRecognizers = New Recognizers()

    ' Create an ink collector that uses the the group box handle
    myInkCollector = New InkCollector(gbInkArea.Handle)

    ' Turn the ink collector on
    myInkCollector.Enabled = True

End Sub

Recognize the Strokes

The Button Leave Site Click Leave Site handler checks to ensure that the user has at least one recognizer installed by examining the Count property of the Recognizers collection.

The SelectedText Leave Site property of the text box is set to the best match for the strokes using the ToString method on the Strokes collection. After the strokes have been recognized, they are deleted. Finally, the code forces a repaint of the drawing area to clear it for further use of ink.

Private Sub btnRecognize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRecognize.Click

    ' Check to ensure that the user has at least one recognizer installed
    ' Note that this is a preventive check - otherwise, an exception
    ' occurs during recognition
    If 0 = myRecognizers.Count Then
        MessageBox.Show("There are no handwriting recognizers installed.  You need to have at least one in order to run this sample.")
        ' ...
        txtResults.SelectedText = myInkCollector.Ink.Strokes.ToString

        ' If the mouse is pressed, do not perform the recognition -
        ' this prevents deleting a stroke that is still being drawn
        If Not myInkCollector.CollectingInk Then

            ' Delete the ink from the ink collector

            ' Force the Frame to redraw (so the deleted ink goes away)

        End If
    End If
End Sub

Closing the Form

The form's Dispose Leave Site method disposes the InkCollector object, myInkCollector.