We help IT Professionals succeed at work.
Get Started

WPF Rotation Question.

399 Views
Last Modified: 2013-12-08
Hi,

I have the following code below that uses the:

RotateTransform Class
http://msdn.microsoft.com/en-us/library/system.windows.media.rotatetransform.aspx

(Thanks to experts exchange members for helping providing the code for me to adapt).

This poses some questions.

1. Why does it not rotate a lot faster considering the DispatchTimer is set to 1ms.
2, Does WPF mean that this code is rendered by Direct3D - really going straight
   to DirectX instead of the GDI?

So I am looking for a good explination of what it renders so slowly (however there
might be something obvious I am missing).

Thanks,

Ward




// Here is the XAML Code (Window1.xaml)

<Window x:Class="Rotating_Circle.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="Window1" Height="336" Width="291" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
    <Grid Height="307" Width="296">
        <Rectangle Stroke="#FF000000" Margin="119,71,127,0" x:Name="RectangleToRotate" ToolTip="I rotate" Height="50" VerticalAlignment="Top">
            <Rectangle.Fill>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FF51AF15" Offset="0"/>
                    <GradientStop Color="#FFFFFFFF" Offset="1"/>
                    <GradientStop Color="#FF458020" Offset="0.348"/>
                    <GradientStop Color="#FF21410C" Offset="0.692"/>
                </LinearGradientBrush>
            </Rectangle.Fill>
            <Rectangle.RenderTransform>
                <RotateTransform x:Name="Trans_RotatingButton" 
Angle="0" CenterX="25" CenterY="25" />
            </Rectangle.RenderTransform>
        </Rectangle>
        <Button Height="23" Margin="101,0,102,80" Name="Start_Button" VerticalAlignment="Bottom" Click="Start_Button_Click">Start</Button>
        <Button Height="23" Margin="101,0,102,30" Name="Stop_Button" VerticalAlignment="Bottom" Click="Stop_Button_Click">Stop</Button>
        <Label Height="28" Margin="34,0,0,117" Name="label1" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="52">Angle:</Label>
        <Label Height="28" Margin="0,0,153,117" Name="Angle_Label" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="51"></Label>
    </Grid>
</Window> 

// Here is the Window1.xaml.cs code..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;


namespace Rotating_Circle
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private DispatcherTimer timer;

        public Window1()
        {
            InitializeComponent();
        }

        private void fnStart_timer()
        {
            this.timer = new DispatcherTimer();
            this.timer.Interval = new TimeSpan(0, 0, 0, 0, 1);
            this.timer.Tick += new EventHandler(this.OnTimerTick);
            this.timer.IsEnabled = true;
        }

        private void fnStop_Timer()
        {
            this.timer.Stop();
        }

        private void OnTimerTick(object sender, EventArgs e)
        {
            double i;

            i =  Trans_RotatingButton.Angle + 1;

            if (i > 360) i = 0;

            Trans_RotatingButton.Angle = i;

            Angle_Label.Content = i.ToString();

        }

        private void ButtonRotate_Click(object sender, RoutedEventArgs e)
        {
            if (this.Trans_RotatingButton.Angle == 340)
                Trans_RotatingButton.Angle = 0;
            else
                Trans_RotatingButton.Angle += 20; 
        }



        private void Start_Button_Click(object sender, RoutedEventArgs e)
        {
            fnStart_timer();
        }

        private void Stop_Button_Click(object sender, RoutedEventArgs e)
        {
            fnStop_Timer();
            System.Windows.Application.Current.Shutdown();
        }
    }
}

Open in new window

Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE