0

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().

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
codeGood
  • 175
  • 1
  • 8

2 Answers2

3

@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);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
2

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().

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
SilverWarior
  • 7,372
  • 2
  • 16
  • 22