Search code examples
delphidelphi-7

How to compare two TRect variables in D7?


How can I compare two variables of TRect type?

var
  r1, r1: TRect;
begin
  if (r1 = r2) then
    ...
end; 

With that above I get: Incompatible types.

Thanks!


Solution

  • If you had a modern Delphi, then that code would compile and work. The TRect in modern Delphi versions takes advantage of operator overloading to overload the equality operator. You simply cannot make that syntax work in Delphi 7 since there is no built in equality operator for Delphi records.

    Without that help from the compiler you need a helper function. You can write your own:

    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;
    

    Although, as @Sertac points out, there's little need to write your own EqualRect when you can use the Windows API function of the same name.