English 中文(简体)
改变形象
原标题:rotating an image with mouse move event

我正试图找到一种像样的轮.,在这种风格中,我发现很难将磁盘带移。

我的Xaml代码

<!-- Disk rotating code -->
        <StackPanel x:Name="disk" Margin="0,-60,0,0">
            <StackPanel.Resources>
                <Storyboard x:Name="myStoryboard">
                    <DoubleAnimation Storyboard.TargetName="myTransform" 
                                     Storyboard.TargetProperty="Angle" 
                                     From="0" To="360" Duration="0:0:5"
                                     RepeatBehavior="Forever"> 
                    </DoubleAnimation>
                </Storyboard>
            </StackPanel.Resources>
            <Rectangle x:Name="ttbg" Margin="5,200,30,0" Stroke="Black" StrokeThickness="0" RenderTransformOrigin="0.503,0.503" Height="420" Width="420" MouseLeftButtonDown="press_down" MouseLeftButtonUp="press_up" MouseMove="press_move" >
            <Rectangle.Fill>
                    <ImageBrush ImageSource="Images/ttbg.png" Stretch="Uniform" />
            </Rectangle.Fill>
                <Rectangle.RenderTransform>
                    <TransformGroup>
                        <RotateTransform x:Name="myTransform" />
                    </TransformGroup>
                </Rectangle.RenderTransform>
            </Rectangle>
        </StackPanel>

这是我的C#代码。

private void press_down(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        // TODO: Add event handler implementation here.

    }

    private void press_up(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        // TODO: Add event handler implementation here.
    }

    private void press_move(object sender, System.Windows.Input.MouseEventArgs e)
    {
        // TODO: Add event handler implementation here.

    }

新bie的问题

最佳回答

这是一个小.,需要一些编码。 但我将给它一个尝试。

首先,我改变了你的XAML,以便我使用Canvas,而不是你使用的<编码>Stacknel。 在这种情况下,我更喜欢<代码>Canvas,因为它使我能够使用绝对坐标定位旋转图像,以便我确切知道投票站的哪里。 XAML现在喜欢:

<UserControl x:Class="TurnTable.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:DesignHeight="300" d:DesignWidth="400"
    >

    <Canvas x:Name="LayoutRoot" Background="White">
        <!-- Disk rotating code -->
        <Canvas.Resources>
            <Storyboard x:Name="myStoryboard">
                <DoubleAnimation x:Name="angleAnimation" Storyboard.TargetName="myTransform" 
                                     Storyboard.TargetProperty="Angle" 
                                     From="0" To="0" Duration="0:0:1"
                                     >
                    <DoubleAnimation.EasingFunction>
                        <CubicEase />
                    </DoubleAnimation.EasingFunction>
                </DoubleAnimation>
            </Storyboard>
        </Canvas.Resources>
        <Rectangle x:Name="ttbg" Canvas.Left="100" Canvas.Top="100" Stroke="Black" StrokeThickness="0" RenderTransformOrigin="0.5,0.5" Height="420" Width="420" MouseLeftButtonDown="press_down" MouseLeftButtonUp="press_up" MouseMove="press_move"  >
            <Rectangle.Fill>
                <ImageBrush ImageSource="Images/ttbg.png" Stretch="Uniform" />
            </Rectangle.Fill>
            <Rectangle.RenderTransform>
                <TransformGroup>
                    <RotateTransform x:Name="myTransform" />
                </TransformGroup>
            </Rectangle.RenderTransform>
        </Rectangle>
    </Canvas>
</UserControl>

Also note that I gave the storyboard a duration of 1 second and I gave it an EasingFunction to give the animation a nice look and feel.

在用户控制编码中,我正在使用Math.Atan功能,以计算将 turn改性运动的角。 我正在使用与周转房中心相对的模拟坐标来确定向<代码>Math.Atan提供食物的价值。 守则最后设想如下:

namespace TurnTable
{
    public partial class MainPage : UserControl
    {
        Point _centerOfTurnTable;
        Point _startMousePosition;
        Point _lastMousePosition;
        bool _isMouseCaptured = false;
        double _deltaAngle;

        public MainPage()
        {
            InitializeComponent();
        }

        private void press_down(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            _centerOfTurnTable = new Point
            (
                Canvas.GetLeft(ttbg) + ttbg.ActualWidth / 2d,
                Canvas.GetTop(ttbg) + ttbg.ActualHeight / 2d
            );

            myStoryboard.Pause();

            _startMousePosition = e.GetPosition(this);
            _isMouseCaptured = true;
            _lastMousePosition = _startMousePosition;
        }

        private void press_up(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (_isMouseCaptured)
            {
                _isMouseCaptured = false;

                angleAnimation.From = myTransform.Angle;
                angleAnimation.To = myTransform.Angle + _deltaAngle * 100;
                _deltaAngle = 0;
                myStoryboard.Begin();
            }
        }

        private void press_move(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Point currentMousePosition = e.GetPosition(this);
            if (_isMouseCaptured && Math.Abs(currentMousePosition.X - _centerOfTurnTable.X) > 5)
            {
                _deltaAngle = GetAngleDelta(currentMousePosition);

                myTransform.Angle += _deltaAngle;
                _lastMousePosition = currentMousePosition;
            }
        }

        private double GetAngleDelta(Point currentMousePosition)
        {
            double lastAngleDegrees = GetAngleDegrees(_lastMousePosition);
            double newAngleDegrees = GetAngleDegrees(currentMousePosition);

            if (Math.Sign(lastAngleDegrees) != Math.Sign(newAngleDegrees))
            {
                lastAngleDegrees = newAngleDegrees;
            }
            double delta = newAngleDegrees - lastAngleDegrees;
            return delta;
        }

        private double GetAngleDegrees(Point position)
        {
            return 180d / Math.PI * Math.Atan((position.Y - _centerOfTurnTable.Y) / (position.X - _centerOfTurnTable.X));
        }
    }
}

The code allows you to turn the turntable and when depressing the mouse an animation will be started to allow the turntable to slowly come to a complete stop which gives it a nice touch.

问题回答

暂无回答




相关问题
Howto get started with C# 4.0 and .NET 4.0?

I don t want to download Visual Studio 2010. How can I start studying (not developing real applications) C# 4.0 and .NET 4.0 with just a text editor? Can I just download C# 4.0 compiler and .NET 4.0 ...

Mocking Framework with C# 4.0 Support?

Anybody know of a mocking framework that supports C# 4.0? Doesn t matter which one ATM, just need something that will work.

Unit Testing interface contracts in C#

Using the Code Contracts tools available in VS2010 Beta 2, I have defined an interface, a contract class for that interface and two classes that implement the interface. Now when I come to test the ...

How to Iterate Through Array in C# Across Multiple Calls

We have an application where we need to de-serialize some data from one stream into multiple objects. The Data array represents a number of messages of variable length packed together. There are no ...

IronPython ScriptRuntime equivalent to CPython PYTHONPATH

The following import works inside ipy.exe prompt but fails using IronPython ScriptRuntime inside a C# 4.0 program. import ConfigParser C# code: using System; using System.Collections.Generic; using ...

i cant understand the following code

Matrix<float> trainData2 = trainData.GetRows(intVar >> 1, intVar, 1); intVar is integer type... please help me to understand this code.

热门标签