다음을 통해 공유


GetTextRangeFromStrokes Method

GetTextRangeFromStrokes Method

Returns the smallest range of recognized text for which the recognizer can return an alternate that contains a known InkStrokes collection.

Declaration

[C++]

HRESULT GetTextRangeFromStrokes (
    [in] IInkStrokes* strokes,
    [in,out] long* selectionStart,
    [in,out] long* selectionLength
);

[Microsoft® Visual Basic® 6.0]

Public Sub GetTextRangeFromStrokes( _
    strokes As InkStrokes, _
    selectionStart As Long, _
    selectionLength As Long _
)

Parameters

strokes

[in] The collection of strokes for which to find the containing alternate.

selectionStart

[in, out] The start position of the range of recognized text within the alternate object on which this method was called that matches the smallest alternate that contains the passed-in strokes.

selectionLength

[in, out] The length of the text within the range of recognized text of the smallest alternate that contains the passed-in strokes.

Remarks

Use this method to retrieve the text that corresponds to a specified range of strokes. For example, consider a collection of strokes, "how are you", that was drawn using nine strokes (one for each letter and three for each word). If a collection that consists of the sixth and seventh strokes is passed in, corresponding to characters "e" and "y", the text range returned matches the alternate containing "are you" and the selection start and length matches this substring.

Example

[Visual Basic 6.0]

This Visual Basic 6.0 example demonstrates calling the GetTextRangeFromStrokes method on the TopAlternate property of an IInkRecognitionResult object, theRecognitionResult, passing in an InkStrokes collection from within the strokes that make up the recognition result in theStrokes. This example starts with a standard .exe application, adds a reference to the Microsoft Tablet PC Type Library, and has a text box Text1 and a check box HitTestMode added to the form. Ink is collected and recognized with the top alternate placed in the text box. When the hit test mode check box is checked, a mouse click calls HitTestCircle to find the containing collection of strokes with GetStrokesFromTextRange and colors it red, and finds the matching text range with GetTextRangeFromStrokes and selects it in the text box.

Option Explicit
Dim WithEvents theInkCollector As InkCollector
Dim WithEvents theRecognizerContext As InkRecognizerContext
Dim theRecognitionResult As IInkRecognitionResult
Dim hasRecoResult As Boolean
Dim theStrokes As InkStrokes

Private Sub HitTestMode_Click()
    theInkCollector.Enabled = Not theInkCollector.Enabled
End Sub

Private Sub Form_Load()
    ScaleMode = vbPixels
    'Initialize the InkCollector
    Set theInkCollector = New InkCollector
    theInkCollector.hWnd = Me.hWnd
    theInkCollector.Enabled = True
    'Create new RecognizerContext
    Dim theRecognizers As New InkRecognizers
    Dim theRecognizer As IInkRecognizer
    Set theRecognizer = theRecognizers.GetDefaultRecognizer
    Set theRecognizerContext = theRecognizer.CreateRecognizerContext
    'Initialize the recognizer's strokes
    'and assign them to the RecognizerContext
    Set theStrokes = theInkCollector.Ink.Strokes
    Set theRecognizerContext.Strokes = theStrokes
    'Clear the hit test check box
    HitTestMode.Value = 0
    hasRecoResult = False
End Sub

Private Sub Form_MouseDown( _
Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim inkX As Long
    Dim inkY As Long
    Dim hitStrokes As InkStrokes
    Dim AltStrokes As InkStrokes
    ' Convert the mouse down to ink space coordinates
    If hasRecoResult And HitTestMode.Value = 1 Then
        inkX = CLng(X)
        inkY = CLng(Y)
        theInkCollector.Renderer.PixelToInkSpace Form1.hDC, inkX, inkY
        'Get the stroke set hit by the mouse (within a radius
        'of 200 HIMETRIC) and color it red.
        Set hitStrokes = theInkCollector.Ink.HitTestCircle(inkX, inkY, 200)
        If hitStrokes.Count > 0 Then
            Set AltStrokes = _
            theRecognitionResult.TopAlternate.GetStrokesFromStrokeRanges(hitStrokes)
            Dim theStroke As IInkStrokeDisp
            For Each theStroke In theInkCollector.Ink.Strokes
                theStroke.DrawingAttributes.Color = RGB(0, 0, 0)
            Next
            For Each theStroke In AltStrokes
                theStroke.DrawingAttributes.Color = RGB(255, 0, 0)
            Next
            Dim SelStart As Long
            Dim SelLength As Long
            theRecognitionResult.TopAlternate.GetTextRangeFromStrokes _
                hitStrokes, SelStart, SelLength
            Text1.SelStart = SelStart
            Text1.SelLength = SelLength
            Text1.SetFocus
            Form1.Refresh
        End If
    End If
End Sub

Private Sub theRecognizerContext_RecognitionWithAlternates( _
ByVal RecognitionResult As IInkRecognitionResult, _
ByVal CustomData As Variant, _
ByVal RecognitionStatus As MSINKAUTLib.InkRecognitionStatus)
    Set theRecognitionResult = RecognitionResult
    'Update the text box with the top result
    Text1.Text = theRecognitionResult.TopString
    hasRecoResult = True
End Sub

Private Sub theInkCollector_Stroke( _
ByVal Cursor As MSINKAUTLib.IInkCursor, _
ByVal Stroke As MSINKAUTLib.IInkStrokeDisp, _
Cancel As Boolean)
    'When a new stroke is collected, add it to
    'the RecognizerContext's strokes collection
    theStrokes.Add Stroke
    'Tell the RecognizerContext to recognize
    theRecognizerContext.BackgroundRecognizeWithAlternates
End Sub

Applies To