Search code examples
delphifiremonkeydelphi-10.2-tokyo

How set the scale of a TControl?


In XE2, I simply used the Scale property to set a control's x- and y-axis scales. In subsequent versions, apparently, the Scale property has become protected in TControl and is published in descended controls.

I have quite a few functions that take a TControl reference and manipulate its scale.

What is the preferred method to set a TControl's scale in 10.2?


Solution

  • You can use the protected hack to overcome this:

    TYPE
      TControlHack = CLASS(TControl);
    
    PROCEDURE SetScale(C : TControl ; NewScale : TPosition);
      BEGIN
        TControlHack(C).Scale:=NewScale
      END;
    

    By declaring a new class inheriting from the original class, you are in effect "dragging" the protected definitions into view when you hard cast the instance to this new class.

    You could also implement it as a class helper:

    TYPE
      TControlHelper = CLASS HELPER FOR TControl
                       STRICT PRIVATE
                         PROCEDURE SetScale(Value : TPosition); INLINE;
                         FUNCTION GetScale : TPosition; INLINE;
                       PUBLIC
                         PROPERTY Scale : TPosition Read GetScale Write SetScale;
                       END;
    
    FUNCTION TControlHelper.GetScale : TPosition;
      BEGIN
        Result:=INHERITED Scale
      END;
    
    PROCEDURE TControlHelper.SetScale(Value : TPosition);
      BEGIN
        INHERITED Scale:=Value
      END;