墨迹识别示例

此应用程序演示如何生成手写识别应用程序。Windows Vista SDK 也以 C# 和 Visual Basic .NET 提供此示例的版本。 本主题引用了 Visual Basic .NET 示例,但各版本的概念是相同的。

访问平板电脑接口

首先,参考随 SDK 一起安装的 Tablet PC API。

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

初始化 InkCollector

该示例将代码添加到表单的 Load 事件处理程序中,该处理程序用于将 InkCollector myInkCollector 与分组框窗口相关联并启用 InkCollector。

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 group box handle
    myInkCollector = New InkCollector(gbInkArea.Handle)

    ' Turn the ink collector on
    myInkCollector.Enabled = True

End Sub

识别笔划

Button 对象的 Click 事件处理程序检查以确保用户至少安装了一个识别器,方法是检查 Recognizers 集合的 Count 属性。

使用 Strokes 集合上的 ToString 方法,文本框的 SelectedText 属性设置为笔划的最佳匹配项。 识别笔划后,将删除它们。 最后,代码强制重新绘制绘图区域,将其清除以供进一步墨迹使用。

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.")
    Else
        ' ...
        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
            myInkCollector.Ink.DeleteStrokes(myInkCollector.Ink.Strokes)

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

        End If
    End If
End Sub

关闭窗体

窗体的 Dispose 方法释放 InkCollector 对象。