Previous Document Next Document

Understanding the CorelDRAW object model : Working with shapes : Selecting shapes


Selecting shapes

Each shape is a member of the Layer.Shapes collection for the layer on which it appears. The shapes in a Layer.Shapes collection appear in the order in which they appear on that layer — the first shape is the one at the top of the “stack,” and the last shape is the one at the bottom. If shapes are added, reordered, or deleted, the affected Layer.Shapes collection is immediately updated to reflect the new shape order of that layer.

 
In addition, each document page has a Shapes collection, which contains all Layer.Shapes collections for that page. The first shape in a Page.Shapes collection is the one at the very top of that page, and the last shape is the one at the very bottom.

If you want to access individual shapes, you can select them. When you select shapes, you create a “selection” that contains only those shapes.

The Shape.Selected property takes a Boolean value that indicates whether a shape is selected: True if the shape is selected, False otherwise. You can select a shape by changing the value of its Selected property to True; this technique adds the shape to the current selection — that is, rather than creating a new selection that contains only that shape.

If you want to create a new selection from a shape — that is, by selecting a specified shape and deselecting any other selected shapes — you can use the Shape.CreateSelection method, as in the following VBA example:

Dim sh As Shape
Set sh = ActivePage.Shapes(1)
If sh.Selected = False Then sh.CreateSelection

You can select multiple shapes by using the ShapeRange.CreateSelection method. The following VBA code uses this method — in combination with the Shapes.All method — to select all shapes on the active page (except those on locked or hidden layers):

ActivePage.Shapes.All.CreateSelection

You can access a selection in one of two ways:

 
Use the Document.Selection method to return a special Shape object that contains the actual selection. This Shape object is automatically refreshed when the selection is updated.
 
Use the Document.SelectionRange property to return a ShapeRange object that contains a copy of the selection. This ShapeRange object represents a “snapshot” of the selection (at the time when the ShapeRange object was created), so it is not automatically refreshed when the selection is updated.

To summarize, you can access a selection directly, or you can access a copy of that selection; alternatively, you can access a subset of the shapes in a selection. You can also reorder the shapes in a selection. When you no longer require a selection, you can deselect one or all of its shapes.

For more information on selecting shapes, see the following subtopics:

 
 
 
 
 
Accessing selections directly

As previously discussed, you can use the Document.Selection method to access the contents of a selection directly. A Shape object is returned, and this Shape object is updated to reflect any changes made to the selection.

The following VBA code returns the selection for the active document:

Dim sel As Shape
Set sel = ActiveDocument.Selection

The shortcut for ActiveDocument.Selection is ActiveSelection, which returns a Shape object of subtype cdrSelectionShape. The Shape subtype has a member collection called Shapes, which represents a collection of all the selected shapes in the document. The shapes in the ActiveSelection.Shapes collection can be accessed as in the following VBA example:

Dim sh As Shape, shs As Shapes
Set shs = ActiveSelection.Shapes
For Each sh In shs
sh.Rotate 15 'Rotate each shape by 15 degrees counterclockwise
Next sh

 
After you use the ActiveSelection command to select shapes, you cannot subsequently use the command to access those shapes. Instead, you must create a copy of the selection by using one of the following methods:
Recreate the selection as an array of Shape objects.
Recreate the selection as a Shapes collection.
Create a “snapshot” of the selection as a ShapeRange object (see Accessing copies of selections).
Accessing copies of selections

As previously discussed, you can use the Document.SelectionRange property to make a copy of the shapes in a selection. However, the returned ShapeRange object is not refreshed when the selection is updated because it represents a “snapshot” of the selection at the moment when that ShapeRange object was created.

The following VBA code returns a copy of the selection for the active document:

Dim selRange As ShapeRange
Set selRange = ActiveDocument.SelectionRange

The shortcut for the ActiveDocument.SelectionRange command is ActiveSelectionRange, which returns a ShapeRange object. This object contains a collection of references to the shapes that were selected at the moment when the property was invoked. The shapes in the ActiveSelectionRange collection can be accessed as in the following VBA example:

Dim sh As Shape, shRange As ShapeRange
Set shRange = ActiveSelectionRange
For Each sh In shRange
sh.Skew 15 ' Skew each shape thru 15° counterclockwise
Next sh

After you use the ActiveSelectionRange command to create a copy of the current document selection, you can subsequently access the returned ShapeRange object to access any of its shapes. You can even add shapes to or remove shapes from the returned ShapeRange object. You can then use the ShapeRange.CreateSelection method if you want to replace the current selection with the modified ShapeRange object.

The following VBA code creates a ShapeRange object from the current document selection, removes the first and second shapes from that shape range, and then replaces the original selection with this modified ShapeRange object:

Dim shRange As ShapeRange
Set shRange = ActiveSelectionRange
shRange.Remove 1
shRange.Remove 2
shRange.CreateSelection

 
If you want to add a specified ShapeRange object to the current selection (rather than use it to replace the current selection), you can use the ShapeRange.AddToSelection method.
Accessing the shapes in a selection

You can use a similar process for accessing the shapes in a selection as you can for accessing the shapes in a selection range.

Here is a a VBA code sample for accessing the shapes in a selection:

Dim shs As Shapes, sh As Shape
Set shs = ActiveSelection.Shapes
For Each sh In shs
' Do something with the shape, sh
Next sh

Here is a VBA code sample for accessing the shapes in a selection range:

Dim sRange As ShapeRange, sh As Shape
Set sRange = ActiveSelectionRange
For Each sh In sRange
' Do something with the shape, sh
Next sh

 
Remember that the ActiveSelection.Shapes command provides direct access to the current selection, while the ActiveSelectionRange command provides a copy of the current selection. Use ActiveSelection.Shapes if you want to access the current selection; use ActiveSelectionRange if you want to create a “snapshot” of the current selection that you can access later.
Reordering the shapes in a selection

The ActiveSelection.Shapes command and the ActiveSelectionRange command return shapes in the reverse order from which they were selected: the first shape is the last one selected, and the last shape is the first one selected. Please keep this fact in mind when reordering the shapes in a selection.

Deselecting shapes

You can deselect any shape by changing the value of its Shape.Selected property to False.

You can deselect all shapes by using the Document.ClearSelection method. The following VBA code uses the ClearSelection method to deselect all shapes in the active document:

ActiveDocument.ClearSelection

If you want, you can use event handlers to respond to events that are triggered by deseleting a shape:

 
Document.ShapeChange

You can also use event handlers to respond to events that are triggered by deactivating a selection:

 
Application.SelectionChange
 
Document.SelectionChange
 
GlobalMacroStorage.SelectionChange

Previous Document Next Document Back to Top

Copyright 2013 Corel Corporation. All rights reserved.