How to stack overlapping UI objects front-to-back

How can one control (in code) the order in which objects are drawn in a window “from front to back” ? For example, if an filled oval and a filled rectangle shape intersect, how can one bring either the oval or the rectangle to the front or to the back. In many drawing programs, you see commands such as “Arrange: Bring to Front, Bring Forward, Send Backward, Send to Back”.

If you’re just using simple shapes, draw them on a canvas and draw the top most item last.

If you have xDev then read the SuperDraw article in 12.5.

If not: the approach is simple but very powerful (and a VERY nice example of the benefits of OOP):

Make classes for each shape that know how to draw/select/move/order themselves
A drawing is simply an array of these classes
Make a buffer picture to draw on
Call each item in the array to draw itself to the buffer picture
In the canvas paint event draw the picture onto the canvas