Previous Document Next Document

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


Transforming shapes

You can transform shapes in various ways, as explained in the following topics:

 
 
 
 
 

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

 
Document.ShapeTransform
Sizing shapes

You can return the width and height of a shape (in document units) by using the Shape.SizeWidth and Shape.SizeHeight properties, as in the following VBA example:

Dim width As Double, height As Double
ActiveDocument.Unit = cdrMillimeter
width = ActiveShape.SizeWidth
height = ActiveShape.SizeHeight

You can also use the Shape.SizeWidth and Shape.SizeHeight properties to resize an existing shape by specifying new values for those properties. The following VBA example uses these properties to set the size of the active shape to a width of 50 millimeters and a height of 70 millimeters:

ActiveDocument.Unit = cdrMillimeter
ActiveShape.SizeWidth = 50
ActiveShape.SizeHeight = 70

You can return both the width and the height of a shape (in document units) by using the Shape.GetSize method, as in the following VBA example:

Dim width As Double, height As Double
ActiveDocument.Unit = cdrMillimeter
ActiveShape.GetSize width, height

You can resize a shape by using the Shape.SetSize method to specify a new width and new height for it, as in the following VBA example:

ActiveDocument.Unit = cdrMillimeter
ActiveShape.SetSize 50, 70

You can also resize a shape by using the Shape.SetSizeEx method. Besides the new width and new height for the shape, this method takes a reference point for the resize (instead of using the center point of the shape). The following VBA code uses the SetSizeEx method to resize the current selection to 10 inches wide by 8 inches high about the reference point (6, 5) in the document:

ActiveDocument.Unit = cdrInch
ActiveSelection.SetSizeEx 6, 5, 10, 8

If you want to take the outline of a shape into account when returning the size of that shape, you must use the Shape.GetBoundingBox method. The bounding box for a shape surrounds both the shape and its outline; however, the actual dimensions of a shape specify its width and height irrespective of the size of its outline. The following VBA example uses the GetBoundingBox method to return the size of the active shape:

Dim width As Double, height As Double
Dim posX As Double, posY As Double
ActiveDocument.Unit = cdrInch
ActiveDocument.ReferencePoint = cdrBottomLeft
ActiveShape.GetBoundingBox posX, posY, width, height, True

The Shape.GetBoundingBox method takes parameters that specify the position of the lower-left corner of the shape, the width of the shape, and the height of the shape. The final parameter is a Boolean value that indicates whether to include (True) or exclude (False) the outline of the shape. The Shape.SetBoundingBox method lets you set the size of a shape by specifying the size of its bounding box; however, this method lacks the parameter for specifying whether to include the outline in the new size. If you want to calculate the size and position of the bounding box of a shape without including its outline, you can use the GetBoundingBox method twice (once including the outline and once excluding it), as in the following VBA example:

Public Sub SetBoundingBoxEx(X As Double, Y As Double, _
Width As Double, Height As Double)
Dim sh As Shape
Dim nowX As Double, nowY As Double
Dim nowWidth As Double, nowHeight As Double
Dim nowXol As Double, nowYol As Double
Dim nowWidthol As Double, nowHeightol As Double
Dim newX As Double, newY As Double
Dim newWidth As Double, newHeight As Double
Dim ratioWidth As Double, ratioHeight As Double
Set sh = ActiveSelection
sh.GetBoundingBox nowX, nowY, nowWidth, nowHeight, False
sh.GetBoundingBox nowXol, nowYol, nowWidthol, nowHeightol, True
ratioWidth = Width / nowWidthol
ratioHeight = Height / nowHeightol
newWidth = nowWidth * ratioWidth
newHeight = nowHeight * ratioHeight
newX = X + (nowX - nowXol)
newY = Y + (nowY - nowYol)
sh.SetBoundingBox newX, newY, newWidth, newHeight, False, _
cdrBottomLeft
End Sub
Stretching shapes

You can stretch a shape (or scale it by stretching is proportionately) by using the Shape.Stretch method or the Shape.StretchEx method. Both methods take a decimal value for the stretch, where 1 is 100% (or no change); you cannot use zero, so you must use a very small value instead.

The following VBA code uses the Shape.Stretch method to stretch the selection to half its current height and twice its width, about the midpoint of the bottom edge of its bounding box:

ActiveDocument.ReferencePoint = cdrBottomMiddle
ActiveSelection.Stretch 2, 0.5

If you want to specify the reference point about which to perform a stretch, you can use the Shape.StretchEx method. The following VBA code performs the same stretch as the previous code, but it performs that stretch about the point (4, 5) on the page (in inches):

ActiveDocument.Unit = cdrInch
ActiveSelection.StretchEx 4, 5, 2, 0.5

 
The Shape.Stretch and Shape.StretchEx methods provide an optional Boolean parameter that determines how to stretch paragraph text. A value of True stretches the characters, while False stretches the bounding box and re-flows the text within it.
Skewing shapes

You can skew a shape by using the Shape.Skew method or the Shape.SkewEx method. These methods let you specify the horizontal-skew angle (in degrees, where positive values move the top edge to the left and the bottom edge to the right) and the vertical-skew angle (in degrees, where positive values move the right edge upwards and the left edge downwards).

 
Skews of angles close to or greater than 90° are not allowed.
 
The horizontal skew is applied before the vertical skew.

The difference between the Shape.Skew and Shape.SkewEx methods is the point about which the skew is performed: Skew uses the center of rotation for the shape, while SkewEx uses the specified reference point.

 
You can determine the center of rotation for a shape by returning the values of its Shape.RotationCenterX and Shape.RotationCenterY properties. Changing these values moves the center of rotation for that shape.

The following VBA code uses the Shape.Skew method to skew the selection (about its center of rotation) by 30° horizontally and by 15° vertically:

ActiveSelection.Skew 30, 15
Rotating shapes

You can rotate a shape by using the Shape.Rotate method or the Shape.RotateEx method. These methods rotate the shape by the given angle (in degrees). However, the difference between these methods is the point about which they perform the rotation: Rotate uses the center of rotation for the shape, while RotateEx uses the specified reference point.

 
You can determine the center of rotation for a shape by returning the values of its Shape.RotationCenterX and Shape.RotationCenterY properties. Changing these values moves the center of rotation for that shape.

The following VBA code uses the Shape.Rotate method to rotate the selection (about its center of rotation) by 30°:

ActiveSelection.Rotate 30

The following VBA code uses the Shape.RotateEx method to rotate each selected shape by 15° clockwise about its lower-right corner:

Dim sh As Shape
ActiveDocument.ReferencePoint = cdrBottomRight
For Each sh In ActiveSelection.Shapes
sh.RotateEx -15, sh.PositionX, sh.PositionY
Next sh
Positioning shapes

You can return the horizontal and vertical position of a shape by using the Shape.PositionX and Shape.PositionY properties (respectively). Alternatively, you can use the Shape.GetPosition method to return both the horizontal position and the vertical position of a shape.

 
You can use the Shape.GetBoundingBox method if you want to return the position of a shape, including its outline. For more information on this method, see Sizing shapes.

The following VBA code uses the Shape.GetPosition method to return the position of the selection relative to the current reference point of the active document, which the code explicitly sets to the lower-left corner:

Dim posX As Double, posY As Double
ActiveDocument.ReferencePoint = cdrBottomLeft
ActiveSelection.GetPosition posX, posY

You can also use the Shape.PositionX and Shape.PositionY properties to set the horizontal and vertical position of a shape (respectively), thereby moving that shape to the specified position. Alternatively, you can use the Shape.SetPosition method to move a shape to specified horizontal and vertical position, or you can use the Shape.SetPositionEx method to move the shape to a specified point.

 
You can also use the Shape.SetSizeEx and Shape.SetBoundingBox methods to set the position of a shape. For more information on these methods, see Sizing shapes.

The following VBA code uses the Shape.SetPosition method to set the position of the lower-right corner of each selected shape in the active document to (3, 2) in inches:

Dim sh As Shape
ActiveDocument.Unit = cdrInch
ActiveDocument.ReferencePoint = cdrBottomRight
For Each sh In ActiveSelection.Shapes
sh.SetPosition 3, 2
Next sh

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

 
Document.ShapeMove

Previous Document Next Document Back to Top

Copyright 2013 Corel Corporation. All rights reserved.