Scale Transform, Rotate Transform in code : Transform « Graphics « Silverlight






Scale Transform, Rotate Transform in code

Scale Transform, Rotate Transform in code
   
            
<UserControl x:Class='SilverlightApplication3.MainPage'
    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' 
    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
    xmlns:d='http://schemas.microsoft.com/expression/blend/2008' 
    xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' 
    mc:Ignorable='d' 
    d:DesignWidth='640' 
    d:DesignHeight='480'>


  <Grid x:Name="LayoutRoot" Background="White">
    <Ellipse Height="100" HorizontalAlignment="Left" Margin="150,150,0,0" VerticalAlignment="Top" Width="100" Stroke="#FF000000" x:Name="myEllipse">
      <Ellipse.Fill>
        <RadialGradientBrush>
          <RadialGradientBrush.RelativeTransform>
            <TransformGroup>
              <ScaleTransform/>
              <SkewTransform/>
              <RotateTransform/>
              <TranslateTransform X="-0.1" Y="-0.14"/>
            </TransformGroup>
          </RadialGradientBrush.RelativeTransform>
          <GradientStop Color="#FFFFFFFF"/>
          <GradientStop Color="#FF000080" Offset="1"/>
        </RadialGradientBrush>
      </Ellipse.Fill>
    </Ellipse>
  </Grid>
</UserControl>

//File: Page.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightApplication3
{
  public partial class MainPage : UserControl
  {
        private ScaleTransform scaleTransform = new ScaleTransform();
        private RotateTransform rotateTransform = new RotateTransform();

    public MainPage()
    {
      InitializeComponent();
            InitializeScaleTransform();
            InitializeRotateTransform();
            InitializeTransformGroup();            
    }

        private void InitializeScaleTransform()
        {
            scaleTransform.ScaleX = 1.0;
            scaleTransform.ScaleY = 1.0;

            scaleTransform.CenterX = 50.0;
            scaleTransform.CenterY = 50.0;
        }

        private void InitializeRotateTransform()
        {
            rotateTransform.Angle = 0.0;
            rotateTransform.CenterX = 150.0;
            rotateTransform.CenterY = 50.0;
        }

        private void InitializeTransformGroup()
        {
            TransformGroup transformGroup = new TransformGroup();
            transformGroup.Children.Add(rotateTransform);
            transformGroup.Children.Add(scaleTransform);
            myEllipse.RenderTransform = transformGroup;
        }

        private void UserControl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.A)  // A-Key (Left)            
                rotateTransform.Angle -= 3.0;
            else if (e.Key == Key.D)  // D-Key (Right)            
                rotateTransform.Angle += 3.0;
            else if (e.Key == Key.S)  // S-Key (Down)
            {
                scaleTransform.ScaleX *= .97;
                scaleTransform.ScaleY *= .97;
            }
            else if (e.Key == Key.W)  // W-Key (Up)
            {
                scaleTransform.ScaleY *= 1.03;
                scaleTransform.ScaleX *= 1.03;
            }
            else if (e.Key == Key.R)  // R-Key (Reset)
            {
                InitializeScaleTransform();
                InitializeRotateTransform();
            }
        }
  }
}

   
    
    
  








Related examples in the same category

1.Transforming an ObjectTransforming an Object
2.Translate transformTranslate transform
3.Transform demoTransform demo
4.Scale transformScale transform
5.Combine Transforms with TransformGroupCombine Transforms with TransformGroup