HitTest(Rectangle, Single) Method [InkDisp Class]

HitTest(Rectangle, Single) Method [InkDisp Class]

Retrieves the strokes that are contained within a specified rectangle.

Declaration

[C++]

HRESULT HitTestWithRectangle (
    [in] IInkRectangle* selectionRectangle,
    [in] float percentIntersect,
    [out, retval] IInkStrokes **Strokes
);

[Microsoft® Visual Basic® 6.0]

Public Function HitTestWithRectangle( _
    selectionRectangle As InkRectangle, _
    percentIntersect As Single _
) As InkStrokes

Parameters

selectionRectangle

[in] The selection rectangle, of type InkRectangle, in ink space coordinates.

percentIntersect

[in] The float or single percentage value that determines which strokes are included in the collection. Strokes that intersect the rectangle are included in the collection if the percentage of points in those strokes contained within the rectangle is greater than or equal to the percentIntersect percentage.

Strokes

[out] Returns the collection of strokes that makes up the ink.

Return Value

HRESULT value Description
S_OK Success.
E_POINTER A parameter contained an invalid pointer.
E_INK_EXCEPTION An exception occurred inside the method.
E_INVALIDARG Invalid display handle.

Remarks

To determine which points of a known stroke intersect the test area, call the GetRectangleIntersections method of the IInkStrokeDisp object, which retrieves the points where a stroke intersects a known rectangle.

Example

[Visual Basic 6.0]

This Visual Basic 6.0 example shows how to call HitTest(Rectangle, Single) using an InkRectangle, theHitRect, to select an InkStrokes collection from InkDisp, theInkCollector.Ink. The percentIntersect parameter is set to 50.0, so a stroke must have at least 50 percent of its points contained within the rectangle to be included in the collection. The example begins with a Standard EXE application, to which a reference to the Microsoft Tablet PC Type Library has been added. A check box, CheckHitTestMode, is added to the form, with a caption of Hit Test Mode. When the check box is cleared, strokes may be inked in the form. When the check box is set, the cursor will have a rectangle surrounding it, which will turn red if it contains more than 50% of a stroke, and the affected stroke will be drawn in blue.

Option Explicit
Dim mouseX As Long
Dim mouseY As Long
Dim inkX As Long
Dim inkY As Long
Dim inkRadius As Long
Dim theInkCollector As InkCollector

Private Sub CheckHitTestMode_Click()
    'Toggle the ink collection mode
    theInkCollector.Enabled = Not theInkCollector.Enabled
    theInkCollector.AutoRedraw = Not theInkCollector.AutoRedraw
    Refresh
End Sub

Private Sub Form_Load()
    'Set ScaleMode to pixels to make calculating easier
    Me.ScaleMode = vbPixels
    'Set up the InkCollector
    Set theInkCollector = New InkCollector
    theInkCollector.hWnd = Me.hWnd
    theInkCollector.Enabled = True
    'Preload the inkRadius to the inkspace equivalent of
    '20 pixels
    inkRadius = 20
    theInkCollector.Renderer.PixelToInkSpace Me.hDC, inkRadius, 0
    'Start with the hit test mode turned off
    CheckHitTestMode.Value = 0
End Sub

Private Sub Form_MouseMove( _
Button As Integer, Shift As Integer, X As Single, Y As Single)
    If CheckHitTestMode.Value = 1 Then
        mouseX = X
        mouseY = Y
        inkX = X
        inkY = Y
        theInkCollector.Renderer.PixelToInkSpace Me.hDC, inkX, inkY
        Refresh
    End If
End Sub

Private Sub Form_Paint()
    If CheckHitTestMode.Value = 1 Then
        'Create the hit test rectangle
        Dim theHitRect As New InkRectangle
        theHitRect.SetRectangle inkY - inkRadius, inkX - inkRadius, _
            inkY + inkRadius, inkX + inkRadius
        'Set the standard ink drawing attributes to black
        Dim theDrawingAttributes As New InkDrawingAttributes
        theDrawingAttributes.Color = vbBlack
        theInkCollector.Ink.Strokes.ModifyDrawingAttributes theDrawingAttributes
        'Get the collection of strokes which have at
        'least 50% of their points within the hit test rectangle
        Dim hitStrokes As InkStrokes
        Set hitStrokes = _           theInkCollector.Ink.HitTestWithRectangle(theHitRect, 50!)
        If hitStrokes.Count > 0 Then
            'Draw the hit test circle in red
            Me.ForeColor = vbRed
            'Draw the hit strokes in blue
            theDrawingAttributes.Color = vbBlue
            hitStrokes.ModifyDrawingAttributes theDrawingAttributes
        Else
            'Draw the hit test circle in black
            Me.ForeColor = vbBlack
        End If
        'Draw the strokes, and the hit test rectangle
        theInkCollector.Renderer.Draw Me.hDC, theInkCollector.Ink.Strokes
        Line (mouseX - 20, mouseY - 20)-Step(40, 40), , B
    End If
End Sub

Applies To