我试图在FMX.Forms
中使用SetBounds()
的重载函数,传递Screen.Displays[index].BoundsRect
作为参数。然而,由于Delphi 11BoundsRect
似乎返回TRectF
而不是TRect
。
我正在寻找一种方法将这个TRectF
转换为TRect
,以便我可以将它传递给SetBounds()
。
@SilverWarior的回答(以及@AndreasRejbrand的评论)解释了如何将TRectF
转换为TRect
,以便您可以将其与TForm.SetBounds()
方法(或TForm.Bounds
属性)一起使用。
我只是想提一下,随着TDisplay.BoundsRect
从TRect
到TRectF
的变化,Delphi 11还引入了一个新的TForm.SetBoundsF()
方法和一个新的TForm.BoundsF
属性,它们都通过TRectF
接受浮点坐标,而不是通过TRect
接受整数坐标。
因此,您根本不需要将坐标从浮点数转换为整数。你只需要更新你的代码逻辑来调用一个不同的方法/属性,例如:
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);
TRect
和TRectF
之间的唯一区别是TRect
将其坐标存储为整数值,而TRectF
将其坐标存储为浮点值。因此,您所要做的就是将存储在TRectF
中的浮点值转换为整数,执行如下操作:
Rect.Left := Round(RectF.Left);
Rect.Right := Round(RectF.Right);
Rect.Top := Round(RectF.Top);
Rect.Bottom := Round(RectF.Bottom);
注意:根据您的情况,您可能希望使用System.Math
单元中可用的另外两种舍入方法:Floor()
或Ceil()
。