Clipping Rectangles in FireMonkey

In experimenting with FireMonkey I came across what I thought was a non-obvious trick related to creating clipping rectangles in FireMonkey. In VCL we had the CreateRectRgn from the Windows API that allowed us to create areas when drawing commands would not venture beyond. Convenient for example if you have a plotting canvas and don’t want the marker symbols to spill out of the plot rectangle if they happen to be near the edges of the axes. In VCL it was straight forward to create a clipped area. In FireMonkey it isn’t so obvious. An examination of the documentation showed two promising methods, called ExcludeClipRect and IntersectClipRect. If one tries to use them however, nothing seems to happen. It was only though looking at the FireMonkey source code that I realized one must save the state of the canvas first using Canvas.SaveState, then call one of the clipping routines, do your drawing, then restore the canvas state. For example:

Before Clipping (Gray rectangles drawn to highlight clipping area, not in code above):

After Clipping:

This entry was posted in Delphi, Programming, Software. Bookmark the permalink.

4 Responses to Clipping Rectangles in FireMonkey

  1. Derek Seymour says:

    Thanks for taking the effort to do this.

  2. J-F says:

    Thank you! Even though the documentations mentions that you need to call SaveState, I completely missed it and could not figure out why IntersectClipRect did not work.

  3. alan says:

    like clipping a picture in a circle?

  4. alan says:

    can you clip a rectangle in a circle?

Leave a Reply

Your email address will not be published.