Share via


InkPicture.Selection Property

InkPicture.Selection Property

Gets or sets the Strokes collection that is currently selected inside the InkPicture control.

Definition

Visual Basic .NET Public Property Selection As Strokes
C# public Strokes Selection { get; set; }
Managed C++ public: __property Strokes* get_Selection();
public: __property void set_Selection(Strokes*);

Property Value

Microsoft.Ink.Strokes. The Strokes collection that is currently selected inside the InkPicture control. The default value is an empty Strokes collection.

This property is read/write. This property has no default value.

Exceptions

COMException Leave Site:
ObjectDisposedException Leave Site:

Remarks

To get the bounding rectangle of the Strokes collection after it has been moved or resized, call the GetBoundingBox method of the Strokes collection returned by this property.

To get the bounding rectangle of of the Strokes collection before it was moved, handle the SelectionMoved event and get the OldSelectionBoundingRect property of the InkOverlaySelectionMovedEventArgs object.

To get the bounding rectangle of of the Strokes collection before it was resized, handle the SelectionResized event and get the OldSelectionBoundingRect property of the InkOverlaySelectionResizedEventArgs object.

Examples

[C#]

This C# example demonstrates use of the Selection property and using the HitTestSelection method. This application uses a check box, theCheckBox, to toggle an InkOverlay object between inking and selection modes, and uses the HitTestSelection results to change the color of the selected strokes from the Selection property when one of the selection sizing handles of the selection rectangle is clicked.

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

namespace theApplication
{
    public class Form1: System.Windows.Forms.Form
    {
        private Microsoft.Ink.InkPicture theInkPicture;
        private System.Windows.Forms.CheckBox theCheckBox;

        public Form1()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
            this.theCheckBox = new System.Windows.Forms.CheckBox();
            this.theInkPicture = new Microsoft.Ink.InkPicture();
            this.SuspendLayout();
            //
            // theCheckBox
            //
            this.theCheckBox.Name = "theCheckBox";
            this.theCheckBox.TabIndex = 0;
            this.theCheckBox.Text = "Selection Mode";
            this.theCheckBox.CheckedChanged += new System.EventHandler(this.theCheckBox_CheckedChanged);
            //
            // theInkPicture
            //
            this.theInkPicture.Image = ((System.Drawing.Bitmap)(resources.GetObject("theInkPicture.Image")));
            this.theInkPicture.Location = new System.Drawing.Point(32, 32);
            this.theInkPicture.MarginX = -2147483648;
            this.theInkPicture.MarginY = -2147483648;
            this.theInkPicture.Name = "theInkPicture";
            this.theInkPicture.Size = new System.Drawing.Size(368, 320);
            this.theInkPicture.TabIndex = 0;
            this.theInkPicture.MouseDown += new System.Windows.Forms.MouseEventHandler(this.theInkPicture_MouseDown);
            //
            // Form1
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(416, 366);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.theCheckBox,
                                                                          this.theInkPicture});
            this.Name = "Form1";
            this.Text = "HitTestSelection";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

        }

        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

        // Create add the mouse down event handler and enable the ink
        private void Form1_Load(object sender, System.EventArgs e)
        {
            theInkPicture.InkEnabled = true;
        }

        // Handle the mouse down event for the overlay
        private void theInkPicture_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (theInkPicture.EditingMode == InkOverlayEditingMode.Select)
            {
                SelectionHitResult result = theInkPicture.HitTestSelection(e.X, e.Y);
                DrawingAttributes theDrawingAttributes = new DrawingAttributes();
                if (result == SelectionHitResult.Northwest)
                {
                    theDrawingAttributes.Color = System.Drawing.Color.Green;
                }
                else if (result == SelectionHitResult.Northeast)
                {
                    theDrawingAttributes.Color = System.Drawing.Color.Red;
                }
                else if (result == SelectionHitResult.Southeast)
                {
                    theDrawingAttributes.Color = System.Drawing.Color.Yellow;
                }
                else if (result == SelectionHitResult.Southwest)
                {
                    theDrawingAttributes.Color = System.Drawing.Color.Blue;
                }
                theInkPicture.Selection.ModifyDrawingAttributes(theDrawingAttributes);
                this.Refresh();
            }
        }

        private void theCheckBox_CheckedChanged(object sender, System.EventArgs e)
        {
            // Can't change EditingMode while the overlay is collecting ink
            if (true == theInkPicture.CollectingInk)
            {
                theCheckBox.Checked = !theCheckBox.Checked;
                return;
            }

            // Toggle the EditingMode between Inking and Selection
            if (theInkPicture.EditingMode == InkOverlayEditingMode.Ink)
            {
                theInkPicture.EditingMode = InkOverlayEditingMode.Select;
            }
            else
            {
                theInkPicture.EditingMode = InkOverlayEditingMode.Ink;
            }
        }
    }
}

[VB.NET]

This Microsoft® Visual Basic® .NET example demonstrates use of the Selection property and using the HitTestSelection method. This application uses a check box, theCheckBox, to toggle an InkOverlay object between inking and selection modes, and uses the HitTestSelection results to change the color of the selected strokes from the Selection property when one of the selection sizing handles of the selection rectangle is clicked.

Option Explicit On

Imports System
Imports System.Windows.Forms
Imports Microsoft.Ink

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private WithEvents theInkPicture As Microsoft.Ink.InkPicture
    Private WithEvents theCheckBox As System.Windows.Forms.CheckBox

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
        Me.theCheckBox = New System.Windows.Forms.CheckBox()
        Me.theInkPicture = New Microsoft.Ink.InkPicture()
        Me.SuspendLayout()
        '
        'theCheckBox
        '
        Me.theCheckBox.Name = "theCheckBox"
        Me.theCheckBox.TabIndex = 0
        Me.theCheckBox.Text = "Selection Mode"
        '
        'theInkPicture
        '
        Me.theInkPicture.Image = CType(resources.GetObject("theInkPicture.Image"), System.Drawing.Bitmap)
        Me.theInkPicture.Location = New System.Drawing.Point(30, 30)
        Me.theInkPicture.MarginX = -2147483648
        Me.theInkPicture.MarginY = -2147483648
        Me.theInkPicture.Name = "theInkPicture"
        Me.theInkPicture.Size = New System.Drawing.Size(362, 354)
        Me.theInkPicture.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
        Me.theInkPicture.TabIndex = 0
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(440, 398)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.theInkPicture, Me.theCheckBox})
        Me.Name = "Form1"
        Me.Text = "HitTestSelection"
        Me.ResumeLayout(False)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles MyBase.Load
        ' Using an Attach Mode of behind lets you check the check box instead of inking on it.
        theInkPicture.InkEnabled = True
    End Sub

    Private Sub theInkPicture_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles theInkPicture.MouseDown
        If theInkPicture.EditingMode = InkOverlayEditingMode.Select Then
            Dim result As SelectionHitResult
            result = theInkPicture.HitTestSelection(e.X, e.Y)
            Dim theDrawingAttributes As New DrawingAttributes()
            If result = SelectionHitResult.Northwest Then
                theDrawingAttributes.Color = Color.Green
            ElseIf result = SelectionHitResult.Northeast Then
                theDrawingAttributes.Color = Color.Red
            ElseIf result = SelectionHitResult.Southeast Then
                theDrawingAttributes.Color = Color.Yellow
            ElseIf result = SelectionHitResult.Southwest Then
                theDrawingAttributes.Color = Color.Blue
            End If
            theInkPicture.Selection.ModifyDrawingAttributes(theDrawingAttributes)
            Refresh()
        End If
    End Sub

    Private Sub theCheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles theCheckBox.CheckedChanged
        ' Can't change EditingMode while the overlay is collecting ink
        If theInkPicture.CollectingInk Then
            theCheckBox.Checked = Not theCheckBox.Checked
        End If

        'Toggle the EditingMode between Inking and Selection
        If theInkPicture.EditingMode = InkOverlayEditingMode.Ink Then
            theInkPicture.EditingMode = InkOverlayEditingMode.Select
        Else
            theInkPicture.EditingMode = InkOverlayEditingMode.Ink
        End If
    End Sub

End Class
        

See Also