简体   繁体   English

在 FMX 中将 TRect 转换为 TRectF

[英]Convert TRect to TRectF in FMX

I'm trying to use the overloaded function of SetBounds() in FMX.Forms , passing Screen.Displays[index].BoundsRect as a parameter.我试图在FMX.Forms中使用SetBounds()的重载函数,将Screen.Displays[index].BoundsRect作为参数传递。 However, since Delphi 11 BoundsRect seems to return a TRectF rather than TRect .但是,由于 Delphi 11 BoundsRect似乎返回一个TRectF而不是TRect

I'm looking for a way to convert this TRectF to a TRect so that I can pass it to SetBounds() .我正在寻找一种方法将此TRectF转换为TRect以便我可以将其传递给SetBounds()

@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). @SilverWarior 的回答(以及@AndreasRejbrand 的评论)解释了如何将TRectF转换为TRect以便您可以将它与TForm.SetBounds()方法(或TForm.Bounds属性)一起使用。

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 .我只想提一下,随着TDisplay.BoundsRectTRectTRectF的变化,Delphi 11 还引入了一个新的TForm.SetBoundsF()方法和一个新的TForm.BoundsF属性,它们都通过TRectF取浮点坐标通过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:预 D11:

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

Post-D11: 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. TRectTRectF之间的唯一区别是TRect将其坐标存储为整数值,而TRectF将其坐标存储为浮点值。 So, all you have to do is convert floating point values stored in TRectF into integers by doing something like this:因此,您所要做的就是通过执行以下操作将存储在TRectF中的浮点值转换为整数:

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() .注意:根据您的情况,您可能希望使用System.Math单元中可用的其他两种舍入方法: Floor()Ceil()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM