Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

Convert TRect to TRectF in FMX

Writer Andrew Mclaughlin

I'm trying to use the overloaded function of SetBounds() in FMX.Forms, passing Screen.Displays[index].BoundsRect as a parameter. However, since Delphi 11 BoundsRect seems to return a TRectF rather than TRect.

I'm looking for a way to convert this TRectF to a TRect so that I can pass it to SetBounds().

2 Answers

@SilverWarior's answer (and @AndreasRejbrand's comment to it) explains how to convert TRectF to TRect so you can use it with the TForm.SetBounds() method (or TForm.Bounds property).

I just want to mention that, along with the TDisplay.BoundsRect change from TRect to TRectF, Delphi 11 also introduced a new TForm.SetBoundsF() method, and a new TForm.BoundsF property, both of which take floating point coordinates via TRectF instead of integer coordinates via TRect.

So, you don't need to convert the coordinates from floating points to integers at all. You just need to update your code logic to call a different method/property instead, eg:

Pre-D11:

MyForm.Bounds := Screen.Displays[index].BoundsRect;
or
MyForm.SetBounds(Screen.Displays[index].BoundsRect);

Post-D11:

MyForm.BoundsF := Screen.Displays[index].BoundsRect;
or
MyForm.SetBoundsF(Screen.Displays[index].BoundsRect);

The only difference between TRect and TRectF is that TRect is storing its coordinates as integer values while TRectF is storing its coordinates as floating point values. So, all you have to do is convert floating point values stored in TRectF into integers by doing something like this:

Rect.Left := Round(RectF.Left);
Rect.Right := Round(RectF.Right);
Rect.Top := Round(RectF.Top);
Rect.Bottom := Round(RectF.Bottom);

NOTE: Based on your case scenario, you might want to use two other rounding methods that are available in the System.Math unit: Floor() or Ceil().

3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.