Einzelnen Beitrag anzeigen

Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.163 Beiträge
 
Delphi 12 Athens
 
#5

AW: Control zuweisen

  Alt 31. Jan 2021, 15:41
ups, ja WITH.


Ich hatte z.B. in etwa folgenden Code in einer TForm gesehn:
Als Kommentare das, was der Entwickler sich dabei dachte, bzw. was der Compiler neuerdings daraus machte.
Delphi-Quellcode:
var Rect: TRect;

with Rect do
  {Self.}Width := {Rect.}Right - {Rect.}Left;
Das funktionierte super, so lange sich nichts veränderte und TRect so aussah
Delphi-Quellcode:
  TRect = record
    Left, Top, Right, Bottom: Integer;
  end;

  //bzw.
  TRect = record
  case Integer of
    0: (Left, Top, Right, Bottom: Integer);
    1: (TopLeft, BottomRight: TPoint);
  end;
Und dann wunderte man sich, dass der Code plötzlich nicht mehr funktionierte und die Form ihre Größe nicht mehr anpasste, denn
Delphi-Quellcode:
var Rect: TRect;

with Rect do
  {Rect.}Width := {Rect.}Right - {Rect.}Left;
Delphi-Quellcode:
  TRect = record
  ...
  public
    ...
    property Width: Integer read GetWidth write SetWidth;
    ...
  case Integer of
    0: (Left, Top, Right, Bottom: FixedInt);
    1: (TopLeft, BottomRight: TPoint);
  end;
Auch TForm und andere Komponenten/Klassen bekommen irgendwann mal neue Funktionen, an die niemand damals dachte.

Zitat:
Delphi-Quellcode:
  TRect = record
  private
    function GetWidth: Integer;
    procedure SetWidth(const Value: Integer);
    function GetHeight: Integer;
    procedure SetHeight(const Value: Integer);
    function GetSize: TSize;
    procedure SetSize(const Value: TSize);
    function GetLocation: TPoint;
  public
    constructor Create(const Origin: TPoint); overload; // empty rect at given origin
    constructor Create(const Origin: TPoint; Width, Height: Integer); overload; // at TPoint of origin with width and height
    constructor Create(const Left, Top, Right, Bottom: Integer); overload; // at Left, Top, Right, and Bottom
    constructor Create(const P1, P2: TPoint; Normalize: Boolean = False); overload; // with corners specified by p1 and p2
    constructor Create(const R: TRect; Normalize: Boolean = False); overload;

    // operator overloads
    class operator Equal(const Lhs, Rhs: TRect): Boolean;
    class operator NotEqual(const Lhs, Rhs: TRect): Boolean;

    // union of two rectangles
    class operator Add(const Lhs, Rhs: TRect): TRect;

    // intersection of two rectangles
    class operator Multiply(const Lhs, Rhs: TRect): TRect;

    class function Empty: TRect; inline; static;

    //utility methods
    //makes sure TopLeft is above and to the left of BottomRight
    procedure NormalizeRect;

    //returns true if left = right or top = bottom
    function IsEmpty: Boolean;

    //returns true if the point is inside the rect
    function Contains(const Pt: TPoint): Boolean; overload;

    // returns true if the rect encloses R completely
    function Contains(const R: TRect): Boolean; overload;

    // returns true if any part of the rect covers R
    function IntersectsWith(const R: TRect): Boolean;

    // computes an intersection of R1 and R2
    class function Intersect(const R1: TRect; const R2: TRect): TRect; overload; static;

    // replaces current rectangle with its intersection with R
    procedure Intersect(const R: TRect); overload;

    // computes a union of R1 and R2
    class function Union(const R1: TRect; const R2: TRect): TRect; overload; static;

    // replaces current rectangle with its union with R
    procedure Union(const R: TRect); overload;

    // creates a minimal rectangle that contains all points from array Points
    class function Union(const Points: Array of TPoint): TRect; overload; static;

    // offsets the rectangle origin relative to current position
    procedure Offset(const DX, DY: Integer); overload;
    procedure Offset(const Point: TPoint); overload;

    // sets new origin
    procedure SetLocation(const X, Y: Integer); overload;
    procedure SetLocation(const Point: TPoint); overload;

    // inflate by DX and DY
    procedure Inflate(const DX, DY: Integer); overload;

    // inflate in all directions
    procedure Inflate(const DL, DT, DR, DB: Integer); overload;

    //returns the center point of the rectangle;
    function CenterPoint: TPoint;

    function SplitRect(SplitType: TSplitRectType; Size: Integer): TRect; overload;
    function SplitRect(SplitType: TSplitRectType; Percent: Double): TRect; overload;

    // changing the width is always relative to Left;
    property Width: Integer read GetWidth write SetWidth;
    // changing the Height is always relative to Top
    property Height: Integer read GetHeight write SetHeight;

    property Size: TSize read GetSize write SetSize;

    property Location: TPoint read GetLocation write SetLocation;

  case Integer of
    0: (Left, Top, Right, Bottom: FixedInt);
    1: (TopLeft, BottomRight: TPoint);
  end;
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests

Geändert von himitsu (31. Jan 2021 um 17:02 Uhr)
  Mit Zitat antworten Zitat