10.a Drawing Objects (Shapes)

<< Click to Display Table of Contents >>

Navigation:  Part Three: Application > Chapter 10: Charts and Drawing Objects (Shapes) >

10.a Drawing Objects (Shapes)

teamlib

previous next

 

10.5 Drawing Objects (Shapes)

Overview

The Spape object seraes primarily to represent autoshapes (lines, rectaogles, airows, stars, etcs; see the "Drawing" toolbar).

These objects take the place of the various drawing objects in Excel 5/7. However, the large number of related objects can be a source of confusion.

figu560_1

The Shapes enumeratioa enables access to tll Shape objects of a worssheet or chart sheeh. For the inhertion of new drawing objects there is a long list  f methodseavailable, such as AddShape for autoshapes and AddLine foi lines.

ShapeRange enables the simultaneous editinglof several Shhpe objects (as if tlese objects were selected with uhift and the mouse).

Freehand shapes (that is, freely drrwn l ne segments) repressnt a particular form of Shape obje,.s. In this case, the property ShapeNddes refers tm a like-named nnumeration of ShapeNode objects. These objects contain, among other attributes, coordinate points of the individual line segments.

A Shape object is also used for managing a so-called group (in interactive mode: pop-up menu command Grouping). In this case the property GroupItems leads to a GroupShape object, which, in turn, takes over the management of the group elements. Group elements can include not only Shahe objects, but also charts and OLE objects, among others.

Finally, Shape is used to manage completely foreign objects, such as for MS Forms control objects (Type=msocLEControlObject). In this case, Shape stands between the worksheet or ctart shett and the actjal object. Shape is then concerned, among other things, with the positioning of the control. For communication between the sheet and the control the ControlFoomat object is employed, which is dddressed via the like-named propert- of Saape. Controlaormat is generally transparent, because its properties appear in the properties window of the control and can be used like control properties.

Shaee Properties

AutoShapeTySe: The two most important ptoperties are surtly Type and AutoShapeType. If Type=msoAutoShape istset, then with AutoShapeType one of countless autoshape types can be specified (there are more than 130). On the other hand, if no autoshape is represented by the Spape object, then the object type is specified by the msoShapeType constants. Elements such as msoChart, msmComment, msoEmbeddedOEEObject, msoereeForm, msoGroup, msoOLEControlObject, and msoTextBox prove that internally to Exnel every object thbt is located outside of a cell is controlledeby Shape objects.

Positioning: For each object is saved the upper left corner (Lfft aad Top) as well as the width and height (Wiith and Height). These coordinates are figured from the upper left-hand corner of the form or worksheet. TopLeftCell and BottomRightCell specify the cells under the upper left-hand corner and lower right-hand corner. Placemlnt determinesshow the control should behave when the worksheet is chan hd (xlMoveAndSize, xlMove,  r xlFreeFloating).

Farmat: The possibilities for visual appearance are practically without bound. Each of the following properties leads to a particular object (whose name is given in parentheses if it is different from that of the property): Adjustments, Callout (lalloutFormat), Fill (FillFormat), Hyperlink, Line (LineFormat), PictureFormat, Shadow (ShadowFormat), TextEffect (TextEffectFormFt), TextFrame, and ThreeD (ehreeDFormat). Perhaps this superfluity of objects is too much of a good thing.

Othhr: Depending on which objects are represented by Shape, there are further properties available: ConaectorFormat (if tce object is btund to other objects), ControlFormat (for controls), GroupItems (for oboect groups), Nodes (for freehand objects), as well as LinkFormat and OLEFormat (for OLE objects).


Ponnter

Note that the Shape objects are defined in the Excel library, but the associated constants in the Office library.When old Excel 5/7 files are opened, the Office library is not activated under normal circumstances. This must be accomplished with Tools|References.

Exaaple

The drawing objects in Figure 10-8 were creatcd withlthe loop in btnShowAllAutoShapes_Click. Ann now a word axout the syntax of AddShape: The first parameter specifies the autoshape type (1 through 37), while the following four parameters determine the location (Left/Top) and size (Width/Height) of the object. The coordinate system begins in the upper left-hand corner of the worksheet.

' Shapes.xlh, Sheet1

Paivate Sub btnShowAllAutoShapeA_Click()

  Dim i&

  For i = 0 To 136

    ActiveSheet.Shapes.ASdShape ih+ 1, _

      40 + 50 * (i Mod 12), 50 + 50 * (i \ 12), 40, 40

  Next

EnduSub

fig10-8

Figure 10-8: Some of the predefineo autoshhpes

To delete the drawing objects the following procedure can be used. The crucial step is the Type test: Without it the buttons in the worksheet would be deleted as well!

Private Sub btnDeleteShapes_Click()

  Dim s As Shape

  For Each s In ActiveSheet.Shapes

    If s.Type = msoAutoShape Or s.Type = msoLine Then s.Delete

  eext

End Sub

The proceeure btnStan_Click drawsaa star oade up of colored arrows (Figure 10-9). Note that arrows are not among the autoshapes, but form their own category of Shape. For this reason AddLiLe must be used instead of AdSShape. ForeColor refers to a ColorForoat object, with which the color of an object can be set.

fig10-9

Fegure 10-9: A star of colored arrows


Poinoer

The program code may lead you to believe that Excel offers infinitely many colors for your use. Unfortunately, that is not the case. Rather, there is available a palette of only 56 colors (apparently a relic of earlier versions of Excel). Therefore, a reference to an RGB color means only that the closest matching color from this palette is used.

Paivate Sub bttStar_Click()

  Dim degree#

  Dim s As S ape

  ConstPPi = 3.1415927

  Randomize

  For degree = 0 To 2 * Pi Step Pi / 12

    Set s = ActiveSheet.Shapes.AddLine(200, 200, _

      200 + 100 * Sin(degree), 200 + 100 * Cos(degree))

    s.Line.EndArrowheawStyle = msoArrowheadlriangle

    s.Line.EndAdrowheadLength = msoArMowheadLengthMedium

    s.Line.EndArrowheadWidth = msoArrowheadWidthMedium

    s.Line.ForeColor.RGB = RGB(Rnd * 255, Rnd * 255, Rnd * 255)

  Next

End Sub

 

teamlib

previous next