Ink.HitTest Method

Ink.HitTest Method

Returns the Strokes collection contained within a known Rectangle Leave Site.

Definition

Visual Basic .NET Public Function HitTest( _
ByVal selectionRectangle As Rectangle, _
ByVal percentIntersect As Single _
) As Strokes
C# public Strokes HitTest(
Rectangle selectionRectangle,
float percentIntersect
);
Managed C++ public: Strokes* HitTest(
Rectangle *selectionRectangle,
float *percentIntersect
);

Parameters

selectionRectangle System.Drawing.Rectangle. The selection rectangle, in ink space coordinates.
percentIntersect System.Single. The percentage value that determines which Stroke objects are included in the Strokes collection. Stroke objects that intersect the rectangle are included in the Strokes collection if the percentage of points in those Stroke objects contained within the rectangle is greater than or equal to the percentage passed in to the percentIntersect parameter.

Return Value

Microsoft.Ink.Strokes. Returns the Strokes collection contained within the specified area.

Exceptions

ObjectDisposedException Leave Site: The Ink object is disposed.

Remarks

To determine which points of a known Stroke object intersect the hit test area, call the Stroke.GetRectangleIntersections method, which returns the points where a Stroke object intersects a known Rectangle Leave Site.

Examples

[C#]

This C# example uses the HitTest method in an application that has two modes: ink mode and hit-test mode. The user chooses the mode by selecting a check box, cbHitTestMode. When NewPackets events are generated by the pen or mouse in hit-test mode, the event handler calls the HitTest method with a rectangle that is centered on the current cursor location. If the hit test succeeds, the status bar contains the string "HitTest is true". If the hit test fails the status bar contains the string "HitTest is false".

using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.Ink;

class HitTestRectangleForm : System.Windows.Forms.Form
{
    private CheckBox cbHitTestMode;
    private StatusBar sbStatusBar;
    InkCollector theInkCollector;
    Ink inkClone;
    int indexX, indexY;
    int theInkSpaceRadius = 500;
    int thePixelRadius;
    Point ptInkSpaceHit, ptPixelHit;
    float theIntersectionPercentage = 5.5f;

    public HitTestRectangleForm()
    {
        cbHitTestMode = new CheckBox();
        sbStatusBar = new StatusBar();
        SuspendLayout();
        sbStatusBar.Size = new Size(292,20);
        sbStatusBar.Location = new Point(0,266);
        sbStatusBar.Text = "Status";
        cbHitTestMode.Location = new Point(8, 8);
        cbHitTestMode.Size = new Size(128, 24);
        cbHitTestMode.Text = "Hit Test Mode";
        cbHitTestMode.CheckedChanged +=
            new EventHandler(this.cbHitTestMode_CheckedChanged);

        ClientSize = new Size(292, 266);
        Controls.AddRange(new Control[] {cbHitTestMode, sbStatusBar});
        Text = "HitTest Rectangle";
        Load += new EventHandler(HitTestRectangleForm_Load);
        ResumeLayout(false);
    }

    public static void Main()
    {
        Application.Run(new HitTestRectangleForm());
    }

    private void HitTestRectangleForm_Load(
        object sender, System.EventArgs e)
    {
        theInkCollector = new InkCollector(Handle);
        theInkCollector.Enabled = true;

        theInkCollector.Stroke +=
            new InkCollectorStrokeEventHandler(Stroke_Event);
        theInkCollector.NewPackets +=
            new InkCollectorNewPacketsEventHandler(NewPackets_Event);

        GetXYIndexes(ref indexX, ref indexY);
        Point ptPixelRadius = new Point(theInkSpaceRadius, 0);
        theInkCollector.Renderer.InkSpaceToPixel(CreateGraphics(), ref ptPixelRadius);
        thePixelRadius = ptPixelRadius.X;
    }

    private void Stroke_Event(
        object sender, InkCollectorStrokeEventArgs e)
    {
        // Do not collect strokes while in hit test mode.
        if (cbHitTestMode.Checked)
        {
            e.Cancel = true;
            Refresh();
        }
    }

    private void NewPackets_Event(
        object sender, InkCollectorNewPacketsEventArgs e)
    {
        if (cbHitTestMode.Checked)
        {
            ptPixelHit = ptInkSpaceHit =
                new Point(e.PacketData[indexX], e.PacketData[indexY]);
            Rectangle rect = new Rectangle(ptInkSpaceHit.X - theInkSpaceRadius,
                ptInkSpaceHit.Y - theInkSpaceRadius,
                theInkSpaceRadius * 2, theInkSpaceRadius * 2);

            Strokes strokes = inkClone.HitTest(rect,theIntersectionPercentage);
            if (strokes.Count > 0)
            {
                sbStatusBar.Text = "HitTest is true.";
            }
            else
            {
                sbStatusBar.Text = "HitTest is false.";
            }
            Refresh();
        }
    }

    private void cbHitTestMode_CheckedChanged(
        object sender, System.EventArgs e)
    {
        // Do not do auto redraw if we are in hit test mode.
        theInkCollector.AutoRedraw = !cbHitTestMode.Checked;
        inkClone = theInkCollector.Ink.Clone();
        if (!cbHitTestMode.Checked)
            sbStatusBar.Text = "Status";

        Refresh();
    }

    // Get the indexes of the X and Y data within the raw
    // packet data array.
    private void GetXYIndexes(ref int theXIndex, ref int theYIndex)
    {
        Guid [] theGuids = theInkCollector.DesiredPacketDescription;

        for (int i = 0; i < theGuids.Length; i++)
        {
            if (theGuids[i].Equals(PacketProperty.X))
                theXIndex = i;
            if (theGuids[i].Equals(PacketProperty.Y))
                theYIndex = i;
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        if (cbHitTestMode.Checked)
        {
            theInkCollector.Renderer.Draw(g, inkClone.Strokes);
        }

        g.Dispose();
        base.OnPaint(e);
    }

    // Event handler for the form's closed event
    private void HitTestRectangleForm_Closed(object sender, System.EventArgs e)
    {
        theInkCollector.Dispose();
        theInkCollector = null;
    }
}

[Visual Basic .NET]

This Microsoft® Visual Basic® .NET example uses the HitTest method in an application that has two modes: ink mode and hit-test mode. The user chooses the mode by selecting a check box, cbHitTestMode. When NewPackets events are generated by the pen or mouse in hit-test mode, the event handler calls the HitTest method with a rectangle that is centered on the current cursor location. If the hit test succeeds, the status bar contains the string "HitTest is true". If the hit test fails the status bar contains the string "HitTest is false".

Imports System.Drawing
Imports System.Windows.Forms
Imports Microsoft.Ink
Public Class HitTestRectangleForm
    Inherits System.Windows.Forms.Form

    Private cbHitTestMode As CheckBox
    Private sbStatusBar As StatusBar
    Dim theInkCollector As InkCollector
    Dim inkClone As Ink
    Dim indexX, indexY As Integer
    Dim theInkSpaceRadius As Integer = 500
    Dim thePixelRadius As Integer
    Dim ptInkSpaceHit, ptPixelHit As Point
    Dim isTestingForHit As Boolean = False
    'The rectangle must contain at least this percentage
    'of a stroke to register a positive hit test:
    Dim theIntersectionPercentage As Single = 5.5

    Public Sub New()
        MyBase.New()
        cbHitTestMode = New CheckBox()
        sbStatusBar = New StatusBar()
        SuspendLayout()
        sbStatusBar.Location = New Point(0, 266)
        sbStatusBar.Size = New Size(292, 20)
        sbStatusBar.Text = "Status"
        cbHitTestMode.Location = New Point(8, 8)
        cbHitTestMode.Size = New Size(128, 24)
        cbHitTestMode.Text = "Hit Test Mode"
        AddHandler cbHitTestMode.CheckedChanged, _
            AddressOf cbHitTestMode_CheckedChanged

        ClientSize = New Size(292, 266)
        Controls.AddRange(New Control() {cbHitTestMode, sbStatusBar})
        Text = "HitTest Rectangle"
        AddHandler Load, AddressOf HitTestRectangleForm_Load
        AddHandler Paint, AddressOf HitTestRectangleForm_Paint
        ResumeLayout(False)

    End Sub

    Public Shared Sub Main()
        Application.Run(New HitTestRectangleForm())
    End Sub

    Private Sub HitTestRectangleForm_Load( _
    ByVal sender As Object, ByVal e As System.EventArgs)
        theInkCollector = New InkCollector(Handle)
        theInkCollector.Enabled = True

        AddHandler theInkCollector.Stroke, AddressOf Stroke_Event
        AddHandler theInkCollector.NewPackets, AddressOf NewPackets_Event

        GetXYIndexes(indexX, indexY)
        Dim ptPixelRadius As New Point(theInkSpaceRadius, 0)
        theInkCollector.Renderer.InkSpaceToPixel(CreateGraphics(), ptPixelRadius)
        thePixelRadius = ptPixelRadius.X
    End Sub

    Private Sub Stroke_Event( _
    ByVal sender As Object, ByVal e As InkCollectorStrokeEventArgs)
        'Do not collect strokes while in hit test mode.
        If cbHitTestMode.Checked Then
            e.Cancel = True
            Refresh()
        End If
    End Sub

    Private Sub NewPackets_Event( _
    ByVal sender As Object, ByVal e As InkCollectorNewPacketsEventArgs)
        Dim rect As Rectangle
        Dim colStrokes As Strokes
        If cbHitTestMode.Checked Then
            ptPixelHit = _
                 New Point(e.PacketData(indexX), e.PacketData(indexY))
            ptInkSpaceHit = ptPixelHit
            rect = New Rectangle(ptInkSpaceHit.X - theInkSpaceRadius, _
            ptInkSpaceHit.Y - theInkSpaceRadius, _
            theInkSpaceRadius * 2, theInkSpaceRadius * 2)

            colStrokes = inkClone.HitTest(rect, theIntersectionPercentage)
            If colStrokes.Count > 0 Then
                sbStatusBar.Text = "HitTest is True"
            Else
                sbStatusBar.Text = "HitTest is False"
            End If
            Refresh()
        End If
    End Sub

    Private Sub cbHitTestMode_CheckedChanged( _
    ByVal sender As Object, ByVal e As System.EventArgs)
        'Do not do auto redraw if we are in hit test mode.
        theInkCollector.AutoRedraw = Not cbHitTestMode.Checked
        If Not cbHitTestMode.Checked Then sbStatusBar.Text = "Status"
        inkClone = theInkCollector.Ink.Clone()
        Refresh()
    End Sub

    'Get the indexes of the X and Y data within the raw
    'packet data array.
    Private Sub GetXYIndexes( _
    ByRef theXIndex As Integer, ByRef theYIndex As Integer)
        Dim theGuids() As Guid = theInkCollector.DesiredPacketDescription
        Dim index As Integer = 0
        For index = 0 To theGuids.Length - 1
            If theGuids(index).Equals(PacketProperty.X) Then
                theXIndex = index
            End If
            If theGuids(index).Equals(PacketProperty.Y) Then
                theYIndex = index
            End If
        Next
    End Sub

    Private Sub HitTestRectangleForm_Paint( _
    ByVal sender As Object, _
    ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim g As Graphics = e.Graphics
        If cbHitTestMode.Checked Then
            theInkCollector.Renderer.Draw(g, inkClone.Strokes)
        End If
        g.Dispose()
    End Sub

    'Event handler for the form's closed event
    Private Sub HitTestRectangleForm_Closed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Closed
        theInkCollector.Dispose()
        Set theInkCollector = Nothing
    End Sub
End Class

See Also