筆跡辨識範例
此應用程式示範如何建置手寫辨識應用程式。Windows Vista SDK 也提供 C# 和 Visual Basic .NET 中此範例的版本。 本主題參考 Visual Basic .NET 範例,但版本之間的概念相同。
存取平板電腦介面
首先,參考隨 SDK 一起安裝的平板電腦 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事件處理常式會檢查辨識器集合的Count屬性,以確保使用者至少已安裝一個辨識器。
文字方塊的SelectedText屬性會設定為在Strokes集合上使用ToString方法比對筆劃的最佳比對。 辨識筆劃之後,就會刪除它們。 最後,程式碼會強制繪圖區域重新繪製,清除以進一步使用筆跡。
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 物件。