字元自動完成範例
自動完成範例示範如何使用辨識應用程式開發介面 (API) ,在日文中實作字元自動完成。
此範例會使用下列功能:
- 使用 筆跡收集器
- 使用 辨識器內容
初始化筆跡收集器和辨識器內容
InkCollector和InkRecognizerCoNtext物件會宣告為可引發事件的類別。
Dim WithEvents ic As InkCollector
Dim WithEvents rc As InkRecognizerContext
表單的 Load 事件處理常式會建立筆跡收集器、將筆跡收集器與圖片框產生關聯,以及啟用筆跡收集。 事件處理常式接著會載入預設的日文辨識器,並初始化辨識器內容的 Guide 和 Strokes 屬性。
Private Sub Form_Load()
' Set the ink collector to work in the small frame window
Set ic = New InkCollector ic.hWnd = fraBox.hWnd ic.Enabled = True
' Get the Japanese recognizer
LoadRecognizer
' Initialize the recognizer context
Dim Guide As New InkRecognizerGuide
Dim FrameRectangle As New InkRectangle
Dim Top As Long
Dim Bottom As Long
Dim Left As Long
Dim Right As Long
Top = 0
Left = 0
Bottom = fraBox.ScaleHeight
Right = fraBox.ScaleWidth
ic.Renderer.PixelToInkSpace Me.hdc, Top, Left
ic.Renderer.PixelToInkSpace Me.hdc, Bottom, Right
FrameRectangle.Bottom = Bottom
FrameRectangle.Top = Top
FrameRectangle.Left = Left
FrameRectangle.Right = Right
Guide.Columns = 1
Guide.Rows = 1
Guide.Midline = -1 ' Do not use midline
Guide.DrawnBox = FrameRectangle
Guide.WritingBox = FrameRectangle
Set rc.Guide = Guide
' Set the strokes collection on the recognizer context
Set ink = ic.ink Set rc.Strokes = ic.ink.Strokes
End Sub
載入預設日文辨識器
會呼叫 InkRecognizers的GetDefaultRecognizer方法來擷取日文語言的預設辨識器。 接下來,會檢查 IInkRecognizer 物件的 Languages 屬性,以判斷辨識器是否支援日文語言。 如果是,則會使用辨識器的 CreateRecognizerCoNtext 方法來產生表單的辨識器內容。
Private Sub LoadRecognizer()
On Error GoTo NoRecognizer
' Get a Japanese recognizer context
Dim recos As New InkRecognizers
Dim JapaneseReco As IInkRecognizer
Set JapaneseReco = recos.GetDefaultRecognizer(&H411)
If JapaneseReco Is Nothing Then
MsgBox "Japanese Recognizers are not installed on this system. Exiting."
End
End If
' Check that this is indeed a Japanese recognizer
Dim IsJapanese As Boolean
Dim lan As Integer
IsJapanese = False
For lan = LBound(JapaneseReco.Languages) To UBound(JapaneseReco.Languages)
If JapaneseReco.Languages(lan) = &H411 Then
IsJapanese = True
End If
Next lan
If Not IsJapanese Then
MsgBox "Japanese Recognizers are not installed on this system. Exiting."
End
End If
Set rc = JapaneseReco.CreateRecognizerContext
Exit Sub
NoRecognizer:
MsgBox "Japanese Recognizers are not installed on this system. Exiting."
End
End Sub
處理 Stroke 事件
Stroke事件處理常式會先停止辨識器內容上的背景辨識。 然後,它會將新的筆劃新增至辨識器內容的 Strokes 屬性。 最後,它會設定辨識器內容的 InkRecognizerCharacterAutoCompletionMode 屬性,並針對三個字元的 Autocomplete 模式呼叫辨識器內容的 BackgroundRecognizeWithAlternates 方法。 BackgroundRecognizeWithAlternates方法呼叫的CustomData參數是用來識別RecognitionWithAlternates事件中傳回的辨識結果。
Private Sub ic_Stroke(ByVal Cursor As MSINKAUTLib.IInkCursor, ByVal Stroke As MSINKAUTLib.IInkStrokeDisp, Cancel As Boolean)
' Stop the unfinished recognition processes
rc.StopBackgroundRecognition
' Add the new stroke
rc.Strokes.Add Stroke
' Get a result for all three CAC modes
rc.CharacterAutoCompletionMode = IRCACM_Full rc.BackgroundRecognizeWithAlternates 0 rc.CharacterAutoCompletionMode = IRCACM_Prefix rc.BackgroundRecognizeWithAlternates 1 rc.CharacterAutoCompletionMode = IRCACM_Random rc.BackgroundRecognizeWithAlternates 2
End Sub
使用替代事件處理辨識
RecognitionWithAlternates事件的處理常式會先檢查RecognitionStatus參數。 如果辨識發生錯誤,事件處理常式會忽略辨識結果。 否則,事件處理常式會將 RecognitionResult 參數新增至適當的圖片方塊,並儲存結果字串。 CustomData參數是在呼叫BackgroundRecognizeWithAlternates方法時設定,並識別辨識器內容所使用的字元自動完成模式。
Private Sub rc_RecognitionWithAlternates(ByVal RecognitionResult As MSINKAUTLib.IInkRecognitionResult, ByVal vCustomParam As Variant, ByVal RecognitionStatus As MSINKAUTLib.InkRecognitionStatus)
' Get the alternates from the recognition result
' and display them in the right place
Dim ResultString As String
Dim alts As IInkRecognitionAlternates
Dim alt As IInkRecognitionAlternate
On Error GoTo EndFunc
If RecognitionStatus = IRS_NoError Then
' Fill a string with all the characters for this CAC mode
Set alts = RecognitionResult.AlternatesFromSelection
For Each alt In alts
ResultString = ResultString + alt.String
Next alt
' Display the string
Dim r As RECT
r.Left = 0
r.Top = 0
r.Right = 1000
r.Bottom = 1000
PctResult(vCustomParam).Cls
DrawText PctResult(vCustomParam).hdc, StrPtr(ResultString), Len(ResultString), r, 0
If vCustomParam = 0 Then
FullCACText = ResultString
Else
If vCustomParam = 1 Then
PrefixCACText = ResultString
Else
If vCustomParam = 2 Then
RandomCACText = ResultString
End If
End If
End If
End If
Exit Sub
EndFunc:
MsgBox Err.Description
End Sub
繪製表單
Paint 事件處理常式會清除結果圖片方塊,並將儲存的辨識結果新增至其中。
刪除筆劃
表單的 CmdClear_Click
方法會處理 Clear 命令。 如果 InkCollector 目前正在收集筆跡,則會顯示訊息方塊並忽略命令。 否則,事件處理常式會停止背景辨識、清除辨識器內容的 Strokes 屬性,並從表單的 InkDisp 物件中刪除筆劃。 然後,事件處理常式會重新繪製筆跡收集器相關聯的圖片方塊,並清除辨識字串和圖片方塊。
If Not (ic.CollectingInk) Then
' Stop the unfinished recognition processes
rc.StopBackgroundRecognition
' ...
Set rc.Strokes = Nothing
' Delete all the strokes from the ink object
ic.Ink.DeleteStrokes strokesToDelete
' Refresh the window
fraBox.Refresh
' refresh the recognizer context
Set rc.Strokes = ic.Ink.Strokes
' Clear the result strings
FullCACText = ""
PrefixCACText = ""
RandomCACText = ""
' Clear the result windows
PctResult(0).Cls
PctResult(1).Cls
PctResult(2).Cls
Else
MsgBox "Cannot clear ink while the ink collector is busy."
End If