我是 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;

    }
}

最佳答案

或者您使用 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(...);

关于c# - TranslateTransform 取消 ScaleTransform,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14175404/

10-12 13:28