Page 1 of 1

Element scaling in C++

Posted: 13 Mar 2021, 05:42
by Astalozar
I'm having trouble figuring out how to apply scale to elements through code in c++.
I found this example in the Gallery sample
ScaleTransform* rootScale = (ScaleTransform*)mLayoutRoot->GetLayoutTransform();
rootScale->SetScaleX(scale);
rootScale->SetScaleY(scale);
But it does nothing for me - the scale of the element doesn't change and if I call
rootScale->GetScaleX()
immediately after I set scale, it returns the old value.
Could you point me to the right way of doing this?

Re: Element scaling in C++

Posted: 15 Mar 2021, 10:34
by sfernandez
Hello,

LayoutTransform is used when you want that the new scaled element affects the layout of the container, so it modifies the Size returned by Measure and Arrange.
On the other hand, RenderTransform is not taken into account in the layout and only affects the rendering.

Image

I think you want to use RenderTransform in this case. Also remember to set the correct transform object in your xaml or manually in code (otherwise you will get the default value, the Identity transform, a frozen resource that cannot be modified):
<Grid x:Name="LayoutRoot" RenderTransformOrigin="0.5,0.5">
  <Grid.RenderTransform>
    <ScaleTransform>
  </Grid.RenderTransform>
  ...
</Grid>
ScaleTransform* rootScale = (ScaleTransform*)mLayoutRoot->GetRenderTransform();
rootScale->SetScaleX(scale);
rootScale->SetScaleY(scale);

Re: Element scaling in C++

Posted: 15 Mar 2021, 13:10
by sfernandez
But it does nothing for me - the scale of the element doesn't change and if I call GetScaleX immediately after I set scale, it returns the old value.
I forgot to answer this. Did you set a ScaleTrasnsform object in your mLayoutRoot object, or where you using the default value (the Identity transform).
Is it possible that before calling SetScaleX/Y you animated those values? Take into account that animated values have precendence over locally set values, so you need to clear animation before setting the new values in code:
ScaleTransform* rootScale = (ScaleTransform*)mLayoutRoot->GetLayoutTransform();
rootScale->ClearAnimation(ScaleTransform::ScaleXProperty);
rootScale->SetScaleX(scale);
rootScale->ClearAnimation(ScaleTransform::ScaleYProperty);
rootScale->SetScaleY(scale);
Please let me know if that solves your problem.

Re: Element scaling in C++

Posted: 16 Mar 2021, 07:12
by Astalozar
Problem solved, thank you!

I missed that I had to add the ScaleTrasnsform object in the .xaml file.
New to this language, there's a lot to take in

Thank you for you help!

Re: Element scaling in C++

Posted: 16 Mar 2021, 12:44
by jsantos
Great! Marking as solved