NShape Programmer Tasks
Shaping the Shape

<< Click to Display Table of Contents >>

Navigation:  Programmer Tasks > Developing a New Shape Class >

NShape Programmer Tasks
Shaping the Shape

Previous pageReturn to chapter overviewNext page

In this step we assume that the new shape has been derived from one of the path based shapes. In this case, you define the appearance of the shape by calculating its graphics path.

To define the appearance of a path based shape

1.Override the method CalculatePath and construct the graphic path in the Path property according to the desired appearance. The graphics path has to be calculated in the origin of coordinates (X = 0, Y = 0) and  takes the current size of the shape in account but not the orientation. The path based shape will rotate the path correctly, if the shape is rotated.

2.For intersection- and hit testing, implement IntersectsWith / IntersectsWithCore and the HitTest method.
For performance reasons, you should also override ContainsPoint / ContainsPointCore without calling the base method because the default method is rather slow (Example: Calling the default implementation on a rotated ellipse is ~300 times (!!) slower than calculating the hit test with geometric functions).

3.In addition to that, you should override the CalculateBoundingRectangle method.
For PathBasedShapes, you should not call the base method because its default implementation uses the GetBounds method of the graphics path which is rather slow (Example: Calling the default implementation on a rotated ellipse is ~3 times slower than calculating the bounds with geometric functions).

4.In order to make your shape resizable, implement the MoveControlPointBy / MovePointByCore method to handle the movement of resize control points. The distances given in these methods refer to the transformed (translated and rotated) shape, so you probably have to transform the coordinates back before manipulating the (untransformed) shape parameters.
When deriving your shape from ShapeBase, you probably have to apply the transformation on your shape. You can do this by overriding the EndMove method.

5.When creating deriving a shape from ShapeBase or PathBasedShape, you have to implement the MoveBy / MoveByCore method.
When deriving your shape from ShapeBase, you probably have to recalculate the draw cache of your shape. You can do this by overriding the EndResize method.

6.In the InitializeToDefault method, define the default appearance of the shape. For example set the Width and the Height of a planar shape to the desired values.
All styles of the shape (usually the LineStyle, the FillStyle, for caption based shapes also the CharacterStyle and the ParagraphStyle)  have to be assigned using the styles provides by the given IStyleSet.

7.Define the properties for the appearance of the shape, colors, line styles etc, add corresponding style properties to the shape and use the styles for the drawing.

8.Compile the library and try out the shape in the NShape Designer. It now looks as required.

The next step is to define the behavior of the control points, which is described in "Defining the Control Points".

See Also

Programmer Tasks: Developing a new shape class