Click here to Skip to main content
15,895,777 members
Articles / Desktop Programming / WPF

Tutorial: Creating a Lookless WPF Custom Rotate Control

Rate me:
Please Sign up or sign in to vote.
4.25/5 (12 votes)
28 Mar 2010CPOL4 min read 48.7K   1.8K   31  
A tutorial on how to create a lookless WPF custom control for rotating another element
//-----------------------------------------------------------------------
// <copyright>
//     Copyright (c) TommySoft. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System.Windows;
using NUnit.Framework;
using System.Windows.Media;
using System.Windows.Threading;
using System;
using System.Threading;

namespace TSoftControlLibrary
{
    [TestFixture]
    public class RotateControlTest
    {
        /// <summary>
        /// Test that the angle is coerced to a value between 0 and 360 no matter what it is set to
        /// </summary>
        [Test]
        public void AngleIsBetweenZeroAnd360()
        {
            RotateControl control = new RotateControl();

            control.Angle = 180;
            Assert.AreEqual(180, control.Angle);
            control.Angle = 359;
            Assert.AreEqual(359, control.Angle);
            control.Angle = 360;
            Assert.AreEqual(0, control.Angle);

            control.Angle = -1;
            Assert.AreEqual(359, control.Angle);

            control.Angle = -180;
            Assert.AreEqual(180, control.Angle);

            control.Angle = -360;
            Assert.AreEqual(0, control.Angle);
            control.Angle = -361;
            Assert.AreEqual(359, control.Angle);

            control.Angle = 720;
            Assert.AreEqual(0, control.Angle);
            control.Angle = 721;
            Assert.AreEqual(1, control.Angle);
        }

        [Test]
        public void ShowInWindow()
        {
            Window w = new Window();
            w.Content = new RotateControl() { Width=200, Height=200, BorderBrush=Brushes.Blue, BorderThickness=new Thickness(1)};
            w.ShowDialog();
        }

        [Test]
        public void ShowAndRotate()
        {
            Window w = new Window();
            var ctrl = new RotateControl() { Width = 200, Height = 200, BorderBrush = Brushes.Blue, BorderThickness = new Thickness(1) };
            w.Content = ctrl;
            DispatcherTimer timer = new DispatcherTimer(){ Interval = TimeSpan.FromMilliseconds(50)};
            timer.Tick += delegate {
                ctrl.Angle += 10;
            };
            timer.Start();
            w.ShowDialog();
        }


        [Test]
        public void ShowRotateControlTestWindow1()
        {
            RotateControlTestWindow1 w = new RotateControlTestWindow1();
            w.ShowDialog();
        }

        [Test]
        public void ShowRotateUserControlTestWindow1()
        {
            RotateUserControlTestWindow1 w = new RotateUserControlTestWindow1();
            w.ShowDialog();
        }

        [Test]
        public void ShowUserControlInWindow()
        {
            Window w = new Window();
            w.Content = new RotateUserControl() { Width = 200, Height = 200, BorderBrush = Brushes.Blue, BorderThickness = new Thickness(1) };
            w.ShowDialog();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Norway Norway
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions