Astalozar
Topic Author
Posts: 2
Joined: 13 Mar 2021, 05:35

Element scaling in C++

13 Mar 2021, 05:42

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?

Tags:
 
User avatar
sfernandez
Site Admin
Posts: 2109
Joined: 22 Dec 2011, 19:20

Re: Element scaling in C++

15 Mar 2021, 10:34

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);
 
User avatar
sfernandez
Site Admin
Posts: 2109
Joined: 22 Dec 2011, 19:20

Re: Element scaling in C++

15 Mar 2021, 13:10

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.
 
Astalozar
Topic Author
Posts: 2
Joined: 13 Mar 2021, 05:35

Re: Element scaling in C++

16 Mar 2021, 07:12

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!
 
User avatar
jsantos
Site Admin
Posts: 3059
Joined: 20 Jan 2012, 17:18
Contact:

Re: Element scaling in C++

16 Mar 2021, 12:44

Great! Marking as solved

Who is online

Users browsing this forum: No registered users and 3 guests