TranslateTransform取消ScaleTransform

时间:2013-01-05 19:18:19

标签: c# .net wpf

我是WPF的新手,在经过一段时间的游戏之后,我终于按照我喜欢的方式安排了我的控件。我有一个显示图像的矩形。鼠标滚轮使用ScaleTransform动画缩放图像,鼠标单击/拖动可在屏幕上移动图像。问题是如果我向上或向下缩放图像然后尝试移动它,它会自动将其自身缩放回1(原始大小)。有什么方法可以在它缩放时移动它吗? 编辑:我尝试将ScaleTransform和TranslateTransform设置为TransformGroup,但它仍然不起作用。

我遇到的另一个问题是,如果图像大于父容器,它会超出范围,我尝试在父容器上设置ClipToBounds = True,但它不起作用。

代码:

private void Window_MouseWheel_1(object sender, MouseWheelEventArgs e)
{
    if (e.Delta > 0)
    {
        ScaleTransform scaleP = new ScaleTransform();
        scaleP.CenterX = e.GetPosition(this).X;
        scaleP.CenterY = e.GetPosition(this).Y;

        rect.RenderTransform = scaleP;

        DoubleAnimation dblAnimX = new DoubleAnimation();
        dblAnimX.From = scaleXFrom;
        dblAnimX.To = scaleXTo + 0.1;
        scaleXFrom = scaleXTo +0.1;
        scaleXTo += 0.1;
        Duration = new Duration(TimeSpan.FromSeconds(0.15));

        DoubleAnimation dblAnimY = new DoubleAnimation();
        dblAnimY.From = scaleYFrom;
        dblAnimY.To = scaleYTo + 0.1;
        scaleYFrom = scaleYTo +0.1;
        scaleYTo += 0.1;
        Duration = new Duration(TimeSpan.FromSeconds(0.15));

        scaleP.BeginAnimation(ScaleTransform.ScaleXProperty, dblAnimX);
        scaleP.BeginAnimation(ScaleTransform.ScaleYProperty, dblAnimY);
    }
    else
    {
        ScaleTransform scaleM = new ScaleTransform();
        scaleM.CenterX = e.GetPosition(this).X;
        scaleM.CenterY = e.GetPosition(this).Y;

        rect.RenderTransform = scaleM;                           

        DoubleAnimation dblAnimX = new DoubleAnimation();
        dblAnimX.From = scaleXFrom;
        dblAnimX.To = scaleXTo -0.1;
        scaleXFrom = scaleXTo -0.1;
        scaleXTo -= 0.1;
        dblAnimX.Duration = new Duration(TimeSpan.FromSeconds(0.15));

        DoubleAnimation dblAnimY = new DoubleAnimation();
        dblAnimY.From = scaleYFrom;
        dblAnimY.To = scaleYTo - 0.1;
        scaleYFrom = scaleYTo -0.1;
        scaleYTo -= 0.1;
        Duration = new Duration(TimeSpan.FromSeconds(0.15));

        scaleM.BeginAnimation(ScaleTransform.ScaleXProperty, dblAnimX);
        scaleM.BeginAnimation(ScaleTransform.ScaleYProperty, dblAnimY);
    }
}

private void rect_MouseMove_1(object sender, MouseEventArgs e)
{
    if (e.LeftButton == MouseButtonState.Pressed)
    {
        System.Windows.Point p = e.GetPosition(this);
        TranslateTransform tt = new TranslateTransform();
        tt.X = (p.X - mouseDownX);
        tt.Y = (p.Y - mouseDownY);

        rect.RenderTransform = tt;

    }
}

3 个答案:

答案 0 :(得分:4)

或者您使用MatrixTransform

var matrix = Matrix.Identity;
matrix.Scale(1.5, 2.5);
matrix.Translate(30, 60);
rect.RenderTransform = new MatrixTransform(matrix);

甚至更短:

var matrix = new Matrix(1.5, 0, 0, 2.5, 30, 60);
rect.RenderTransform = new MatrixTransform(matrix);

或者更好的是,您每次都避免设置新的RenderTransform并只更新转换矩阵:

// set RenderTransform once in constructor
rect.RenderTransform = new MatrixTransform()

...

// update matrix in event handler
((MatrixTransform)rect.RenderTransform).Matrix = new Matrix(...);

答案 1 :(得分:1)

我没有将以下示例改编为您的案例,因为您提供的代码非常长。如果可以的话,请考虑仅发布它的关键部分。

要合并不同类型的Transform,您可以这样使用TransformGroup

void Button_Click_1(object sender, RoutedEventArgs e)
{
    var button = sender as Button;
    var transformGroup = new TransformGroup();
    var scale = new ScaleTransform(1.5, 2.5);
    var translate = new TranslateTransform(30, 60);
    transformGroup.Children.Add(scale);
    transformGroup.Children.Add(translate);
    button.RenderTransform = transformGroup;
}

答案 2 :(得分:0)

我还尝试将MatrixTransform与鼠标事件配合使用,并成功使其工作如下:

public partial class MainWindow : Window
{
    ....
    // MyMatrixTransform could directly be defined in the xaml file        
    private MatrixTransform MyMatrixTransform = new MatrixTransform();

    public MainWindow()
    {
        InitializeComponent();
        MyMatrixTransform= new MatrixTransform();
        // MyCanvasArea is a canvas defined in the xaml file
        MyCanvasArea.RenderTransform = MyMatrixTransform;
        .....
    }

    // canvas_MouseWheel is attached to canvas in the xaml file as follow
    // <Canvas x:Name="MyCanvasArea" MouseWheel="canvas_MouseWheel" />
    void canvas_MouseWheel(object sender, MouseWheelEventArgs e)
    {
        if (e.Delta > 0)
        {
            Matrix matrix = MyMatrixTransform.Matrix;
            matrix.Scale(1.1, 1.1);
            MyMatrixTransform.Matrix = matrix;
        }
        else
        {
            Matrix matrix = MyMatrixTransform.Matrix;
            matrix.Scale(0.9, 0.9);
            MyMatrixTransform.Matrix = matrix;
        }
    }
    ....
}

也许可以直接访问MatrixTransform中的Matrix字段,但我找不到方法。