Partager via


Character Autocomplete Sample

Character Autocomplete Sample

Description of character Autocomplete sample for the Tablet PC.

The Autocomplete sample demonstrates how to implement character Autocomplete in Japanese by using the recognition application programming interfaces (APIs).

The following features are used in this sample:

  • Using an ink collector
  • Using a recognizer context

Initializing the Ink Collector and Recognizer Context

The InkCollector and InkRecognizerContext objects are declared as classes that can raise events.

Dim WithEvents ic As InkCollector
Dim WithEvents rc As InkRecognizerContext

The form's Load event handler creates an ink collector, associates the ink collector to picture box, and enables ink collection. The event handler then loads the default Japanese recognizer, and initializes the recognizer context's Guide and Strokes properties.

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

Loading the Default Japanese Recognizer

The GetDefaultRecognizer method of the InkRecognizers is called to retrieve the default recognizer for the Japanese language. Next, the IInkRecognizer object's Languages property is checked to determine if the recognizer supports the Japanese language. If it does, then the recognizer's CreateRecognizerContext method is used to generate a recognizer context for the form.

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

Handling the Stroke Event

The Stroke event handler first halts background recognition on the recognizer context. Then, it adds the new stroke to the recognizer context's Strokes property. Finally, it sets the recognizer context's InkRecognizerCharacterAutoCompletionMode property and calls the recognizer context's BackgroundRecognizeWithAlternates method for each of the three character Autocomplete modes. The CustomData parameter of the BackgroundRecognizeWithAlternates method call is used to identify which recognition results are returned in the RecognitionWithAlternates event.

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

Handling the Recognition with Alternates Event

The handler for the RecognitionWithAlternates event first checks the RecognitionStatus parameter. If there is an error in recognition, the event handler ignores the recognition results. Otherwise, the event handler adds the RecognitionResult parameter to the appropriate picture box and saves the result string. The CustomData parameter is set in the call to the BackgroundRecognizeWithAlternates method and identifies which character Autocomplete mode was used by the recognizer context.

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

Painting the Form

The Paint event handler clears the results picture boxes and adds the saved recognition results to them.

Deleting the Strokes

The form's CmdClear_Click method handles the Clear command. If the InkCollector is currently collecting ink, then a message box is displayed and the command is ignored. Otherwise, the event handler stops background recognition, clears the Strokes property of the recognizer context, and deletes the strokes from the form's InkDisp object. Then, the event handler redraws the picture box to which the ink collector is associated, and clears the recognition strings and picture boxes.

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