如何比较D7中的两个TRect变量?

Joh*_*ohn 3 delphi delphi-7

如何比较TRect类型的两个变量?

var
  r1, r1: TRect;
begin
  if (r1 = r2) then
    ...
end; 
Run Code Online (Sandbox Code Playgroud)

由此我得到:不兼容的类型.

谢谢!

Dav*_*nan 10

如果你有一个现代的Delphi,那么该代码将编译和工作.在TRect现代德尔福版本需要运营商的优势超载重载相等运算符.由于没有用于Delphi记录的内置等式运算符,因此您无法在Delphi 7中使该语法有效.

如果没有编译器的帮助,您需要一个辅助函数.你可以写自己的:

function EqualRect(const r1, r2: TRect): Boolean;
begin  
  Result := (r1.Left=r2.Left) and (r1.Right=r2.Right) and
            (r1.Top=r2.Top) and (r1.Bottom=r2.Bottom);
end;
Run Code Online (Sandbox Code Playgroud)

虽然,正如@Sertac指出的那样,EqualRect当您可以使用同名Windows API函数时,几乎不需要编写自己的函数.

  • ..或使用`windows.EqualRect`. (8认同)